Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - map array to child component

I'm coding a site with multiple pages. A page ComponentA, have a child component that return sections with titles and paragraphs.

The array in ComponentA pass data as props to the child. Inside the child, a map function return the paragraphs correct. What's missing for the titles, how would you do to pass title1 to paragraph1, title2 to paragraph2 and so on?

ComponentA:

import Child from "../components/child";

const ComponentA = () => {
  <Layout>
    <h1>Home Page</h1>
    <Child title={info.title} text={info.text} />
  </Layout>
}

const info = {
  title: ["Title1", "Title2"],
  text: ["Paragraph1", "Paragraph2"]
};

Child component:

const Child = ({ text, title }) => {
  return (
    <div>
      {text.map(text => {
        return (
          <div>
            <h3>{title}</h3>
            <p>{text}</p>
          </div>
        );
      })}
    </div>
  );
};
like image 458
noxsta Avatar asked Mar 24 '19 06:03

noxsta


People also ask

How do you pass an array of objects into a child component in React?

To pass an array as a prop to a component in React, wrap the array in curly braces, e.g. <Books arr={['A', 'B', 'C']} /> . The child component can perform custom logic on the array or use the map() method to render the array's elements. Copied!

How do I map an array in React?

We assign the new array returned by map() to the variable doubled and log it: const numbers = [1, 2, 3, 4, 5]; const doubled = numbers.map((number) => number * 2);console.log(doubled); This code logs [2, 4, 6, 8, 10] to the console. In React, transforming arrays into lists of elements is nearly identical.

How do you pass object from parent to child in React?

React. js allows us to use props (short form of property) to pass data from parent component to child component. We have to set props value inside the child component, while we embed it to the parent component.

How do you get data from array of objects in React JS?

How we will render an Array of Objects? We will use the map function to access each object from the array. The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method.


1 Answers

Your info object is not an iterable list - so I would convert them into a list {title, text} like so:

const data = info.title.map((e,i) => {
  return {title : e, text: info.text[i]}
})

Now I would shift the map() function to ComponentA instead of Child as that makes the child component more meaningful.

See demo below:

const info = {
  title: ["Title1", "Title2"],
  text: ["Paragraph1", "Paragraph2"]
};

const data = info.title.map((e,i) => {
  return {title : e, text: info.text[i]}
})

const ComponentA = () => {
  return (
    <div>
      <h1>Home Page</h1>
      { data.map(item => {
          return (
            <Child key={item.title} title={item.title} text={item.text} />
          );
        })
      }
    </div>
  )
}

const Child = ({ text, title }) => {
  return (
    <div>
      <h3>{title}</h3>
      <p>{text}</p>
    </div>
  );
};

ReactDOM.render(
  <ComponentA/>, 
  document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"/>
like image 156
kukkuz Avatar answered Nov 15 '22 14:11

kukkuz