--- id: 5a24c314108439a4d403616a title: Pass an Array as Props challengeType: 6 forumTopicId: 301401 dashedName: pass-an-array-as-props --- # --description-- The last challenge demonstrated how to pass information from a parent component to a child component as `props` or properties. This challenge looks at how arrays can be passed as `props`. To pass an array to a JSX element, it must be treated as JavaScript and wrapped in curly braces. ```jsx ``` The child component then has access to the array property `colors`. Array methods such as `join()` can be used when accessing the property. `const ChildComponent = (props) =>

{props.colors.join(', ')}

` This will join all `colors` array items into a comma separated string and produce: `

green, blue, red

` Later, we will learn about other common methods to render arrays of data in React. # --instructions-- There are `List` and `ToDo` components in the code editor. When rendering each `List` from the `ToDo` component, pass in a `tasks` property assigned to an array of to-do tasks, for example `["walk dog", "workout"]`. Then access this `tasks` array in the `List` component, showing its value within the `p` element. Use `join(", ")` to display the `props.tasks`array in the `p` element as a comma separated list. Today's list should have at least 2 tasks and tomorrow's should have at least 3 tasks. # --hints-- The `ToDo` component should return a single outer `div`. ```js assert( (function () { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return mockedComponent.children().first().type() === 'div'; })() ); ``` The third child of the `ToDo` component should be an instance of the `List` component. ```js assert( (function () { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return mockedComponent.children().first().childAt(2).name() === 'List'; })() ); ``` The fifth child of the `ToDo` component should be an instance of the `List` component. ```js assert( (function () { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return mockedComponent.children().first().childAt(4).name() === 'List'; })() ); ``` Both instances of the `List` component should have a property called `tasks` and `tasks` should be of type array. ```js assert( (function () { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return ( Array.isArray(mockedComponent.find('List').get(0).props.tasks) && Array.isArray(mockedComponent.find('List').get(1).props.tasks) ); })() ); ``` The first `List` component representing the tasks for today should have 2 or more items. ```js assert( (function () { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return mockedComponent.find('List').get(0).props.tasks.length >= 2; })() ); ``` The second `List` component representing the tasks for tomorrow should have 3 or more items. ```js assert( (function () { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return mockedComponent.find('List').get(1).props.tasks.length >= 3; })() ); ``` The `List` component should render the value from the `tasks` prop in the `p` tag. ```js assert( (function () { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return ( mockedComponent .find('p') .get(0) .props.children.replace(/\s*,\s*/g, ',') === mockedComponent .find('List') .get(0) .props.tasks.join(',') .replace(/\s*,\s*/g, ',') && mockedComponent .find('p') .get(1) .props.children.replace(/\s*,\s*/g, ',') === mockedComponent .find('List') .get(1) .props.tasks.join(',') .replace(/\s*,\s*/g, ',') ); })() ); ``` # --seed-- ## --after-user-code-- ```jsx ReactDOM.render(, document.getElementById('root')) ``` ## --seed-contents-- ```jsx const List = (props) => { { /* Change code below this line */ } return

{}

{ /* Change code above this line */ } }; class ToDo extends React.Component { constructor(props) { super(props); } render() { return (

To Do Lists

Today

{ /* Change code below this line */ }

Tomorrow

{ /* Change code above this line */ }
); } }; ``` # --solutions-- ```jsx const List= (props) => { return

{props.tasks.join(', ')}

}; class ToDo extends React.Component { constructor(props) { super(props); } render() { return (

To Do Lists

Today

Tomorrow

); } }; ```