question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any example using hooks?

See original GitHub issue

Hi 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:open
  • Created 3 years ago
  • Reactions:11
  • Comments:8

github_iconTop GitHub Comments

4reactions
callmemonkycommented, Jan 12, 2022

Hey @callmemonky, I ended up using Class Components. I didn’t try any new solution since.

Solution that worked for me.

const _dataProvider = useMemo(() => {
  return new DataProvider((r1, r2) => {
    return r1 !== r2;
  });
}, []);

const newDataProvider = useMemo(() => {
  return _dataProvider.cloneWithRows(listData);
}, [listData]);


const _layoutProvider = useMemo(() => {
  return new LayoutProvider(
    (index) => {
      console.log(newDataProvider.getDataForIndex(index)); //This will work.
      return 1;
    },
    (type, dim) => {
      switch (type) {
        case 1:
          dim.width = width;
          dim.height = 100;
          break;
        default:
          dim.width = 0;
          dim.height = 0;
      }
    }
  );
}, [newDataProvider]);

2reactions
EmersonPinheirocommented, Jun 22, 2020

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found