When you are building your apps, thinking in terms of props, state, components, JSX tags, and other React-isms may be the last thing on your mind. Most of the time, you are dealing with data in the form of JSON objects, arrays, and other data structures that have no knowledge (nor interest) in React or anything visual. Bridging the gulf between your data and what you eventually see can be frustrating! Not to worry, though. This article will help reduce some of those frustrating moments by running through some common scenarios you'll encounter!
Onwards!
OMG! A React Book Written by Kirupa?!!
To kick your React skills up a few notches, everything you see here and more (with all its casual clarity!) is available in both paperback and digital editions.
BUY ON AMAZONThe Example
To help make sense of everything you are about to see, we are going to need an example. It's nothing too complicated. Create a new Vite project (if you don't have one, follow along with Easily Set Up Your React Dev Environment), open the src/index.css file, and delete everything inside it so the starter styles don't get in our way. Then, replace the contents of src/App.jsx with the following:
function Circle({ bgColor }) {
const circleStyle = {
padding: 10,
margin: 20,
display: "inline-block",
backgroundColor: bgColor,
borderRadius: "50%",
width: 100,
height: 100,
};
return <div style={circleStyle}></div>;
}
function App() {
return (
<div>
<Circle bgColor="#F9C240" />
</div>
);
}
export default App;
Once you have saved your file, preview what you have in your browser. If everything went well, you will be greeted by a happy yellow circle:

If you see what I see, great! Now, let's take a moment to understand what our example is doing. The bulk of what you see comes from the Circle component. It is mostly made up of our circleStyle object that contains the inline style properties that turn our boring div into an awesome circle. All the style values are hard-coded except for the backgroundColor property. It takes its value from the bgColor prop that gets passed in.
Going beyond our component, the App component is where we use it. We have a single instance of our Circle component declared, and we declare it with the bgColor prop set to the color we want our circle to appear in. Having our Circle be written out by hand like this is a bit limiting, especially if you are going to be dealing with data that could affect how many circles we need and what they look like. In the next couple of sections, we'll look at the ways we have for solving that.
Your JSX Can Be Anywhere, Part II
In the Meet JSX...Again tutorial, we learned that our JSX can actually live outside of a return statement and can be used as a value assigned to a variable. For example, we can fearlessly do something like this:
function App() {
const theCircle = <Circle bgColor="#F9C240" />;
return (
<div>
{theCircle}
</div>
);
}
The theCircle variable stores the JSX for instantiating our Circle component. Evaluating this variable inside our returned JSX results in a circle getting displayed. The end result is no different than what we had earlier, but having our Circle component instantiation freed from the shackles of the return statement gives you more options to do crazy and cool things.
For example, you can go further and create a function that returns a Circle component:
function showCircle() {
const colors = ["#393E41", "#E94F37", "#1C89BF", "#A1D363"];
const ran = Math.floor(Math.random() * colors.length);
// return a Circle with a randomly chosen color
return <Circle bgColor={colors[ran]} />;
}
In this case, the showCircle function returns a Circle component (boring!) with the value for the bgColor prop set to a random color value (awesomesauce!). To have our example use the showCircle function, all you have to do is call it inside our JSX:
function App() {
return (
<div>
{showCircle()}
{showCircle()}
{showCircle()}
</div>
);
}
As long as the expression you are evaluating returns something React can display, you can put pretty much anything you want inside the { and } brackets. That flexibility is really nice, because there is a lot of things you can do when your JavaScript lives outside of the JSX. A LOT OF THINGS!
Dealing with Arrays
Now we are going to get to some fun stuff! When you are displaying multiple components, you won't always be able to manually specify them like we did with our three showCircle() calls. In many real-world scenarios, the number of components you display will be related to the number of items in an array you are working with. For example, let's say that we have an array called colors that looks as follows:
const colors = [
"#393E41", "#E94F37", "#1C89BF", "#A1D363",
"#85FFC7", "#297373", "#FF8552", "#A40E4C",
];
What we want to do is create a Circle component for each item in this array (and set the bgColor prop to the value of each array item). One way to do this is by creating an array of Circle components using a plain old for loop:
const colors = [
"#393E41", "#E94F37", "#1C89BF", "#A1D363",
"#85FFC7", "#297373", "#FF8552", "#A40E4C",
];
function App() {
const renderData = [];
for (let i = 0; i < colors.length; i++) {
renderData.push(<Circle bgColor={colors[i]} />);
}
return <div>{renderData}</div>;
}
In this snippet, we populate our renderData array with Circle components just like we originally set out to do. To display all of these components, React makes it very simple. We just specify our renderData array as an expression that we need to evaluate. We don't need to take any other step to go from an array of components to seeing something that looks like the following when you preview in your browser:

