- Review the benefits of React over Vanilla JS
- Review the difference between HTML and JSX
- Review the importance of Components
- Review how a component is written
- Explain what props are and how to create them
- Recognize destructured props and how to work with them
- Recognize best practices when writing components and props
- Observe how to render multiple components from a list
-
Returns the
Header
,ProjectForm
andProjectList
components -
Provides the array of
projects
toProjectList
as props
- Renders the text
Header
- Renders the text
ProjectForm
-
Accepts the props provided to the component
-
Destructures
projects
from the props object -
Maps over the
projects
array to render theProjectListItem
component for each element:-
Provide a key prop set to the
project id
value -
Provide each
project
object as a prop namedproject
-
-
Accepts the props argument
-
Destructure the props object and return
project
-
Destructure the properties of the
project
object -
Create an
img
element nested inside afigure
element -
Set the
img
propertysrc
to theimg
variable created when destructuring -
Set the
img
propertyalt
toproject: ${name}
-
Create an
article
element withh4
andp
tags nested inside-
Dynamically render each project name inside the
h4
tag -
Dynamically render each project's about data inside the
p
tag
-
create-react-app
will build a project folder for our application and install all the dependencies we need (via Node Package Manager).
To create a new React application and start it, run:
npx create-react-app app-name
cd app-name
npm start
In addition to React, it gives us:
- Webpack: a tool for bundling and minifying our code (along with a server for running our code in development)
- Babel: a transpiler that lets us write modern JavaScript and JSX
Components are the building blocks of React. A component is a function that:
- Takes in some raw data (props)
- Returns user interface (JSX)
There are some things you'll need to keep in mind:
- The name of your component function must be capitalized. This will not work
- A component can only return one element. That element can have children, like this:
function Card() {
return (
<div id="card1" className="card">
<h1>hi</h1>
<p>wassup?</p>
</div>
);
}
When you create components, one way to make them dynamic and reusable is by passing in props. For example, if we wanted to create several cards on our page using a Card component, we could do so like this:
function Card(props) {
return (
<div id="card1" className="card">
<h1>{props.greeting}</h1>
<p>{props.subGreeting}</p>
</div>
);
}
ReactDOM.render(
<div>
<Card greeting="hi" subGreeting="hello" />
<Card greeting="sup" subGreeting="what's up" />
</div>,
document.getElementById("root")
);
The props argument in our Card component defines an object that React will pass to our function when it is called, and it will use whatever attributes we add to our JSX component as key-value pairs on that props object.