Any example using hooks?
See original GitHub issueHi guys!
I’m working on RLV with react hooks, and here comes an issue that when using forceNonDeterministicRendering
.
I’ve given estimated height to LayoutProvider, and it works well in the first rendering, but then I refresh the list by pulling down <RefreshControl />
component, the item height becomes the estimated height rather than the real height.
Then I find out that it is because I’ve used dataProvider in the layoutProvider. The codes are something like:
// I'm using useState hooks because dataProvider are saved in the component's state in the example code
const [dataProvider, setDataProvider] = useState<DataProvider>(
new DataProvider((r1, r2) => r1 !== r2),
)
const layoutProvider = useMemo(
() =>
new LayoutProvider(
index => {
const item = dataProvider.getDataForIndex(index)
return item.type
},
(type, dim) => {
//...
},
),
[dataProvider],
)
// this is how I update dataProvider
useEffect(() => {
setDataProvider(d => d.cloneWithRows(listData))
}, [listData])
I think this problem may be caused by updating layoutProvider
or dataProvider
incorrectly. And I tried to find best practice when using react hooks but I didn’t make it. Could you please offer some demo using react hooks? I’ll appreciate your help very much!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:11
- Comments:8
Top Results From Across the Web
Hooks at a Glance - React
Hooks are functions that let you “hook into” React state and lifecycle features from function components. Hooks don't work inside classes — they...
Read more >The Guide to Learning React Hooks (Examples & Tutorials)
Learn all about React Hooks with this hands-on guide. Includes tutorials and code examples on using hooks for state and effects, for context...
Read more >useHooks - Easy to understand React Hook recipes
This website provides easy to understand code examples to help you learn how hooks work and inspire you to take advantage of them...
Read more >React Hooks cheat sheet: Best practices with examples
In this tutorial, we'll outline some React Hooks best practices and highlight some use cases with examples, from simple to advanced ...
Read more >Useful React Hooks That You Can Use In Your Projects
A good example of this is the use of the Redux connect Higher Order Component (HOC) to subscribe to the Redux store. Like...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Solution that worked for me.
Hello there! Right now I’m facing a similar issue, better described here https://stackoverflow.com/questions/59573435/recyclerlistview-scrolls-to-top-onendreached-with-functional-component and in #449.
My code is quite like @SunskyXH 's, but instead of using the useMemo hook, I’m using layoutProvider as a state to prevent recreating it. Everything works pretty well, until I try to access the dataProvider state inside layoutProvider, which is done because my item layout depends on the data content (apparently using layoutProvider as a state with useState hook doesn’t get the updated value of dataProvider’s state, any idea on that?).
Until now I wasn’t able to figure what the problem is, but looking foward to try the workaround presented by Mike in the StackOverflow question. I’d also appreciate a demo using react hooks. Thanks in advance!