-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
Using setState & this.props in onLayout callback of SectionList throws an error - "this.props" should not be accessed during state updates #36329
Comments
The invariant violation here is that VirtualizedList is accessing Could you post the full callstack when this happens? I would expect to see more VirtualizedList code happening lower in the stack. It seems most likely that VirtualizedList is calling In the meantime I would recommend deferring any imperative methods until after the |
Summary: VirtualizedList refactoring moved [a call of `_updateViewableItems`](https://www.internalfb.com/code/fbsource/[a9d4ad3cf149][history][blame]/xplat/js/react-native-github/Libraries/Lists/VirtualizedList.js?lines=1431-1447) to the inside of a state update. This call may trigger an `onViewableItemsChanged`, which is normally not an issue (minus changing timing), but creates problems if the users callback then calls imperative methods on the VirtualizedList, since the batched state update may be in the process of changing the props/state the imperative method is reading. See facebook#36329 for what I suspect is an example of this. This moves the `_updateViewableItems` call to before the state update, like the previous version of VirtualizedList. Changelog: [General][Fixed] - Avoid VirtualizedList viewability updates during state updates Reviewed By: javache Differential Revision: D43665606 fbshipit-source-id: 18d41eb7f05f3f16a13df211b4ab5778ea405e16
#36340 is landing and should likely fix your issue. Feel free to make a pick request for this, but you can otherwise workaround this by avoiding scrolling in callbacks for viewability changes (semi-related, calling |
Summary: VirtualizedList refactoring moved [a call of `_updateViewableItems`](https://www.internalfb.com/code/fbsource/[a9d4ad3cf149][history][blame]/xplat/js/react-native-github/Libraries/Lists/VirtualizedList.js?lines=1431-1447) to the inside of a state update. This call may trigger an `onViewableItemsChanged`, which is normally not an issue (minus changing timing), but creates problems if the users callback then calls imperative methods on the VirtualizedList, since the batched state update may be in the process of changing the props/state the imperative method is reading. See #36329 for what I suspect is an example of this. This moves the `_updateViewableItems` call to before the state update, like the previous version of VirtualizedList. Changelog: [General][Fixed] - Avoid VirtualizedList viewability updates during state updates Reviewed By: javache Differential Revision: D43665606 fbshipit-source-id: 9398273c5209e371e69aafb02bac173c69874273
Summary: VirtualizedList refactoring moved [a call of `_updateViewableItems`](https://www.internalfb.com/code/fbsource/[a9d4ad3cf149][history][blame]/xplat/js/react-native-github/Libraries/Lists/VirtualizedList.js?lines=1431-1447) to the inside of a state update. This call may trigger an `onViewableItemsChanged`, which is normally not an issue (minus changing timing), but creates problems if the users callback then calls imperative methods on the VirtualizedList, since the batched state update may be in the process of changing the props/state the imperative method is reading. See facebook#36329 for what I suspect is an example of this. This moves the `_updateViewableItems` call to before the state update, like the previous version of VirtualizedList. Changelog: [General][Fixed] - Avoid VirtualizedList viewability updates during state updates Reviewed By: javache Differential Revision: D43665606 fbshipit-source-id: 9398273c5209e371e69aafb02bac173c69874273
New Version
0.71.2
Old Version
0.70.4
Build Target(s)
iOS 11.0 & above
Output of
react-native info
Issue and Reproduction Steps
Problem
Using setState & this.props in onLayout callback of SectionList throws an error
Error
"this.props" should not be accessed during state updates
Reproduction steps
Create a
MySectionList
class component that uses aSectionList
with below props & anonLayout
callback where you call a local class method before calling the passed down onLayout callback to your class component.CC: @NickGerleman
The text was updated successfully, but these errors were encountered: