48

I have a React.JS component that will map the notes variable to display.

However, I have run into the problem of having no notes and receiving an error. What is a proper way to approach this?

Here is the code:

import React, {Component} from 'react';


class List extends Component {
    constructor(props){
      super(props);
    }

render(){
  var notes = this.props.items.map((item, i)=>{
      return(
          <li className="listLink" key={i}>
              <p>{item.title}</p>
              <span>{item.content}</span>
          </li>
          )
      });
    return(
      <div className='list'>

          {notes}

      </div>
    );
  }
}

export default List;
1
  • Good question I was just wondering the same thing! Commented Aug 31, 2022 at 15:22

4 Answers 4

50

If you want to render the notes when at least one note exists and a default view when there are no notes in the array, you can change your render function's return expression to this:

return(
  <div className='list'>
      {notes.length ? notes : <p>Default Markup</p>}
  </div>
);

Since empty arrays in JavaScript are truthy, you need to check the array's length and not just the boolean value of an array.

Note that if your items prop is ever null, that would cause an exception because you'd be calling map on a null value. In this case, I'd recommend using Facebook's prop-types library to set items to an empty array by default. That way, if items doesn't get set, the component won't break.

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

1 Comment

nice move bro !
16

here is the simplest way to deal with

  import React, {Component} from 'react';


class List extends Component {
    constructor(props){
      super(props);
    }

render(){
  var notes = this.props.items?.map((item, i)=>{
      return(
          <li className="listLink" key={i}>
              <p>{item.title}</p>
              <span>{item.content}</span>
          </li>
          )
      });
    return(
      <div className='list'>

          {notes}

      </div>
    );
  }
}

export default List;

just try to add "?" for the array that you maped

3 Comments

Hello, can you explain why you add a question mark before the map function?
@YohanW.Dunon: this is called "optional chaining" -- if "items" (in this case) is undefined or null, the optional chaining (?) will just return undefined instead of throwing an error.
this does not work for me. Maybe because i use webpack to compile my front-end code?
6

You can just setting a fallback value for the this.props.item

render() {
  const items = this.props.items || [] // if there's no items, give empty array
  const notes = items.map((item, i) => {
  return(
      ....

Doing .map() on empty array will not produces an error, but will return an empty array. Which is fine because empty array is a renderable item in react and won't produce error in render() and will not render the notes as there are no notes provided.

1 Comment

doesn't work if items is an object. It still says the object exists so does not replace it with a list.
4

In your component, you can set the defaultProps property to set the initial value to your props:

static defaultProps = {
    items: []
};

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.