Skip to content


 Zabbix
 Grafana
 Prometheus
 React Three Fiber
 Threejs and TypeScript
 SocketIO and TypeScript
 Blender Topological Earth
 Sweet Home 3D
 Design Patterns Python
 Design Patterns TypeScript
   
 Course Coupon Codes
Three.js and TypeScript
Kindle Edition
$6.99 $9.99 Paperback 
$22.99 $29.99




Design Patterns in TypeScript
Kindle Edition
$6.99 $9.99 Paperback
$11.99 $19.99




Design Patterns in Python
Kindle Edition
$6.99 $9.99 Paperback
$11.99 $19.99




Props

Video Lecture

Section Video Links
Props Props Props

Description

Props (a.k.a. properties) are used in React to pass in attributes to your components when you declare them in the JSX.

Our Box from the last lesson is our component that returns a JSX.Element describing a THREE.Mesh.

But we cannot modify any of its properties when we create it, so if we create 2 or more, then they will just be placed in the scene on top of each other in the same position.

Instead, we can add props that we will use to further initialize them with a position when we declare them in the JSX.

./src/Box.jsx

1
2
3
4
5
6
7
8
export default function Box(props) {
  return (
    <mesh {...props}>
      <boxGeometry />
      <meshBasicMaterial color={0x00ff00} wireframe />
    </mesh>
  )
}

./src/App.jsx

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import { Canvas } from '@react-three/fiber'
import Box from './Box'

export default function App() {
  return (
    <Canvas camera={{ position: [0, 0, 2] }}>
      <Box position={[-0.75, 0, 0]} name="A" />
      <Box position={[0.75, 0, 0]} name="B" />
    </Canvas>
  )
}

Note that React will re-render any component instances whenever a change is made to its state or props.

Components and Props reactjs.org codesandbox.io
Spread Syntax developer.mozilla.org
Destructuring Assignment developer.mozilla.org
THREE.Mesh threejs.org
THREE.Object3D threejs.org sbcode.net

Working Example