React: Hi guys, please help me, I think it's a bug

Created on 30 Apr 2019  路  2Comments  路  Source: facebook/react

When I wheel mouse on simple div it's working correctly and give me useState [1,2,3,4,5,6,7,8,9,0] value right
but when I wheel mouse on div that added by ReactDOM.createPortal it doesn't work correctly and give me useState [] value

https://codesandbox.io/s/8n7n7x0wm9

Most helpful comment

In ModalSelf, you're attaching the onwheel function as an event handler, only once, on component creation. But the onwheel function you're passing to it closes over rows, which represents the current value of the rows state object at the moment ListViewItems was rendered. Each call to setRows causes ListViewItems to be rerendered, which causes the ListViewItems function to run again, with a new value for rows, and hence a new copy of the onwheel function with a new closed over rows value. But you never attach the new onWheel function to the modal.

It works in the other case because when you tell react to onWheel something, it will constantly update it to use the new function as the function.

This is not a bug.

If you have to manage events manually and not use react to do them, the usual approach is to use useLayoutEffect (for functional components) or componentDidMount / componentDidUpdate for class components: https://codesandbox.io/s/92611r7wpr

All 2 comments

In ModalSelf, you're attaching the onwheel function as an event handler, only once, on component creation. But the onwheel function you're passing to it closes over rows, which represents the current value of the rows state object at the moment ListViewItems was rendered. Each call to setRows causes ListViewItems to be rerendered, which causes the ListViewItems function to run again, with a new value for rows, and hence a new copy of the onwheel function with a new closed over rows value. But you never attach the new onWheel function to the modal.

It works in the other case because when you tell react to onWheel something, it will constantly update it to use the new function as the function.

This is not a bug.

If you have to manage events manually and not use react to do them, the usual approach is to use useLayoutEffect (for functional components) or componentDidMount / componentDidUpdate for class components: https://codesandbox.io/s/92611r7wpr

Hi
I said that "I think it's a bug" 馃檶馃檲
I'm lately starting to work with React and for learn and understand it, I need time and good people (like you馃槝)

Thank a lot馃尮

Was this page helpful?
0 / 5 - 0 ratings