Ok, I lied. There is actually one more thing we need to do, and this is a subtle one. The way React makes UI updates really fast is by having a good idea of what exactly is going on in your DOM. It does this in several ways, but one really noticeable way is by internally keeping track of each item in a list.
When you create elements dynamically (such as what we are doing with our array of Circle components), React needs a way to tell the items apart. We need to do some extra work. That extra work takes the form of a key prop whose value React uses to uniquely identify each particular component.
Check Your Console Yo!
React is really good at telling you when you might be doing something wrong. If you run the code from earlier, where we didn't specify a key prop, you will be greeted with the following error in your browser's console:
Error: Each child in a list should have a unique "key" prop. Check the render method of `App`. See https://react.dev/link/warning-keys for more information.
When you are working with React, it is a good idea to periodically check your console for any messages it may have. Even if things seem to be working just fine, you'll never know what you might find :P
For our example, the color values in our array are all different, so we can use the color itself as the key:
for (let i = 0; i < colors.length; i++) {
const color = colors[i];
renderData.push(<Circle key={color} bgColor={color} />);
}
That makes our warning go away, and it works just fine. But we can do better.
Meet map
Creating an empty array, looping, and pushing items into it is a lot of ceremony for something we do all the time. JavaScript arrays have a method called map that does exactly this job. It goes through every item in an array, calls a function on each one, and gives you back a brand new array with whatever that function returned. When the function returns JSX, the result is an array of components, which is something React already knows how to display. That lets us skip the loop and the temporary array entirely and do all of this right where we need it:
function App() {
return (
<div>
{colors.map((color) => (
<Circle key={color} bgColor={color} />
))}
</div>
);
}
Our colors.map call takes each color, creates a Circle with that color as its bgColor and key, and returns the whole collection. The output is identical to the picture we saw earlier, but the code is shorter and reads like what it is doing: for each color, give me a circle. You will see this pattern in almost every React app you ever read.
Arrays of Objects and Keys
Real-world data is rarely a plain list of strings. It is usually an array of objects, where each object has an identifier of some sort that came from a database or an API. Let's change our data to look more like that:
const circles = [
{ id: 1, color: "#393E41" },
{ id: 2, color: "#E94F37" },
{ id: 3, color: "#1C89BF" },
{ id: 4, color: "#A1D363" },
];
function App() {
return (
<div>
{circles.map((circle) => (
<Circle key={circle.id} bgColor={circle.color} />
))}
</div>
);
}
This is the shape of data you'll see the most, and the id is the perfect candidate for a key. It's unique, and it stays attached to its item no matter where that item moves in the list. A few rules about keys are worth remembering:
- Keys only need to be unique among siblings. The same key can appear in a different list somewhere else in your app.
- Keys go on the outermost element returned from map. It's the element React sees directly in the array.
- A key isn't a prop. Your Circle component can't read props.key. It is something React uses for itself. If your component needs the value, pass it again under a different name.
- Don't generate keys on the fly. Something like key={Math.random()} gives every item a brand new identity on each render, so React throws away and re-creates all of them each time.
What if your data doesn't have a unique identifier? You'll be tempted to use the array index, as in map((item, index) => <Circle key={index} ... />). That is fine for a list that never changes order and never has items added or removed from the middle. The moment it does, an index stops describing which item you are looking at (the circle at index 2 is now a different circle), and React can end up reusing the wrong element or the wrong state for it. If you can, give your items an id when you create them.
Filtering and Empty Lists
Because map and filter both return arrays, you can chain them. Let's say that each object in our data has a visible property, and we only want to show the circles that are visible:
const circles = [
{ id: 1, color: "#393E41", visible: true },
{ id: 2, color: "#E94F37", visible: false },
{ id: 3, color: "#1C89BF", visible: true },
{ id: 4, color: "#A1D363", visible: true },
{ id: 5, color: "#85FFC7", visible: false },
{ id: 6, color: "#297373", visible: true },
];
Our App component can filter first and then map, and it can also handle the case where nothing is left to show:
function App() {
const visibleCircles = circles.filter((circle) => circle.visible);
return (
<div>
{visibleCircles.length === 0 ? (
<p>No circles to show.</p>
) : (
visibleCircles.map((circle) => (
<Circle key={circle.id} bgColor={circle.color} />
))
)}
</div>
);
}
When you run this, you'll see four circles because the two items with visible set to false are skipped. If every item were hidden, we would get our friendly message instead of a blank space.
You will often see the shorter && form for conditionally showing something, like {isOpen && <Menu />}. It works great with true/false values, but there's a trap if you use it with a number:
const count = 0;
// shows a stray 0 on the page
{count && <p>You have {count} circles</p>}
// shows nothing when count is 0
{count > 0 && <p>You have {count} circles</p>}
React doesn't render true, false, null, or undefined. It does render the number 0. Since 0 && anything evaluates to 0, you end up with a lonely zero showing up on your page. Turning the condition into a real boolean, like count > 0, avoids the problem.
When Each Item Needs More Than One Element
Sometimes each item in your list needs to produce more than one sibling element without a wrapper around them. For example, a circle followed by a label. The short fragment syntax (<> and </>) can't accept a key, so you need to use the full Fragment component, which you import from React. (This assumes each of our objects also has a name property.)
import { Fragment } from "react";
{circles.map((circle) => (
<Fragment key={circle.id}>
<Circle bgColor={circle.color} />
<span>{circle.name}</span>
</Fragment>
))}
The Fragment gets the key, and it doesn't add anything extra to the DOM. Our circle and its label end up as siblings, just like we wanted.
Conclusion
All the tips and tricks you've seen in this article are made possible because of one thing: JSX is JavaScript. This is what allows you to have your JSX live wherever JavaScript thrives. To us, it looks like we are doing something absolutely bizarre when we specify something like this:
for (let i = 0; i < colors.length; i++) {
const color = colors[i];
renderData.push(<Circle key={color} bgColor={color} />);
}
Even though we are pushing pieces of JSX to an array, just like magic, everything works in the end when renderData is evaluated. I hate to sound like a broken record, but this is because what ends up in your browser is the result of a tool turning each piece of JSX into a plain function call. With React's modern JSX transform, the code above becomes something like this:
import { jsx } from "react/jsx-runtime";
for (let i = 0; i < colors.length; i++) {
const color = colors[i];
renderData.push(jsx(Circle, { bgColor: color }, color));
}
Notice how the key gets its own spot as the third argument to jsx. (Older code and tutorials will show this as React.createElement(Circle, { key: color, bgColor: color }). Same idea, different function.) When our JSX gets converted into pure JS, everything makes sense again. This is what allows us to get away with putting our JSX in all sorts of uncomfortable (yet photogenic!) situations with our data and still get the end result we want! Because, in the end, it's all just JavaScript.
Turning data into UI is half of the story. The other half is deciding what to show and what to hide depending on the situation, which is what we look at next.
Next tutorial: Conditional Rendering in React
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

