
Youāll have understood by now that reusing components is at the very heart of Reactās logic. However, to be reused, components often require configuration, which is where props come in. š„³
Do you realize that you've already used a prop? Thatās right ā the Ā keyĀ prop in the chapter on lists! So youāve seen the syntax.
Letās go back to our plant site. Weāre now going to create a new component that weāll reuse. The idea is to create a water scale and a light scale for each plant.
Weāll reopen Ā ShoppingList.jsĀ andĀ add the relevant data in Ā plantList:Ā
waterĀ data, which corresponds to the recommended amount of water for each plant.
lightĀ data, which corresponds to the amount of light required.
Starting with light, in each plant item, add a Ā CareScaleĀ component to which weāll pass the value prop:
<CareScale scaleValue={plant.light} />Ā But hold on, how do we get the value of a prop into our component?Ā
Ā You get props from the parameters of the function that defines your component.Ā
So, for Ā CareScaleĀ ,Ā you have:
function CareScale(props) {
const scaleValue = props.value
return <div>{scaleValue}āļø</div>
}
export default CareScaleBut didnāt we say we wanted a scale from 1 to 3?
To do this, go for a list, which you can create manually.
YouĀ end up with:
function CareScale(props) {
const scaleValue = props.value
const range = [1, 2, 3]
return (
<div>
{range.map((rangeElem) =>
scaleValue >= rangeElem ? <span key={rangeElem.toString()}>āļø</span> : null
)}
</div>
)
}Congratulations! Youāve just used props!
Props areĀ objects that you get from the parameters of your function component.Ā Ā
Letās take this concept a bit further to configure our component fully.
Start by specifying aĀ Ā typeĀ Ā propĀ for theĀ Ā CareScaleĀ component and then reuse this component for both water and light:
<CareScale careType='water' scaleValue={plant.water} />
<CareScale careType='light' scaleValue={plant.light} />Now adapt the Ā CareScaleĀ to get the Ā careTypeĀ .Ā
We can now configure theĀ CareScaleĀ component so it can generate the data for water and light:
function CareScale({ scaleValue, careType }) {
const range = [1, 2, 3]
const scaleType = careType === 'light' ? 'āļø' : 'š§'
return (
<div>
{range.map((rangeElem) => scaleValue >= rangeElem ? <span key={rangeElem.toString()}>{scaleType}</span> : null
)}
</div>
)
}As you can see, we used two different syntaxes for our props. For Ā scaleTypeĀ , we assigned a Ā stringĀ between quotes. However, we gaveĀ Ā scaleValueĀ the value of an object, which we passed between curly braces.Ā
In practice, you can give a prop any possible value in JavaScript, but syntactically, in JSX, you only have two options:
AĀ Ā StringĀ literal, marked by quotes.Ā
Curly braces Ā {}Ā for everything else (boolean, number, JavaScript expression, etc.).
So props allow you to configure your components. They respond to the React logic, according to which data descends through your tree of components in a one-way data flow.Ā Ā

Did we mention parent and child components in the first part?
As you probably suspected, a component is the child of the component in which it was defined in the Ā return()Ā .Ā
In our example, Ā CareScaleĀ Ā is the child, and Ā ShoppingListĀ is the parent.Ā
For props, there are two rules you have to follow:
A parent component always passesĀ a prop to its child: this is the only normal route of transmission.
A prop is read-only in the component that receives it.
Some React props behave in a slightly peculiar manner. These are calledĀ technical props.Ā
The Ā childrenĀ prop has slightly unusual syntax, as it is not provided using an attribute but rather by nesting components within the relevant component.Ā
So you have:
<Parent>
<Child1 />
<Child2 />
</Parent>For example, if youĀ use Ā childrenĀ Ā to rewrite the Ā BannerĀ in Ā App.jsĀ , you get:
<Banner>
<img src={logo} alt='Jungle House' />
<h1 className='jh-title'>Jungle House</h1>
</Banner>Here, Ā ImgĀ and Ā h1Ā are the child nodes in the Ā BannerĀ DOM.Ā
You can access these Ā BannerĀ child nodes in the parameters, similar to the way you get props:Ā
function Banner({ children }) {
return <div className='jh-banner'>{children}</div>
}This way of using Ā childrenĀ is particularly useful when a component does not know its children in advance, e.g., on a sidebar or a modal.Ā
To help with this, React has created PropTypes, which you can use to specify the type of prop from the very start, if required, and assign it a default value. We wonāt study these in this course, so take a moment to look at them.Ā
Itās time to use what youāve learned to improve our plant shop site. š± You can find the code for starting this exercise on branch P2C4-Begin.Ā
Create a PlantItem component, which will be a child component ofĀ ShoppingList.
PlantItem takes the props:
nameĀ for the plant name.
coverĀ for the corresponding image.
idĀ , which corresponds to the item for each element.
lightĀ .
waterĀ .Ā
The Ā nameĀ and Ā coverĀ will be used directly in PlantItem.
lightĀ and Ā waterĀ , which we have already created together, will be passed to the Ā CareScaleĀ component in Ā PlantItemĀ .Ā
In Ā styles/PlantItem.cssĀ , youāll find the CSS classes Ā jh-plant-item-coverĀ and Ā jh-plant-itemĀ , which will help you create good-looking components. Also, replace our emojis with the icons Ā sunĀ and Ā waterĀ , which you can find in Ā /assetsĀ .
Ā Youāll find the solution on branch P2C4-Solution.
Props are objects thatĀ you can get from the parameters of your function component.Ā
There are two syntaxes for assigning a value to a prop:Ā
Quotes for Ā stringĀ .
Curly braces for everything else (names, JavaScript expressions, booleans, etc.).
Destructuring is a syntax that you can use to declare a variable by giving it the value of an object (or array).
A prop is:Ā
Always passed from parent to child component.Ā
Read-only in the component that receives it.Ā
The Ā childrenĀ prop is entered by nesting children in the parent: <Parent><Child/></Parent>.
childrenĀ is useful when a component doesnāt know its children in advance.Ā
Our plant site is starting to take shape!Ā But donāt you think itās all a bit static? There isnāt much opportunity for interaction, is there? No problem, because thatās what weāll tackle in the next chapter. See you there!