Sitemap
Dan Buda

Dan Buda

Web developer, real estate investor and recovering political junkie in Austin, TX

React Props & Mapping

3 min readFeb 22, 2017

I like React because of how easy I find it to work with components. And when I’m working on a project that uses clickable thumbnails to send a user to a more detailed page, I really like React props.

Props are basically attributes that can be added to a component inside of the render function of a parent component. They can be pretty much anything. And for projects like I mentioned above, props are made even more powerful when combined with the map() method as I’ll show in the example below.

First things first, let’s set up 2 files. We’ll call them Parent.js and Child.js to make it easy.

Parent.js is going to be our main component, and will show a list of names. Child.js will be the component that displays those names.

So for Child.js I’m just going to set up a basic React component with empty <p> tags that will display first & last names when it’s complete:

Press enter or click to view image in full size

And Parent.js also starts out very simple. I’ve added several ChildComponents inside just as a placeholder (and don’t forget to import ChildComponent!).

Press enter or click to view image in full size

But since we want to access our names and display them with ChildComponent, we have to give that information to ChildComponent. And here’s where map() and props come in, as well as a little bit of the use of state:

Press enter or click to view image in full size

At the top, I’ve imported Names from a file Names.js in the same directory as ParentComponent. For this example, I’m just assuming the names data is stored in a file that looks something like this:

Press enter or click to view image in full size

And I’ve added a getInitialState function that sets the key “names” equal to our imported Names array. Also, I’ve removed the <ChildComponent />s from the code, since I’m using map() to create those components. You’ll notice that the mapping is done OUTSIDE of the render function. That is very important.

Get Dan Buda’s stories in your inbox

Join Medium for free to get updates from this writer.

The key/value pair that is put into each ChildComponent within the map function becomes the props for each ChildComponent.

One last thing for our ParentComponent. We need to return something, since now we just have empty <div> tags. We add our mapped array that we called “names” inside:

Press enter or click to view image in full size

So now we can go over to our ChildComponent file and finish everything up. Quite simply, we can now just use the props passed into each ChildComponent during the mapping, and our first & last names will display:

Press enter or click to view image in full size

And that’s about it! (Unless I completely forgot something, that is.) For a working CodePen example, click here.

--

--

Dan Buda
Dan Buda

Written by Dan Buda

Web developer, real estate investor and recovering political junkie in Austin, TX