How to pass value from variable to React component


Vinso

So I'm using https://github.com/and-who/react-p5-wrapper to create p5.js in React.

I was wondering if there is a way to pass the values ​​from the p5 Sketch file back to React? E.g. I want to keep track of the tag value generated in p5 Sketch and use it somewhere later. How can I store this value globally?

export default function sketch(p5) {

    function modelReady() {
        console.log("Model is ready!!!")
        mobilenet.predict(gotResults)
    }

    function gotResults(error, results) {
        if (error) {
            console.error(error)
        } else {
            **label = results[0].label** <-- This badboy
            mobilenet.predict(gotResults)
        }
    }

What I'm building https://codesandbox.io/s/dark-wave-dgrlg (this is a machine learning cam that will detect what the camera sees) You can see the labels in React are not updating, But only the labels in the p5 sketch are updated.

My workaround: I did something similar in my setInterval(setLabel, 0)code , but it doesn't seem like the correct pattern? Is there a better way?

Solution : https://codesandbox.io/s/jovial-gates-usew3 _

Joseph D

How can I store this value globally?

For global state, use the redux store. You can dispatch an action to store this label.

Note , as stated in the README ,

myCustomRedrawAccordingToNewPropsHandlerThis function is called if the properties of the wrapper component are changing

So I'm going to share the sketch object to make it explicit. This repaint function will be used to access the parent function that actually dispatches to the redux store.

// sketch
export default function sketch(p) {
  // use custom redraw handler
  p.myCustomRedrawAccordingToNewPropsHandler = function (props) {
    // TODO: replace with actual label
    // get value out
    setInterval(() => props.gotResults(null, "done"), 5000);
  };
}
// action
const key = "p5";
const SAVE_LABEL = `${key}/SAVE_LABEL`;

// action creator
function saveLabel(label) {
  return {
    type: SAVE_LABEL,
    payload: {
      label
    }
  }
}

export default function Sketch(props) {
  // dispatch inside component
  const dispatch = useDispatch()

  function gotResults(error, label) {
    if (error) {
      console.error(error);
      return;
    }

    dispatch(saveLabel(label));
  }

  return (
    <P5Wrapper
      sketch={sketch}
      gotResults={gotResults}
    />
  );
}

// TODO: add reducer

Sandbox example : https://codesandbox.io/s/pedantic-breeze-54mde

Related


React JS: Pass a value from route to component

Information Technology Center I have these routes <Router> <Switch> <Route path="/" exact component={Home} /> <Route path="/reports" exact component={ReportPage} /> <Route path="/reports/browse" exact component={ActiveReportsPage} /> <Route p

How to pass value from variable to React component

Vinso So I'm using https://github.com/and-who/react-p5-wrapper to create p5.js in React. I was wondering if there is a way to pass the values from the p5 Sketch file back to React? E.g. I want to keep track of the tag value generated in p5 Sketch and use it so

Pass value from functional component in React

Motys I'm new to ReactJS and JavaScript and now I'm working on a small student project using these technologies. I have a problem with a piece of code: import './List.css'; import Bin from './bin.png' function List({items, listName, accessCode, onBinClick, onN

How to pass the value of a variable through a component in React?

Jaguar 1512 That's my App.js import React, { Component } from 'react' import { BrowserRouter as Router, Route, Link} from "react-router-dom"; import Particles from './components/particles' import Title from './components/start' import Explain from "./componen

How to pass value from variable to React component

Vinso So I'm using https://github.com/and-who/react-p5-wrapper to create p5.js in React. I was wondering if there is a way to pass the values from the p5 Sketch file back to React? E.g. I want to keep track of the tag value generated in p5 Sketch and use it so

How to pass the value of a variable through a component in React?

Jaguar 1512 That's my App.js import React, { Component } from 'react' import { BrowserRouter as Router, Route, Link} from "react-router-dom"; import Particles from './components/particles' import Title from './components/start' import Explain from "./componen

How to pass the value of a variable through a component in React?

Jaguar 1512 That's my App.js import React, { Component } from 'react' import { BrowserRouter as Router, Route, Link} from "react-router-dom"; import Particles from './components/particles' import Title from './components/start' import Explain from "./componen

How to pass value from variable to React component

Vinso So I'm using https://github.com/and-who/react-p5-wrapper to create p5.js in React. I was wondering if there is a way to pass the values from the p5 Sketch file back to React? E.g. I want to keep track of the tag value generated in p5 Sketch and use it so

How to pass value from variable to React component

Vinso So I'm using https://github.com/and-who/react-p5-wrapper to create p5.js in React. I was wondering if there is a way to pass the values from the p5 Sketch file back to React? E.g. I want to keep track of the tag value generated in p5 Sketch and use it so

How to pass value from variable to React component

Vinso So I'm using https://github.com/and-who/react-p5-wrapper to create p5.js in React. I was wondering if there is a way to pass the values from the p5 Sketch file back to React? E.g. I want to keep track of the tag value generated in p5 Sketch and use it so

How to pass value from variable to React component

Vinso So I'm using https://github.com/and-who/react-p5-wrapper to create p5.js in React. I was wondering if there is a way to pass the values from the p5 Sketch file back to React? E.g. I want to keep track of the tag value generated in p5 Sketch and use it so