React Props
Explore how to move data from global scope into component state and pass it via props, enabling clear communication between parent and child components. Understand how props prevent global scope pollution and allow data handling across multiple components in React.
We'll cover the following...
We are currently using the list variable as a global variable in the current application. We used it directly from the global scope in the App component, and again in the List component. This could work if you only had one variable, but it doesn’t scale with multiple variables across multiple components from many different files.
By using props in react, we can pass variables as information from one component to another component. This creates a clear communication channel between parent and child components.
Before using props, we’ll move the list from the global scope into the App component and rename it to its actual domain:
Next, we’ll use React props to pass the array to the List component:
The variable is called stories in the App component, and we pass it under this name to the List component. In the List component’s instantiation, however, it is assigned to the list attribute. We access it as list from the props object in the List component’s function signature: