Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Updating React state within JavaScript eval()

I'm writing a live-coding web app in React where the user can write javascript code that moves a player icon through a maze. I want to do this all on the frontend, so I'm using eval() to run the user's code rather than sending it to a backend for evaluation. I am finding some behavior involving eval() and updating graphics/setting state that is causing an issue I can't get around. Below is a simplified version of the functional component in charge of displaying the maze graphics. It accepts the prop code, which updates when the user clicks a "run" button.

export default function MazeEngine(props) {

        const code = props.code;
        const [player, setPlayer] = useState({position: 0});
        const canvasRef = useRef(null);

          useEffect(() => {
            runCode();
          }, [code])

          useEffect(() => {
            draw(canvasRef.current);
          }, [player])

        const move = () => {
           setPlayer((pl) => {..pl, position: pl+1});
        }

        const sleep = (ms) => {
            var start = new Date().getTime(), expire = start + ms;
            while (new Date().getTime() < expire) { }
            return;
          }
        }

        const runCode = () => {
            eval(code);
        }  

        const draw = (ctx) => { // implementation not shown}

    return(
        <canvas ref={canvasRef} style={{width:350, height:350}}>
    )
}

What I have found is that if the user makes multiple calls to move(), all of the calls to setPlayer are batched for after eval() completes. So, for example, if the user writes

move();
sleep(1000);
move();

the display will update after 1 second, with the player moved two spaces forward. I verified that the effect on player is only called once.

Instead, I would like the player to move forward one space, render the new position, wait one second, and move forward another space. That way the user can more easily see the behavior of their algorithm.

using references instead of state

I attempted a different approach where I changed player to being a reference with useRef, and called draw() directly in the move method rather than using useEffect. This gave the same problematic behavior - the canvas does not update until the completion of the eval.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!