Populating Data Dynamically
Populating data dynamically means building a component's output from values held in JavaScript, instead of typing fixed text into the JSX. The data might be a variable, an object, an array returned from an API, or props passed down by a parent. Any time the same layout repeats with different content, such as product cards, table rows or menu links, you describe the layout once and let the data fill it in.
Curly braces open a window into JavaScript
Inside JSX, anything between { and } is evaluated as a JavaScript expression and its result is placed in the output. That can be a variable, a property such as product.price, a calculation, or a function call. Braces also work in attribute values, for example src={product.image}.
const products = [
{ id: 'p1', name: 'Wireless Mouse', price: 799, inStock: true },
{ id: 'p2', name: 'USB-C Hub', price: 1499, inStock: false },
{ id: 'p3', name: 'Laptop Stand', price: 1199, inStock: true }
];
function ProductList() {
return (
<ul className="product-list">
{products.map(product => (
<li key={product.id}>
<h3>{product.name}</h3>
<p>Rs. {product.price.toLocaleString('en-IN')}</p>
{!product.inStock && <span className="badge">Out of stock</span>}
</li>
))}
</ul>
);
}
Turning an array into elements with map
The array method map() takes each item and returns something new. Here, each product object becomes a <li> element, and React renders the resulting array of elements in order. Adding a fourth product to the array automatically adds a fourth list item, with no change to the markup.
Each element produced in a list needs a key prop that is unique among its siblings and stays the same for that item between renders. A database ID is ideal. React uses keys to work out which items were added, removed or reordered.
Keeping data separate from markup
Holding the data in its own array or file brings a few benefits. The component becomes easier to read, because it describes structure rather than content. You can later swap the hard-coded array for data from an API without touching the layout. And you can pass the data in as a prop, which makes the same component reusable on different pages.
Mistakes that break dynamic rendering
Trying to render an object directly, such as {product}, throws the error "Objects are not valid as a React child". React can render strings, numbers, elements and arrays of those, but not plain objects. Pick the property you want, such as {product.name}.
Another trap is using forEach() instead of map(). forEach() returns undefined, so nothing appears. Also watch out when writing an arrow function with curly braces: item => { <li>...</li> } has no return value. Use parentheses for an implicit return, or add return.
Finally, be careful with && when the left side is a number. {cart.length && <Badge />} renders a literal 0 when the cart is empty. Write {cart.length > 0 && <Badge />} instead.
Formatting belongs close to the data too. Converting a number into a currency string or a date into readable text inside the JSX, as toLocaleString does above, keeps the stored data raw and easy to sort or calculate with.
Takeaways
- Braces embed any JavaScript expression in JSX.
- Use
map()to turn arrays into lists of elements, each with a stablekey. - Render individual properties, never whole objects.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.