Giter Site home page Giter Site logo

Comments (12)

trafnar avatar trafnar commented on April 25, 2024 7

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.

justinsunho avatar justinsunho commented on April 25, 2024 1

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.

bsaphier avatar bsaphier commented on April 25, 2024

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

from react.

kkarkos avatar kkarkos commented on April 25, 2024

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.

bsaphier avatar bsaphier commented on April 25, 2024

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.

kkarkos avatar kkarkos commented on April 25, 2024

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.

bsaphier avatar bsaphier commented on April 25, 2024

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

from react.

jmandel1027 avatar jmandel1027 commented on April 25, 2024

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.

hifilorau avatar hifilorau commented on April 25, 2024

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

from react.

hifilorau avatar hifilorau commented on April 25, 2024

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

justinsunho avatar justinsunho commented on April 25, 2024

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

jamesrweb avatar jamesrweb commented on April 25, 2024

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)

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.