-
Notifications
You must be signed in to change notification settings - Fork 24.1k
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
ListView does not re-render a row when property of an object in datasource array is changed #4104
Comments
Make sure to create new objects instead of updating the properties of existing objects. |
Thanks for the reply. So if I have an array of objects as datasource, do I have to create a new array, push objects until the object I want to update, rebuild the particular object by creating a new object and manually pushing properties, then clone the datasource? |
Yeah, that's a good way to do it. Probably would take very few lines of code: let newArray = oldArray.slice();
newArray[indexToUpdate] = {
...oldArray[indexToUpdate],
field: newValue,
};
let newDataSource = oldDataSource.cloneWithRows(newArray); |
That indeed worked, thank you very much! |
Will that ever be fixed in a more elegant way? |
Yeah. I find this to be tad dirty as well. It's does only render the row that was affected. I had previously empty the datasource then repopulated so I appreciate this solution better than I had. |
@ide If a field in the object in the array is an array, how can I push an item? Do I need apply slice() there too? Could you please share how the previous code would be? |
The workaround by @ide defeats the purpose of |
@sshymko The approach I pasted allows your rowHasChanged function to be very simple and very fast. By treating the row objects as immutable objects and always creating new ones instead of mutating existing ones, the rowHasChanged implementation can simply be: rowHasChanged: (r1, r2) => r1 !== r2 |
@ide thank you very much! |
I am working on an 'invite friends' list using ListView. When I click on a friend, a button next to his/her name should toggle active/inactive.
I am fetching a friends list API as initial dataSource. When a friend is clicked, I am taking the particular rowData (an object) and setting a property of 'invited: true', then re-cloning the dataSource. The button next to the friend's name should re-render depending on the property of friend.invited, but renderRow function in ListView is not running; only the render() function is running. It seems like Listview is not recognizing that data has changed because I only updated a property of an object in an array. I was able to confirm that if the dataSource is an array of strings, not objects, changing the string does indeed re-render the row (as in Facebook's ListView example in UIExplorer).
Is this a bug or is there a workaround? I also asked the question here http://stackoverflow.com/questions/33663461/react-native-listview-row-not-re-rendering-after-state-change/33663737#33663737. Thank you.
The text was updated successfully, but these errors were encountered: