Working with software program implies that you’ll have to take care of information. You’ll take some enter information, do some processing, after which optionally output the ultimate outcome.
In React, we primarily use props
to go information to our parts. The phrase props
is mainly a shorter time period used for properties. You’ve got seen them in motion in previous couple of tutorials the place we created parts to show details about international locations.
On this tutorial, we are going to deal with studying about props
in additional element. This tutorial will merely cowl the fundamentals after which we are going to transfer to extra superior matters later within the sequence.
Props are Like Attributes
The best option to perceive props
is to think about them just like the attributes you could go to HTML components throughout internet improvement. Nonetheless, props
are way more superior.
Any prop
may have two components. The primary is the prop title which is solely the attribute title. The second is the prop worth which is the worth of that attribute. You possibly can assign as many props
to your part as you want.
There are two guidelines that it’s important to comply with when assigning a reputation to completely different props.
- The prop title can’t be a reserved key phrase from JavaScript. It’s because the JSX we write will in the end be transformed to JavaScript and utilizing reserved key phrases will mess issues up. This is the reason we use
className
as a substitute ofclass
andhtmlFor
as a substitute offor
as prop title. - The prop names needs to be in camelCase.
As I stated earlier, you’ll be able to go as many props to a part as you want. Nonetheless, a part shouldn’t be required to make use of all of the props.
You possibly can consider passing props to parts as passing parameters to capabilities. Parts are like capabilities in that sense and similar to you’ll be able to go any form of worth as parameter to capabilities, you’ll be able to go any form of worth as a prop.
/* let countryElement = ( <Nation title="United States" capital="Washington, D.C." inhabitants="332 million" /> ); */ let countryElement = Nation({title: "United States", capital: "Washington D.C.", inhabitants: "332 million"}); let rootElement = doc.getElementById("root"); ReactDOM.createRoot(rootElement).render(countryElement);
Within the above instance, we changed the JSX for our Nation
part with a operate name to Nation
and saved the outcome inside countryElement
. Nonetheless, rendering out countryElement
ultimately gave us the identical outcome.
Props Should be Learn-Solely
A part shouldn’t be supposed to change the worth of its props. The capabilities or lessons that we outline to create our part should hold the props as read-only. This habits is enforced once you create a React app by working the command:
npx create-react-app your-app
In such instances, the next code provides you with an error about “title” being read-only.
operate Nation(props) { props.title = "Australia"; return ( <div className="container"> <h2 className="country-name">Nation Identify: {props.title}</h2> <p className="inhabitants">Inhabitants: {props.inhabitants}</p> <p className="space">Space: {props.space} km<sup>2</sup></p> </div> ); } export default Nation;
The rationale props have to be immutable is as a result of parts are supposed to make use of them to get info from their mother and father. In the event you truly need to modify some info in a part, utilizing state is your finest wager. State is mainly information that’s maintained inside the part and React will robotically replace the DOM based mostly on any modifications in state. We’ll be taught extra about that later within the sequence.
Needless to say information in React flows from guardian to baby and so forth additional down the record.
Last Ideas
I hope you now have a primary understanding of props in React. In later tutorials, we are going to talk about learn how to validate props or present default values for them.