DEV Community

Cover image for Integrating P5.js with React

Integrating P5.js with React

Christian on February 13, 2020

In the course of my final project at the Flatiron School in software engineering, I really wanted to push my skills farther than what I was comfort...
Collapse
 
rizz0s profile image
Summer Rizzo

Thank you so much! I was initially using a package that ultimately made what I was trying to do more complicated (I also needed the ability for user input). I'd probably be tearing my hair out if I didn't stumble upon this.

Collapse
 
christiankastner profile image
Christian

Awesome! Stoked to help

Collapse
 
dcsan profile image
dc

Unfortunately this approach just keeps creating new instances of a p5 component on each "hot update" when using create-react-app or similar.

Collapse
 
nardove profile image
Ricardo Sanchez • Edited

A solution can be found here lloydatkinson.net/posts/2022/how-t...

Basically, if you follow the example in the comments above all you need to do is:

useEffect(() => {
  const myP5 = new p5(Sketch, myRef.current);
  return myP5.remove;
},[])
Enter fullscreen mode Exit fullscreen mode

This will remove the current p5 instance and creates a new one on the next render

Collapse
 
goksuokar profile image
Goksu

yess.. did you find a solution for this? I'm trying to create a sorting visualizer using react and p5 and it's just not working..

Collapse
 
witherc0d3 profile image
WITHERC0D3

Hello, thanks so much for this constructive document answer. I am new to react and i am developing a project with my friends. I have a doubt regarding the same topic. is there a way to do this as a functional component?

Collapse
 
anshul16 profile image
Anshul Sachdev

import React, { useEffect } from 'react';
import p5 from 'p5';

const App = () => {

const myRef = React.createRef();

const Sketch = (p) => {

 p.setup = () => {
  p.createCanvas(300, 200);
  p.noStroke();
 }

 p.draw = () => {
  p.background('orangered');
  p.ellipse(150, 100, 100, 100);
 }
Enter fullscreen mode Exit fullscreen mode

}

useEffect(() => {
const myP5 = new p5(Sketch, myRef.current)
},[])

return (
  <div ref={myRef}>
    Hello
  </div>
)
Enter fullscreen mode Exit fullscreen mode

}

export default App;

Collapse
 
ndutared profile image
Abby Nduta

Thanks for the response @anshul16. It will surely help someone in the community.

Collapse
 
artydev profile image
artydev

Hello Christian,

Thank you.
You can test your code here P5inReact

Regards

Collapse
 
snoogans775 profile image
Kevin Fredericks • Edited

THANK YOU SO MUCH. I don't know why we can not import Javascript functions directly in to React components. I guess I am missing something fundamental. Your work is really helpful for my current project. P5.js is great. Cheers.

edit: I also love the way you've structured the projects. Great balance between readability and modularity.

Collapse
 
dcsan profile image
dc

can you be a bit more clear about why this is a better option than using the wrapper libraries, that people have put a lot of work into? I assume there's some complication that is created by that layer of abstraction but fear it would only be after spending a lot of time working on this that would become clear....?

Collapse
 
celetra profile image
Celetra

Thanks, this was really helpful! I'm using p5 to make a dynamic background for my website, and I was having trouble integrating it into a React app. It's very nice to see how the whole process is put together instead of adding yet another dependency to magically make it work.

Collapse
 
yesklin profile image
Carlos Daniel

"I learned very quickly that best option is to actually just learn the tech and not treat it like a black box", this phrase couldn't be more right!
Thank you, this post was very helpful.

Collapse
 
adithyavasudevan profile image
adithyavasudevan

Christian would it be possible for you to Git a stub project? Loved the simplicity of your explanations

Collapse
 
iris666 profile image
Iris

Thank you so much for the tutorial! I'm wondering have you tried to deploying your website? I'm trying to deploy my website which integrated p5.js with react, but it always shows that "(undefined) ReferenceError: window is not defined". Have you encountered this kind of issue?

Collapse
 
dannyroberts95 profile image
dannyroberts95

Not all heros don't wear capes. You're a gent for posting this!

Collapse
 
thisisgiorgio profile image
Giorgio Martini

Thanks! But its truly a bummer and a pain to have to prefix every thing in p5js with something...

Collapse
 
subimage profile image
Vortex Pigeon

Really nice. Great work! Thanks for the clear writeup.

Collapse
 
risingbirdsong profile image
Peter

Thanks Christian! Very helpful article :)

Collapse
 
xiaoju profile image
Jerome Clerambault

thank you!!!