2018-10-12 15:37:13 -04:00
---
title: Pass an Array as Props
---
2019-07-24 00:59:27 -07:00
# Pass an Array as Props
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
To pass an array as a prop, first an array must be declared as a "tasks" prop on each of the components to be rendered:
2019-07-24 00:59:27 -07:00
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```javascript
2019-07-24 00:59:27 -07:00
const List = props => {
return <p></p>;
2018-10-12 15:37:13 -04:00
};
class ToDo extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h1>To Do Lists</h1>
<h2>Today</h2>
<List tasks={["Walk", "Cook", "Bake"]} />
<h2>Tomorrow</h2>
2019-07-24 00:59:27 -07:00
<List tasks={["Study", "Code", "Eat"]} />
2018-10-12 15:37:13 -04:00
</div>
);
}
2019-07-24 00:59:27 -07:00
}
2018-10-12 15:37:13 -04:00
```
Then, the props must be handled inside the "List" component:
```javascript
2019-07-24 00:59:27 -07:00
const List = props => {
return <p>{props.tasks.join(", ")}</p>;
2018-10-12 15:37:13 -04:00
};
// ... same as above
```
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
* The `.join(", ")` method is used to take each element from within the array and join them into a string to be displayed.
* We are using the modularity of React in this example to display the tasks passed by two different components to a common component which renders the final HTML.
</details>
2018-10-12 15:37:13 -04:00