Pass Multiple Value Inside Component In React Using Props And Map

In this article, we are going to discuss how to call child component with looping props values,  or pass multiple values one by one inside child components in React. Here we are using Map function to loop values inside objects.
 
Let's create a new React application using npx create-react-app myreactapp. Create a folder inside src and name it propsusage. Then create a file with the name propsusage.js inside propsusage folder. Then create another file with the name loopprops.js inside propsusage folder.
 
Now open propusage.js file .
 
We will create json object.
  1. let order = [{  
  2.     customerName: 'murali',  
  3.     progress: 0  
  4. }, {  
  5.     customerName: 'krishnan',  
  6.     progress: 1  
  7. }, {  
  8.     customerName: 'harsan',  
  9.     progress: 2  
  10. }, {  
  11.     customerName: 'heman',  
  12.     progress: 3  
  13. }, ];  
Now I am going to pass this object value one by one to the child component loopprops one by one.
 
For all props key must differentiate the value . 
  1. <div>  
  2.    {order.map(value => (<LoopProps key={value.progress} customerName={value.customerName} progress={value.progress} ></LoopProps>))  
  3. }  
  4. </div >  
Now In child component's loopprops.js I am going to use all the values with a progressive bar.
  1. <p>Pizza for: {props.customerName}</p>  
  2. <div className="progress">  
  3.    <div className={`progress-bar ${props.progress === 0 ? 'bg-danger' : 'bg-info'}`} role="progressbar" style={{ width: '20%' }}>Ordered</div>  
  4.    <div className={`progress-bar ${props.progress === 1 ? 'bg-danger' : 'bg-info'}`} role="progressbar" style={{ width: '20%' }}>Prepping</div>  
  5.    <div className={`progress-bar ${props.progress === 2 ? 'bg-danger' : 'bg-info'}`} role="progressbar" style={{ width: '20%' }}>Cooking</div>  
  6.    <div className={`progress-bar ${props.progress === 3 ? 'bg-danger' : 'bg-info'}`} role="progressbar" style={{ width: '20%' }}>Quality</div>  
  7.    <div className={`progress-bar ${props.progress === 4 ? 'bg-danger' : 'bg-info'}`} role="progressbar" style={{ width: '20%' }}>Delivery</div>  
  8. </div>  
Code for propusage.js
  1. import React from 'react'  
  2. import LoopProps from './loopprops'  
  3. function propusage() {  
  4.    let order = [  
  5.       { customerName: 'murali', progress: 0 },  
  6.       { customerName: 'krishnan', progress: 1 },  
  7.       { customerName: 'harsan', progress: 2 },  
  8.       { customerName: 'heman', progress: 3 },  
  9.    ];  
  10. return (  
  11.    <div>  
  12.       {order.map(value => (<LoopProps key={value.progress}  
  13.       customerName={value.customerName} progress={value.progress}  
  14.       ></LoopProps>))  
  15.       }  
  16.    </div >  
  17. );  
  18. }  
  19.   
  20. export default propusage;  
Code for loopprops.js
  1. import React from 'react';  
  2. function loopprops(props) {  
  3.   
  4. return (<div>  
  5. <div>  
  6.    <p>Pizza for: {props.customerName}</p>  
  7.    <div className="progress">  
  8.       <div className={`progress-bar ${props.progress === 0 ? 'bg-danger' : 'bg-info'}`} role="progressbar" style={{ width: '20%' }}>Ordered</div>  
  9.       <div className={`progress-bar ${props.progress === 1 ? 'bg-danger' : 'bg-info'}`} role="progressbar" style={{ width: '20%' }}>Prepping</div>  
  10.       <div className={`progress-bar ${props.progress === 2 ? 'bg-danger' : 'bg-info'}`} role="progressbar" style={{ width: '20%' }}>Cooking</div>  
  11.       <div className={`progress-bar ${props.progress === 3 ? 'bg-danger' : 'bg-info'}`} role="progressbar" style={{ width: '20%' }}>Quality</div>  
  12.       <div className={`progress-bar ${props.progress === 4 ? 'bg-danger' : 'bg-info'}`} role="progressbar" style={{ width: '20%' }}>Delivery</div>  
  13.    </div>  
  14. </div>  
  15. </div>)  
  16. }  
  17. export default loopprops;  
In App.Js, we have to call the PropUsage component to execute our code.
  1. import React from 'react';  
  2. import './App.css';  
  3. import PropUsage from './propsusage/propsusage'  
  4. function App() {  
  5.    return (  
  6.       <div className="App">  
  7.          <PropUsage></PropUsage>  
  8.       </div>  
  9.    );  
  10. }  
  11.   
  12. export default App;  
To run the application type
 
npm start 
 
then the application will be open in http://localhost:3000 post. 
 
Now the output will be like this,
 
Pass Multiple Value Inside Component In React Using Props And Map
 
I have attached my code as well. 
 
I am going to give another example that will pass the objects directly in the child component and in that child component we are forming the table with map function.
 
Create object as given below
  1. let productLists = [{  
  2.     id: 'p01',  
  3.     name: 'name 1',  
  4.     price: 5,  
  5.     quantity: 6,  
  6.     status: true,  
  7.     photo: 'thumb1.gif'  
  8. }, {  
  9.     id: 'p02',  
  10.     name: 'name 2',  
  11.     price: 2,  
  12.     quantity: 3,  
  13.     status: false,  
  14.     photo: 'thumb2.gif'  
  15. }, {  
  16.     id: 'p03',  
  17.     name: 'name 3',  
  18.     price: 15,  
  19.     quantity: 16,  
  20.     status: true,  
  21.     photo: 'thumb3.gif'  
  22. }];  
Now i am going to create a new child component called product list. in this product list i will pass this above object and form the table.
  1. function ProductList({ productLists }) {  
  2. return (<table className="table">  
  3.    <tbody>  
  4.       {productLists.map((product, index) => {  
  5.          return <EachProduct key={index} SingleProduct={product} />  
  6.       })}  
  7.    </tbody>  
  8. </table>)  
In that above code I am calling one more child component called eachproduct that will form the rows.
  1. function EachProduct(SingleProduct) {  
  2. console.log(SingleProduct);  
  3. return (<tr>  
  4.    <td>{SingleProduct.SingleProduct.id}</td>  
  5.    <td>{SingleProduct.SingleProduct.name}</td>  
  6.    <td>{SingleProduct.SingleProduct.status ? 'show' : 'hide'}</td>  
  7.    <td>{SingleProduct.SingleProduct.price}</td>  
  8. </tr>)  
  9. }  
To call this product list component i will give the syntax below .
  1. <ProductList productLists={productLists}></ProductList>  
Here I am assigning the productlists object to productLists props(red color). and those props have been used in child component
.
Output will be given below:
 
Pass Multiple Value Inside Component In React Using Props And Map