Giter VIP home page Giter VIP logo

Comments (6)

lewiswolf avatar lewiswolf commented on April 20, 2024 1

This is kinda rough, but should show you what I mean. Thanks!

/*
    this is how I want to write it
*/

class SomeP5Component extends React.Component {
    render() {
    return (
        <div
            style={{
                opacity: this.props.value || 0,
            }}
        >
            <P5Wrapper sketch={this.props.src} />
        </div>
    );
}}

class App extends React.Component {
    state = {
        value: 0,
    }

    render() {
        return (
            <React.Fragment>
                <input type='range' min='0' max='100' value={this.state.value} onChange={(value) => this.setState({ value, })} />
                <SomeP5Component value={this.state.value / 100} src={'./path/to/sketch'}/>
            </React.Fragment>
        )
    }
}

/*
    this is how I currently write it
*/

class SomeP5Component extends React.PureComponent {
    render() {
    return (
        <div>
            <P5Wrapper sketch={this.props.src} />
        </div>
    );
}}

class App extends React.Component {
    state = {
        value: 0,
    }

    render() {
        return (
            <React.Fragment>
                <input type='range' min='0' max='100' value={this.state.value} onChange={(value) => this.setState({ value, })} />
                <div
                    style={{
                        opacity: this.state.value,
                    }}
                >
                    <SomeP5Component src={'./path/to/sketch'} />
                </div>
            </React.Fragment>
        )
    }
}

from react.

lewiswolf avatar lewiswolf commented on April 20, 2024

I thought I'd be a little bit more detailed about my issue. This is the component I'm using, which automatically fits the p5 canvas to its parent element, and changes the size of the canvas as the parent element changes. Changing the size of the canvas will always cause it to re-initialise, but if I were to update the props of my component (say, set the wrapper style for example...) the canvas will also always re-initialise.

At the moment, I've gotten around this by making my component a purecomponent, so I can change the elements it is contained within, but I would like to be able to update the props of my component and not have the p5 canvas re-render.

Please disregard the comment in my code - this component was written for someone who doesn't know how to use react, and my goal was to keep the sketch.js file as clean as possible. Although I left the comment there, I do believe I solved my issue, with your help of course!

from react.

jamesrweb avatar jamesrweb commented on April 20, 2024

@lewiswolf your code looks reasonable to me, perhaps you could attach an example of what you would prefer the usage to look like in an "example of what I want" code suggestion? Happy to support you if I can.

from react.

jamesrweb avatar jamesrweb commented on April 20, 2024

Hey @lewiswolf, could you do something like this or am I misunderstanding the issue?

app.js

import React, { useState } from 'react';
import P5Wrapper from 'react-p5-wrapper';
import { sketch } from './sketch';

function SomeP5Component(props) {
  return (
    <div style={{ opacity: props.opacity || 0 }}>
      <P5Wrapper sketch={props.sketch} />
    </div>
  );
}

function App() {
  const [state, setState] = useState(0);
  return (
    <React.Fragment>
      <input
        type="range"
        min={0}
        max={100}
        value={state}
        onChange={event => setState(event.target.value)}
      />
      <SomeP5Component opacity={state / 100} sketch={sketch} />
    </React.Fragment>
  )
}

export default App;

sketch.js

export function sketch(p5) {
  p5.setup = () => p5.createCanvas(300, 300);

  p5.draw = () => {
    p5.background(100);
    p5.ellipseMode(p5.CENTER);
    p5.ellipse(p5.width / 2, p5.height / 2, 100, 100);
  };
};

If this is the kind of solution you are looking for then I would also suggest skipping the second component in this case and trying this instead for app.js:

import React, { useState } from 'react';
import P5Wrapper from 'react-p5-wrapper';
import { sketch } from './sketch';

function App() {
  const [state, setState] = useState(0);
  return (
    <React.Fragment>
      <input
        type="range"
        min={0}
        max={100}
        value={state}
        onChange={event => setState(event.target.value)}
      />
      <div style={{ opacity: state / 100 }}>
        <P5Wrapper sketch={sketch} />
      </div>
    </React.Fragment>
  )
}

export default App;

The sketch re-rendering depends on it's containing component and so if the containing component must re-render then the sketch must but you can pass props into the sketch and use the myCustomRedrawAccordingToNewPropsHandler function as detailed here to accept the new state and for you to do with as you need to in such a case. I have a comment on this issue which relates somewhat to this point but not sure when I will make such a change, either way, it is just a bit of sugar to what you can already do like I mentioned above anyway if and when I implement the idea in the comment.

If this is not the solution you are looking for, do tell me and I will try to support further 👍

from react.

lewiswolf avatar lewiswolf commented on April 20, 2024

I'm pretty set on class components for this project, although I don't quite know if that effects react's render method. I haven't tried anything yet, but will give a whirl and get back to you. However, 'The sketch re-rendering depends on it's containing component and so if the containing component must re-render then the sketch must', this is what I mean. If P5Wrapper was a PureComponent, it wouldn't update if it's parent element updated.

from react.

jamesrweb avatar jamesrweb commented on April 20, 2024

Closing for now since I don't think it makes sense after testing locally with functional, class and pure components. The render process doesn't seem to differ much and doesn't unneccessarily re-render without a shared state change from what I've seen.

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.