Onchange function getting triggered on each and every component render
See original GitHub issueHey, I am using react excalidraw package. I am trying to do collaboration feature for 2 people. Both the people will share the below component. Let’s say person A and person B. If person A changes anything in the whiteboard, it will inturn trigger onChange function, which will send data over socket and will be recieved by person B in the useEffect where I am calling updateScene API. The only problem is I am running into infinite loop because onChange function is getting triggered by itself on each and every component render. Am I missing something?
This is what my component returns :
return (
<Excalidraw
ref={props.innerChildRef}
width={width}
height={height}
initialData={InitialData}
onChange={onChange}
/>
)
This is the onChange function :
const onChange = (elements, state) => {
console.log("onChange:::::::::::::::::::::::::::::::::::: :", props.role, elements, "State : ", state);
props.sendWhiteboardSocket(elements)
};
This is my useEffect for updating elements upon receiving elements prop :
useEffect(() => {
if (props.elements && props.innerChildRef && props.innerChildRef.current) {
let newElements = reconcileElements(props.elements)
let sceneData = {"elements" : newElements}
props.innerChildRef.current.updateScene(sceneData);
}
}, [props.elements, props.innerChildRef]);
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
React JS Functional component renders twice on ...
the problem is you're changing a useState value directly from the onChange which causes a re-render and triggers the hook, ...
Read more >Trigger onchange function after component is rendered
I just simply wants to call function supplyLength as soon as the combobox is rendered. Not after changing the value in it. Thanks...
Read more >React onChange Events (With Examples)
The onChange event in React detects when the value of an input element changes. Let's dive into some common examples of how to...
Read more >Simulate React On-Change On Controlled Components
We only care about the state changes and React manages all events triggered on itself. Also if we don't care about onChange event...
Read more >How React onChange event handlers work - with code ...
This way, React component that render <input> elements will also control ... Every time the onChange event is triggered, React will pass the ......
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
@CV1928 yes currently
onChange
gets triggered everytime comp updates but this shouldn’t happen. Will make the change so that it gets called only onelements
orappState
change. As a workaround, you can cache the version you get using getSceneVersion and check if the version has changed and perform the actions accordinglyCompletely optimizing
onChange
would take some time as we need to figure out what all cases we need not callonChange
callback. But we will roll out the optimization in patches very soon. Since you are maintaining multiple instances of Excal so you would be having some kind of mapping among the instances right so you can use that to maintain version cache mapping in memory ?