Embarking on a Galactic Journey: Introduction to Props, Component Nesting
Introduction to Props in Functional Components
Pack your bags! We're immersing ourselves into the world of React, exploring props in functional components. The term props is short for properties, and we use them to pass data between components.
Unlike variable data, props data is read-only. This means that a child component can only read the props sent from a parent component but can't change them. To better understand, let's consider props as 'instructions' that a parent passes down to a child.
To illustrate, let's review this code snippet:
In the line of code {props.name}, we are reading the name prop within our Welcome component.
Nesting Components into HTML Elements
Those who are well-versed in LEGO building can relate to the concept of nested components. Within React, components can be placed within other components. This concept of modularity allows us to reuse and combine components.
An earlier Welcome component nested within an HTML <h1> tag bears witness to this concept:
In this scenario, the App function is the main component, and the Welcome component is a child that is nested within it.
Passing Primitive Data Types as Props
Jumping ahead in our journey, let's discuss how to pass primitive data types (strings, numbers, booleans) as props. Though we can pass other types (objects, functions) as props, for now, we're focusing on primitive data types.
Let's revisit our Welcome component to pass a string prop:
And, for numbers and booleans, we use braces rather than quotes:
Default Props in Functional Components
React components can have default props. These are the values that props fall back on when they are not supplied by the parent. Here's a modified Welcome component with a default name:
Now, rendering <Welcome /> without providing a name prop results in "Hello, Galactic Traveler".
