# How to pass React prop into setup()?

**URL:** https://discourse.processing.org/t/how-to-pass-react-prop-into-setup/11849
**Category:** Libraries
**Created:** [June 5, 2019, 12:52am UTC](https://discourse.processing.org/t/how-to-pass-react-prop-into-setup/11849 "2019-06-05T00:52:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![venn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/venn/32/5430_2.png) [@venn](https://discourse.processing.org/u/venn)
#### Post date: [June 5, 2019, 12:52am UTC](https://discourse.processing.org/t/how-to-pass-react-prop-into-setup/11849/1 "2019-06-05T00:52:22Z")

</div>

Hi legends, I’m trying to figure out a problem with combining React UI with p5.js  
Currently I’m using p5-react-wrapper.  
I do aware @atoro has a more advanced and elegant solution (but it’s currently too complicated) for me to understand and use ):

I’m currently trying to create a slider in React (HTML input range) that will control the diameter of my particles. The prop value is able to get passed into init function scope but not into the setup(). I believe setup() is only run once. If I use the OOTB createSlider from p5 it’ll work but I can’t hook it up with my own React UI.

**In my Sketch.tsx**

```auto
import Particle from "./Particle"
import "p5/lib/addons/p5.dom.min"

export default function sketch(p) {
    let bug1, bug2, diameter
    diameter = 10 // Default value
    
    const init = () => {
        console.log("here0 " + diameter) // Diameter value will get updated when you drag the slider (but not sure how I can pass it into setup()?
        p.setup = () => {
            p.createCanvas(710, 400)
            // diameter = p.createSlider(10, 30, 15, 1)
            bug1 = new Particle(p, diameter)
            bug2 = new Particle(p, diameter)
        }
    }
    init()

    p.myCustomRedrawAccordingToNewPropsHandler = function(props) {
        if (props.diameter) {
            diameter = props.diameter
        }
    }

    p.draw = () => {
        init() // The prop value is able to get read here in the draw() function
        p.background(50, 89, 100)
        bug1.move()
        bug1.display()
        bug2.move()
        bug2.display()
    }
}

```

**Particle.js**

```auto
export default class Particle {
    constructor(p5, diameter) {
        this.p5 = p5
        this.diameter = diameter
        this.x = this.p5.random(300)
        this.y = this.p5.random(300)
        this.speed = 1
    }

    move() {
        this.x += this.p5.random(-this.speed, this.speed)
        this.y += this.p5.random(-this.speed, this.speed)
    }

    display() {
        this.p5.ellipse(
            this.x,
            this.y,
            // this.diameter.value(),
            // this.diameter.value()
            this.diameter,
            this.diameter
        )
    }
}

```

---

<div class="post-metadata">

### Author: ![atoro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/atoro/32/1351_2.png) [@atoro](https://discourse.processing.org/u/atoro)
#### Post date: [June 5, 2019, 6:39am UTC](https://discourse.processing.org/t/how-to-pass-react-prop-into-setup/11849/2 "2019-06-05T06:39:39Z")

</div>

@venn  
 ![demo](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/ef4cccae46ba77240bf92d11e9aef348c905e6e4.gif)

```auto
import React, { useState } from 'react'
import P5Wrapper from 'react-p5-wrapper'
import { render } from 'react-dom'

const NUMBER_OF_BUGS = 5

class Particle {
    constructor(p, diameter) {
        this.p = p
        this.diameter = diameter
        this.scale = this.p.random(0.5, 2.5)
        this.x = this.p.random(300)
        this.y = this.p.random(300)
        this.speed = 1
    }

    move() {
        this.x += this.p.random(-this.speed, this.speed)
        this.y += this.p.random(-this.speed, this.speed)
    }

    display() {
        this.p.circle(this.x, this.y, this.diameter * this.scale)
    }
}

function sketch(p) {
    const bugs = []
    let diameter = 0

    p.setup = () => {
        p.createCanvas(600, 400)
        for (let i = 0; i < NUMBER_OF_BUGS; i++) {
            bugs.push(new Particle(p, diameter))
        }
    }

    p.myCustomRedrawAccordingToNewPropsHandler = (props) => {
        if (props.diameter) {
            diameter = props.diameter
        }
    }

    p.draw = () => {
        p.background(127)
        bugs.forEach((bug) => {
            bug.move()
            bug.diameter = diameter
            bug.display()
        })
    }
}

function App() {
    const [diameter, setDiameter] = useState(15)
    return (
        <>
            <label htmlFor="slider">
                <input
                    id="slider"
                    type="range"
                    min={10}
                    max={30}
                    step={1}
                    value={diameter}
                    onChange={event => setDiameter(+event.target.value)}
                />
                {diameter}
            </label>
            <P5Wrapper sketch={sketch} diameter={diameter} />
        </>
    )
}

render(<App />, window.document.querySelector('#app-root'))

```

---

<div class="post-metadata">

### Author: ![venn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/venn/32/5430_2.png) [@venn](https://discourse.processing.org/u/venn)
#### Post date: [June 5, 2019, 7:12am UTC](https://discourse.processing.org/t/how-to-pass-react-prop-into-setup/11849/3 "2019-06-05T07:12:06Z")

</div>

OMG thanks! @atoro  
You fix my problem that I struggle to fix the whole night!

Btw, quick question,  
+e.target.value I know this is converting string --\> number but how does this work? Putting a “+” infront?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 5, 2019, 7:18am UTC](https://discourse.processing.org/t/how-to-pass-react-prop-into-setup/11849/4 "2019-06-05T07:18:24Z")

</div>

> [@venn](#):
>
> Putting a `+` in front?

The unary plus operator `+` is the cheapest way to coerce most datatypes to number: ➕

- [Expressions and operators - JavaScript | MDN](http://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_())

---

<div class="post-metadata">

### Author: ![atoro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/atoro/32/1351_2.png) [@atoro](https://discourse.processing.org/u/atoro)
#### Post date: [June 5, 2019, 7:28am UTC](https://discourse.processing.org/t/how-to-pass-react-prop-into-setup/11849/5 "2019-06-05T07:28:41Z")

</div>

> Btw, quick question,  
> +e.target.value I know this is converting string → number but how does this work? Putting a “+” infront?

> **[Cast to Number in Javascript using the Unary (+) Operator](https://medium.com/@nikjohn/cast-to-number-in-javascript-using-the-unary-operator-f4ca67c792ce)**
>
> The Unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on…
