Props
Props enable reusability of components by allowing configuration.
To assign props to an element, you add it as you would add attributes to an HTML element.
To read the props inside the component, you need to pass it as a parameter.
Destructuring Props
Instead of repeating props.name
every time, we can destructure the props to make the code cleaner.
Passing children element props
We can also pass in child JSX elements or dynamic HTML content as props in between the component - as follows:
In the component:
Props with a Class Component
For a class component, the passing of props works in a similar way, except that in the component you have to use the this
keyword:
Immutability of Props
Props are immutable, their value cannot be changed from within the component. Attempting to change the props results in TypeError: Cannot assign to read only property.
Last updated