Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native ListView not updating on change #569

Closed
MarkEstefanos opened this issue Sep 19, 2016 · 2 comments
Closed

React Native ListView not updating on change #569

MarkEstefanos opened this issue Sep 19, 2016 · 2 comments

Comments

@MarkEstefanos
Copy link

MarkEstefanos commented Sep 19, 2016

Appending to the list is working, but modifying elements isn't. Here's my code:

@observer
class TodoList extends Component {

  renderRow = (rowData) => {
    return (
      <CheckBox 
      checked={rowData.done} title = {rowData.body} 
      onPress = {() => actions.toggleTodo(this.props.index, rowData.index)}/>
    )
  }

  render() {
    const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => JSON.stringify(r1) !== JSON.stringify(r2)})
    return (
        <ListView
          dataSource={ds.cloneWithRows(state.todoLists[this.props.index].todos.slice())}
          renderRow={this.renderRow}
        />
      </View>
    );
  }
}
  @action toggleTodo(list, index) {
    var todo = state.todoLists[list].todos[index]
    todo.done = !todo.done
    state.todoLists[list].todos[index] = todo
  }

I tried wrapping renderRow in an observer tag as suggested here, but that crashes the app.

@MovingGifts
Copy link

That's not a mobx related issue, as it's React Native's issue for not refreshing on a change of a list view row item, only on newly created items. mobx does indeed update the array, so it does its part, to modify a list view row item in React Native, you have to do what ide suggested in your link, (also reread skleest's comment before ide's solution). By grabbing the index of the item from the old array, overwriting that particular item using its index with the new item/item property, and then setting the new array as the data source, that's how React Native picks up the change.

Again, this is an issue with React Native, not mobx.

@MarkEstefanos
Copy link
Author

Well, that was embarrassingly simple. Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants