Professional Documents
Culture Documents
React - Passing Data Via Props
React - Passing Data Via Props
React - Passing Data Via Props
Planning …………………………………………………………………………………………………………………….. 4
Conclusion ……………………………………………………………………………………………………………………... 22
INTRO TO PROPS
Before doing any coding, we need to evaluate what our project is asking from us
and map it out.
The parents CH CH CH
<CHILD DETAILS>
Each Child Detail card will
ONE TWO THREE display different aspects of the
child.
🏡🏡 🏡
🏡
📫
📫 📫 📫
📫
🏡
Components are like the blueprints for these houses. Instead of
creating a new blueprint for each house (when they all look the same),
they create one and use it over and over again.
Props are the items unique to each house. They have a key:value pair.
Instead of that information being hard-coded in (predetermined), the
builder will pass along the component (house) along with the props to
the new owners and allow them to fill out that information unique to
their needs/wants.
address: ‘ ‘ ,
doorColor: ‘ ‘ ,
familySize: 0,
dog: false
EXPORTING
function NameTag (props) { // NameTags will take in the individual props added by each person
return (
<div>
<h2>Name: {props.name}</h2>
<h3>Age: {props.age}</h3>
<h3>Job: {props.job}</h3>
</div>
)
}
export default NameTag
The NameTag component is then imported where it needs to be used and can
be reused over and over again as it is rather than recreating it each time. We can
simply input the values we want for the props.
IMPORTING NAME TAG
function NameTagCollector () {
<NameTag name = “Katrina”, age = 32, job = “SL”/>
<NameTag name = “Mel”, age = 25, job = “SL”/>
<NameTag name = “Taja”, age = 24, job = “TL”/>
}
Here, this information is hardcoded. (Like writing it on a name tag and slapping it
on your shirt.)
If there is data set up, when the <NameTag /> is added to the
<NameTagCollector/> and the <NameTagCollector /> is added to the <App />,
then the information from data will be pulled in and rendered.
<App />
Data is housed
<NameTagCollector />
and passed down.
<NameTag />
THREE-CAR PILE UP PROJECT
You will see me relate a lot of things to mass traumas, car accidents, EMS
calls, etc. That comes from a lifetime in the medical field and has helped
me learn many of the webdev concepts we study.
For passing props, you’ll learn about a three-car pile up and how EMS
would store and share the information from the call. Be sure to check out
the Three-Car Pile Up project in code-pen to see the full breakdown of
the components.
Component layout:
1. Index.js = renders <DailyReport /> (aka <App />)
2. <DailyReport />
a. Main Component
b. Houses <CallLog/>
3. <CallLog />
a. Parent component
b. Houses data and maps through it to display:
c. <PatientCard />
i. Receives props from <CallLog />
4. <PatientCard />
a. Displays several components that make up the <PatientCard/>
i. <PtBio /> (patient info)
ii. <Treatment /> (what was done)
iii. <PainScalePoints /> (has buttons to + /- pain)
iv. <ResourceList /> (who showed up)
b. Passes props given by <CallLog />
DATA FLOW FOR THREE-CAR PILE UP
In this case, we are going to rename the <App/> to be <DailyReport />. It is the
main component that will render everything.
In EMS, there is a Daily Report that is given. That <DailyReport /> component (or
App) holds all the calls of that day- CallLog. The <CallLog /> component holds all
the data. Since the CallLog is housed in the DailyReport (App), the data can be
accessed by all the other components through props.
You have to dig a little deeper in the CallLog to get to the information about each
call. But on the Daily Report (App), an EMS provider could list many CallLog
components
��
<DailyReport>
<CallLog>
<PtCard/>
<PtCard/>
</CallLog>
</DailyReport>
PARENT COMPONENT BREAKDOWN
If you take a look at <CallLog /> being housed in the <DailyReport(App) />, there is no mention
of data or props. Just the component.
If you want to see what happened on the CallLog, you have to go TO the CallLog
<CallLog /> is also a component. (It is the parent component that will be passing the
information down.)
At the beginning of every shift, The DailyReport is empty and the medic has an empty CallLog
waiting to have values added to it. The CallLog has many properties(props) but no hardcoded
value. It’s a standardized form of information that is used for every patient. With each new
patient, new values are imputed.
Because the CallLog is a component, it only has to be created once and can be reused as
many times as needed without having to recreate it.
(Refer back to the IMG-1 if you are still having a hard time understanding components
and props)
Call Log
The CallLog brings in the data from the call(patientData.) Rather than add data to every
component that will need it, we house it in a parent component(CallLog), render it in <App />
(DailyReport) and then every component has access to it.
From there, we pass all the properties down to each component that needs them.
As you see, there is a lot happening in this tiny little component. But that’s how it
goes even on an emergency call. Someone calls 911 and even though the event is
contained to one intersection or one house in a large area, there are a lot of people,
resources, and moving parts involved.
Now the data has been pulled in. A new PatientCard is waiting to be created for
each new patient.
😷 Each PatientCard will hold a lot of properties (props) waiting for values to be added. Each
PatientCard will look the same. Only the values will change.
Before we can create any of the components included on the PatientCard, we have to pay
attention to a few aspects. Particularly this line:
Component name
In this example above, we have destructured propsFromPtCard. This means, each time we
use propsFromPtCard, we won’t have to add props. in front of it. It’s a small change but it
saves us a few key-strokes.
While that isn’t a big change, writing it with (props) means we have to add props. Everytime we
are pulling down props. (props.propsFromPtCard.whatever)
Next, let’s look at the components listed that will be added/created. We aren’t changing the data
that is accessible on the parent component - CallLog. That is the information we are given and
will remain as it is.
But, the PainScale component has the ability to update the patient’s pain level.
Now all we have to do is set up the components we want to add to the <PatientCard />
component.
We will:
● add them in the order we want them to appear,
● pull down props, and
● get the appropriate date using dot notation.
(ex)
🚺🚹 PtBio (name/age/gender)
But remember, we will be updating the patient’s pain level after we’ve administered treatment. It
will either remain the same, increase, or decrease. (Hopefully it doesn’t increase but it could. 😉)
When we add the <PainScalePoints> component, we will pull in these functions as well as the
patient’s pain level recorded in data. It will look like this:
<PainScalePoints
incrementPain = {incrementPain}
decrementPain = {decrementPain}
Pain = {pain}
/>
PASSING PROPS TO CHILD
COMPONENT
The only thing left to do is export the PatientCard (since we’ve already imported it to the
CallLog.) Otherwise, we will get errors.
When a medic fills out a PatientCard, she will go down the list to make sure she hasn’t
missed inputting any of the values(props) or points on the list.
With <PtBio /> component, pull in props and then set up the component how you want it
presented (presentational component.)
MAPPING OVER ARRAYS
<Treatment /> and <ResourceList /> are different from the <PtBio /> component.
You’ll notice that complaint, treatments, and resources all contain arrays.
We will have to address these differently than we did the PtBio where we were simply
addressing individual items.
We will be putting the complaint array on the <Treatments /> component rather than it’s
own component since these two depend on each other. This is strictly a personal choice.
Complaints could have had its own card but it didn’t seem necessary.
We will map over each of these and return a new array with each item.
The difference is, complaint is returning an array of items, not objects. So
presentationally, it will look differently.
Nothing fancy. We map over the complaint array, stopping at each complaint ( c ) along
the way, creating an index and returning that complaint ( c ) with a key index and <h3>
tag.
But, the treatments array returns an array of objects which means we need to list those
key:value pairs and use dot notation to pull in the values of each.
The ResourceList is also an array of objects that when mapped over will return a <ResourceCard
/> that includes all the key:value pairs of the object.
Finishing up the components is the ResourceCard. The props are passed in there as well, either
by adding (props) as the parameter or destructuring the items that will be using props:
rsType
Type
Unit
Chief
Now our DailyReport(<App />) is complete (aside from styling. We’ll cover
that in a later lecture/workshop..)
But, there are 7 components in this App that can be reused over and over
again. If the Emergency Room staff wanted to incorporate a DailyReport
into their files, they wouldn’t need to recreate these components, they
could simply borrow ours.
Be sure to fork the repo. Play in it. Break it (that’s the best way to learn.)
Add some more components if you want. Have fun.