0

I'm trying to loop through an array and return a React Component on each element. The render functions are being called but nothing is showing up.

class ListOfFoundPages extends Component {
  constructor(props) {
    super(props);
    this._renderList = this._renderList.bind(this);
  }
  _renderList(data) {
    if (data !== null) {
      // eslint-disable-next-line
      data.list.map( (obj) => {
        return <ListItem obj={obj} />;
      });
    }
  }
  render() {
    return (
      <ul className="listOfFoundPages">
        {this._renderList(this.props.list)}
      </ul>
    );
  }
}

And this is the Component to be returned:

const ListItem = (props) => {
  return (
    <li>
      <div className="foundPagesItem">
        <img role="presentation" className="searchPageImg" src={props.obj.picture.data.url} />
        <span className="searchPageInfo">{props.obj.name} - {props.obj.category}</span>
      </div>
      <div className="seperator" />
    </li>
  );
};
export default ListItem;
1

1 Answer 1

1

_renderList function should return an array of components (the result of data.list.map...). Currently, it doesn't return anything. The code needs to look like:

_renderList(data) {
  if (data !== null) {
    // eslint-disable-next-line
    return data.list.map( (obj) => {
      return <ListItem obj={obj} />;
    });
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.