Reaction, how to transfer props to subcomponents

How to pass all the props of a component from its parent to its children in React

Suppose you have a hierarchy of components, in which you pass props from the top component, and you need to pass those unchanged props to the children. It happened many times, and you don’t actually want to do this:

const IntermediateComponent = (props) => {
  return (
    <ChildComponent prop1={props.prop1} prop2={props.prop2} />
  )
}

Instead, you want to pass all items, regardless of their name.

you can use itSpread operator:

const IntermediateComponent = (props) => {
  return (
    <ChildComponent {...props} />
  )
}

This grammar is easier to understand, less error-prone, and it is flexible, because there is no need to change the name of the prop or add props to the intermediate components when changing the props.

Download mine for freeResponse Handbook


More response tutorials: