Use this repo to get some practice making small, composable components.
-
Replace the hard-coded
<h1>inApp.jswith a functional React component (Header.js). -
Then, set a prop called
nameon yourHeadercomponent (set the prop inApp.js).Don't worry, we'll go over
propsin class. Be sure to check out the videos if you need a refresher!
- There are 2 steps involved for utilizing
props- setting a prop and getting / calling a prop:- Setting
props- declaring your data. Syntax is like an HTML attribute:
// src/components/App.js <App> <MyComponent myProp="howdy" /> </App>
- Getting
props- calling and rendering your data. Syntax is like a JavaScript object:
// src/components/MyComponent.js <div> <h2>{props.myProp}</h2> </div>
- Setting
-
Practice cranking out simple, composable Components by creating a list of User Profiles for rendering the data in
users.json. The structure should be something like this:ListOfUsers.jsUserProfile.jsUserAddress.jsUserAvatar.js
-
Use
propsto pass theusersdata fromindex.jsallllll the way down to your components.
importthe data intoindex.jsand set it as a prop on yourAppcomponent (in therendermethod)- Work in small pieces, one step at a time - and commit your work every time you finish a feature.
- Start with the
UserProfile.- Don't try to do it all at once. Just write the component with statically defined data, and get it rendering:
function UserProfile (props) {
return (
<div>
<h1>User Name</h1>
<a href="mailto:user@hotmail.com">User Email</a>
/* etc */
</div>
);
}- Now,
importthe data fromsampleUser.jsontoindex.jsand pass it down aspropsto yourUserProfilecomponent. - Once you have data rendering via
props, you'll want to use.mapto iterate over the Array of user data. If you access user data fromprops, you'll find that there's a way to easily swap out the single user insampleUser.jsonwith multiple users inusers.json.