Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to correctly pass props across components in react?

I am trying to get leva debugger to give my cube a scale property, but When I pass the scale property the cube just disapears with no error. I’ve run it through chat GPT with no luck and cant seem to find out why it doesnt work as there is no error message.

Here is my Cube component:

export default function Cube(scale) {
    return <>
        <mesh receiveShadow  position-y={ - 1 } rotation-x={ - Math.PI * 0.5 } scale={ scale }>
            <boxGeometry />
            <meshStandardMaterial color="greenyellow" />
        </mesh>
    </>
}

Here is my Experience component:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { OrbitControls } from '@react-three/drei'
import { Perf } from 'r3f-perf'
import Cube from './scene_elements/cube'
import GrassPlane from './scene_elements/GrassPlane'
import { useControls } from 'leva'

export default function Experience()
{

    const {scale} = useControls({
        scale: 1
    })
    console.log(scale);
    return <>

        <Perf position="top-left" />

        <OrbitControls makeDefault />

        <directionalLight castShadow position={ [ 1, 2, 3 ] } intensity={ 4.5 } />
        <ambientLight intensity={ 1.5 } />

        <Cube scale={scale}/>

        <GrassPlane/>

    </>
}

>Solution :

default function Cube(scale)

Should be Cube(props) where you use them as props.scale.

Or use object syntax to retreive only the scale key:

Cube({ scale })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading