0

I have a problem with my component, I need to render list of skills-elements from array in export.data. I think, that I missed some arguments in my map:

const React = require('react')

class Post extends React.Component {
  render () {
    const { route } = this.props
    const post = route.page.data
    return this.props.route.page.data.skills.map(() => (
      <div>
        <h1>{this.props.route.page.data.title}</h1>
        <ul>
          <li>{this.props.route.page.data.skills}</li>
        </ul>
      </div>
    )
   )
  }
}
Post.propTypes = {
  route: React.PropTypes.object,
}
export default Post

exports.data = {
  title: 'Tech Skills',
  skills: ['One skill', 'Two skill'],
}

3 Answers 3

1

You need to recieve the skill inside your map, since your map function will recieve each element of the array.

You can get the information of each element in the skill object that the map recieves.

Also, with your code, beware that you will be showing the <h1> title for each element that you are printing. You should do something like this:

return (
   <div>
       <h1>{this.props.route.page.data.title}</h1>
       <ul>
           {
             this.props.route.page.data.skills.map((skill) => {
               return <li key={skill.id}>{skill}</li>
             })
           }
       </ul>
   </div>
)

Here, you will only show the title once, and afterwards you will create a element of the list of each skill. Also remember that the key parameter is needed if you are creating JSX elements inside a map or any iterating function, since React uses this key to better optimize the re-rendering of the component

Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Federico, thanks. You are right, I need only list with elements and not need to duplicate my <h1>. Only one thing is confused me now. My console shows me: "skill is not defined", before my changes everything was ok, and I write this.props.route.page.data.skills.map((skill) - so I don't know why "skill is not defined".
1

Reason is, you are returning more than one element from render method (an array). We can't return multiple elements so wrap them inside a div.

Don't forget that the render() is basically a function, Functions always take in a number of parameters and always return exactly one value.

Like this:

render () {
     const { route } = this.props;
     const post = route.page.data;
     return <div>
         {this.props.route.page.data.skills.map(el => (
             <div>
                 ...
             </div>
         ))}
       </div>
    )
}

Inside the map body el will be each object of the array, to access any value use el.keyName.

Comments

1

The callback in map function received the iterated element as its argument. Also, your render function should return a single container, which contains the list of elements. So you need to do something like this:

return (
<div>
  {this.props.route.page.data.skills.map((skill) => (
    <div key={skill}>
      <h1>{this.props.route.page.data.title}</h1>
      <ul>
        <li>{skill}</li>
      </ul>
    </div>
  ))}
</div>)

Note also that you need to give each <div> a key attribute to avoid errors in react.

2 Comments

Yes, I understand the general idea, but my trouble is how to do it exactly :) Now my console shows error to me like: "Post.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object."
Oh yeah sorry, you need to wrap the whole map thing in another container component, so the component render function returns a single element instead of an array of elements. I've updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.