I am tired of doing this all the time:
<Elem x={x} y={y} z={z} /> <Elem x={this.props.x} y={this.props.y} z={this.props.z} />
Is there a way I can get something like this to work?
<Elem x, y, z />
or
<Elem {x, y, z} />
export default App; Basically that's how props are passed from component to component in React. As you may have noticed, props are only passed from top to bottom in React application's component hierarchy. There is no way to pass props up to a parent component from a child component.
Use the spread syntax (...) to pass an object as props to a React component, e.g. <Person {... obj} /> . The spread syntax will unpack all of the properties of the object and pass them as props to the specified component.
If your variables are contained in an object, such as this.props
, you spread the object:
<Elem {...this.props} />
Otherwise, you spread a new object containing the variables you need:
<Elem {...{ x, y, z }} />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With