Comments (12)
This seems to be working for me (using recommendations from this gatsby documentation page)
The code below is Typescript, you'll need to alter it slightly if you are using regular JS.
Create a new custom component which uses Loadable
import Loadable from "@loadable/component"
export const loadableP5 = Loadable(() => import('react-p5-wrapper'))
instead of importing P5Wrapper, import that custom component, then use it as usual.
import {loadableP5 as P5Wrapper} from './loadableP5'
from react.
Was facing this issue when trying to use react-p5-wrapper
in gatsby.
Just moved the p5
import into componentDidMount()
. It's been working for me.
If anyone else has a better solution, I'm all ears.
from react.
why would you want any p5 stuff to render server side?
from react.
No, I would not want to use it on the server of course. Problem is that with isomorphic/universal react apps the server will load the same JS as on the client, so the code is "isomorph". Some animation libraries use a shim or something like
if (typeof(window) == 'undefined'){
So on the server window
would be skipped. I think this is more a feature and than an issue.
from react.
Oh, I see what you mean.
Wouldn't it work to just put that logic inside the function that you pass to the sketch prop?
from react.
Unfortunately not - once I call 'import X from Y' in a component and this component get's loaded on the server, library + dependencies will be loaded. In 'react-p5-wrapper' p5.js is a dependency which causes the error. So if I am correct p5.js would need to implement the 'if (window)' which would then also work for 'react-p5-wrapper'. That's what I think, but there might be other ways I am not aware of.
from react.
@kkarkos I wonder... would the new dynamic imports in ESNext fix this issue?
from react.
This is still a big issue and causing me a lot of trouble. I tried dynamic importing and it almost worked, the browser rendered the canvas for a second and then crashed. Definitely would love to see this happen!
from react.
@jay-manday @kkarkos Did you guys ever figure out a solution to this issue? It's killing me.
from react.
@justinsunho are you saying you pulled p5 itself out as a dependency of the wrapper and then imported p5 via either CDN or from a .js file in componentDidMount()? I've been building a lot of gatsby sites in the last year and have been trying to get p5 working on and off throughout that time. I feel like i tried something like that once and failed, but if you got it to work and build in gatsby or netlify i want back in!!! Thanks
from react.
@hifilorau I did eventually get it working. Although to be completely honest, I've been using gatsby to learn react so I'm not super sure if it's hacky or not.
I ended up not using this wrapper and instead writing my own: https://github.com/justinsunho/website/tree/master/src/components
I imported p5
from npm on my own.
Check out p5component.js
and sketchData.js
. I also am dynamically rendering the sketches in gatsby if you wanted to check that out too.
I mainly based a lot of my work on @Jinksi's create-react-app
site: https://github.com/Jinksi/jinksi-react (viewable at https://v2.ericjinks.com).
You can see my stuff in action at justinsunho.com, but keep in mind that it's all still in flux.
Also, if you have any tips, I'm more than glad to hear them! Super new to react and gatsby and p5 have been such fun ways of learning about it.
from react.
Closing due to the solutions outlined above being viable.
You can alternatively include the wrapper directly into your HTML output file using this UNPKG link and then use P5 as usual in the wrapper.
from react.
Related Issues (20)
- Migrate to Vite and Vitest
- Migrate to the @P5-wrapper npm org
- Loading image P5-react
- React's strict mode causes the canvas to be created twice HOT 4
- An example of exposing imperative methods from the sketch? HOT 9
- unexpected array behavior inside p5 sketch function HOT 1
- ReactP5Wrapper has additional pixels in height HOT 3
- Could not find a declaration file but the code works HOT 10
- Duplicate canvas when running in vite dev HOT 5
- TypeError: p5.loadSound is not a function HOT 3
- P5-wrapper size HOT 5
- Stop rerender of p5 component when redux variables update HOT 3
- Latest commits not in the npm registry HOT 5
- Issue building with Vite 5 HOT 16
- npm ERR! Usage Error: This project is configured to use npm HOT 2
- windowResized() not firing HOT 4
- Error in nextjs HOT 7
- Change p5 dependency version to minimum 1.4.1 HOT 9
- Examples directory doesn't exist, link broken on README HOT 3
- Module parse failed: Identifier 'XX' has already been declared (7762:41) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.