Relative Content

Tag Archive for javascriptreactjsstatescroller

Moving the scroller of a list container based on the position of an item in React

I have a list container and separate items that open as separate pages/routes. I’m trying to dinamically move the scroller of the container to a desired position based on the element’s position. For that I pass a function to the child item that retrieves the position of the item and then in the parent list, after updating the position state I try to set the scroller in useEffect. I have no problem retrieving the item position and setting the current position state in the parent. But the scroller itself never moves. To test with simpler code I checked how the state updates. The parent does re-render and the position does update but scroller position stays the same. I’m a new learner so I must be doing somethiong wrong. I have a separate context for more complex state updates, maybe the way I also update state in the component and not in the provider isn’t consistent and interferes with other async state updates (which involve retrieving the list of items with a request from a mock server)?