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.

Onchange function getting triggered on each and every component render

See original GitHub issue

Hey, 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:open
  • Created 3 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ad1992commented, Feb 12, 2021

@CV1928 yes currently onChange gets triggered everytime comp updates but this shouldn’t happen. Will make the change so that it gets called only on elements or appState change. As a workaround, you can cache the version you get using getSceneVersion and check if the version has changed and perform the actions accordingly

1reaction
ad1992commented, Feb 16, 2021

Completely optimizing onChange would take some time as we need to figure out what all cases we need not call onChange 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 ?

Read more comments on GitHub >

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

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