4

Running onClick handler which use event properties cause infinite loop in react :

import React, {useState, useEffect} from 'react';
import Plot from 'react-plotly.js';
import ReactDOM from 'react-dom';

function PlotlyTest() {
    const [coord, setCoord] = useState([]);
    
    const handleClick = event => {
        const point = event.points[0];
        setCoord([point.x, point.y, point.z])
    }

    useEffect(() => console.log(coord))

    return (
        <div>
            <Plot
            data={[
                {
                x: [-2.294, -2.28, -2],
                y: [6.18, 7.19, 8],
                z: [
                    [0.16, 0.16, 0.16],
                    [-0.11, -0.22, -0.62],
                    [-0.1, -0.2, -0.3]
                ],
                    type: 'surface',
                    "mode": "markers",
                    opacity: 0.6
                },
            ]}            
            onClick={handleClick}
            />
        </div>
    );
}

ReactDOM.render(
  <React.StrictMode>
      <PlotlyTest/>
  </React.StrictMode>,
  document.getElementById('root')
);

infinite render loop in

3
  • 1
    changing setCoord([point.x, point.y, point.z]) to setCoord([] + [point.x, point.y, point.z]) solves the problem, but I dont know why Commented Jan 29, 2021 at 20:53
  • 1
    Seems to be a problem for the surface type specifically. You could also make PlotlyTest a class component and do this or try memo so the updating isn't too eager. I also have no idea why setCoord([] + [point.x, point.y, point.z]) works. Commented Jan 29, 2021 at 22:44
  • i have the same issue, any solutiion? Commented Jun 2, 2022 at 22:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.