Git Product home page Git Product logo

Comments (5)

Mo0812 avatar Mo0812 commented on April 27, 2024 1

Hey @jamesrweb,
thanks a lot for your answer. I am not using Next.js, but do use strict mode in react due to the default configuration of the vite@latest composition. By removing the strict mode tag I also get rid of the duplicate canvas.

from react.

jordiyapz avatar jordiyapz commented on April 27, 2024 1

This worked! Please add that workaround into the readme file to help others solving this common confusion :)

For me, I moved the ReactP5Wrapper outside of StrictMode wrapper like this:

// main.tsx
// ...

// Remove the `StrictMode` wrapper, leaving the `App` unwrapped.
ReactDOM.createRoot(document.getElementById("root")!).render(<App />);
// App.tsx
// ...

function App() {
  return (
    <>
      {/* The p5wrapper component is free from `StrictMode` */}
      <ReactP5Wrapper sketch={sketch} />
      <React.StrictMode>
        {/* While this area has strict mode enabled */}
        <p>Hello, strict mode!</p>
      </React.StrictMode>
    </>
  );
}

from react.

jamesrweb avatar jamesrweb commented on April 27, 2024

Is your react app using strict mode?
Are you using NextJS?

P5 binds to the element reference of the containing element, should that element ever change or the reference differ, a new canvas will be rendered. Reacts strict mode does this as part of its "double render test" strategy.

NextJS causes this since the element would be rendered once on the server and once on the client (in such a case you should be using the @P5-wrapper/next implementation instead to work around that specific use case.

from react.

jamesrweb avatar jamesrweb commented on April 27, 2024

Also see #255, #162, etc

from react.

jamesrweb avatar jamesrweb commented on April 27, 2024

This worked! Please add that workaround into the readme file to help others solving this common confusion :)

For me, I moved the ReactP5Wrapper outside of StrictMode wrapper like this:

// main.tsx

// ...



// Remove the `StrictMode` wrapper, leaving the `App` unwrapped.

ReactDOM.createRoot(document.getElementById("root")!).render(<App />);

// App.tsx

// ...



function App() {

  return (

    <>

      {/* The p5wrapper component is free from `StrictMode` */}

      <ReactP5Wrapper sketch={sketch} />

      <React.StrictMode>

        {/* While this area has strict mode enabled */}

        <p>Hello, strict mode!</p>

      </React.StrictMode>

    </>

  );

}

@jordiyapz would you be willing to submit a PR?

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.