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
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馃尮
Most helpful comment
In ModalSelf, you're attaching the
onwheelfunction as an event handler, only once, on component creation. But theonwheelfunction you're passing to it closes overrows, which represents the current value of the rows state object at the moment ListViewItems was rendered. Each call tosetRowscauses ListViewItems to be rerendered, which causes the ListViewItems function to run again, with a new value forrows, and hence a new copy of theonwheelfunction 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
onWheelsomething, 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) orcomponentDidMount/componentDidUpdatefor class components: https://codesandbox.io/s/92611r7wpr