Repository navigation
3 props
Components are the building blocks of React apps, and they come in a few different flavours.
Let's change the app around to demostrate some of the modularity that components give us, and introduce techniques of "passing" data into components via "props".
Create a new file in the src folder of the app project, call it Name.js open it in your editor, copy the code below into the file and save it.
-
The first line of this code imports the React library, giving us access to all the React magic things, like JSX.
-
The component is created using modern "fat arrow" style notation. A constant variable "Name" is created, containing a function that does just one thing, returning some JSX (a paragraph tag wrapping the word "me") to whatever it is that uses the component.
-
Finally, export enables the component to be import by other components.
import React from 'react'
const Name = () => {
return <p>me</p>
}
export default NameEdit App.js and add an instruction to the top section of the file to import our new Name component. Notice that we import the Name component using a relative path from the current file. Use ./ as path for the current directory.
This code snippet is just showing the top part of the file, and I've deleted the ; semicolons because they are optional in Javascript now, and I think its cleaner and quieter without them.
import React from 'react'
import Name from './Name'
import './App.css'
...Now the Name component is imported, it can be used in the App component.
-
Delete the person object that we created in workshop 2.
-
Delete the
<h1>tag that was showing the person properties from inside the<header>tag. -
Add the Name component as a "self-closing"
<Name />tag inside the<header>tag.
Here's the full code.
import React from 'react'
import Name from './Name'
import './App.css'
function App() {
return (
<div className="App">
<header className="App-header">
<Name />
</header>
</div>
)
}
export default AppSave it, and look at the results in the browser. Your app should now have the word "me" showing. To recap, the App component uses the Name component, which has the "me" word in it.
Let's make the Name component more practical by specifying the person's name when we use it, rather than hard-coding "me" into the component. To do this, we will start using properties, or props.
Change the App.js use of the <Name /> tag to include a value that will be passed into the Name componenet.
The lines beginning with
//are comments, descriptions of what's happening for our benefit, they don't affect the code.
// change
<Name />
// to
<Name firstName="ben" />Now we can change the Name.js component to use the passed in prop value rather than the static "me".
-
Give the function access to the passed in props by setting
propsas an input parameter. By the way, when fat arrow functions have only one parameter, the round brackets are optional. -
Replace "me" with
props.firstName. This is getting us the firstName property of the props object. -
We also need to wrap
props.firstNamein curly brackets, to let React know that we want it to be evaluated as an expression, not a string. (Try it without the curlies and see what the difference is.)
Before:
import React from 'react'
const Name = () => {
return <p>me</p>
}
export default NameAfter:
import React from 'react'
const Name = props => {
return <p>{props.firstName}</p>
}
export default NameNow check that everything is saved, and view the result in the browser. Whatever you set the firtName prop to should have passed into the Name component, and returned to be displayed.
Try adding another Name element to the App component with a different prop value, to see how the passed in value is used.
import React from 'react'
import Name from './Name'
import './App.css'
function App() {
return (
<div className="App">
<header className="App-header">
<Name firstName="baby" />
<Name firstName="shark" />
</header>
</div>
)
}
export default AppSo far we have been using "Functional" components. In the next workshop, we'll use Class components.