Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does not work with isomorphic react apps #2

Closed
kkarkos opened this issue Feb 11, 2017 · 12 comments
Closed

does not work with isomorphic react apps #2

kkarkos opened this issue Feb 11, 2017 · 12 comments

Comments

@kkarkos
Copy link

kkarkos commented Feb 11, 2017

Unfortunately p5 won't work with isomorphic react apps due to window and documentrequirements. Any thoughts?

@bsaphier
Copy link
Contributor

why would you want any p5 stuff to render server side?

@kkarkos
Copy link
Author

kkarkos commented Feb 16, 2017

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.

@bsaphier
Copy link
Contributor

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?

@kkarkos
Copy link
Author

kkarkos commented Feb 16, 2017

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.

@bsaphier
Copy link
Contributor

bsaphier commented May 28, 2017

@kkarkos I wonder... would the new dynamic imports in ESNext fix this issue?

@jmandel1027
Copy link

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!

@hifilorau
Copy link

@jay-manday @kkarkos Did you guys ever figure out a solution to this issue? It's killing me.

@justinsunho
Copy link

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.

@hifilorau
Copy link

@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

@justinsunho
Copy link

@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.

@trafnar
Copy link
Contributor

trafnar commented Jul 9, 2019

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'

@jamesrweb
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants