admin管理员组

文章数量:1277888

Why react crashes when it access props of undefined cannot read property of undefined

ex One: Knowing that props.name is undefined this code will return an error.

const ponent = (props) => {
         return(
             <p>{props.name}</p>
     )
  }

ex Two: Why in this case it is ok for react to render without errors undefined which in this case would return invisible.

const ponentTwo = () => {
         return(
             <p>{undefined}</p>
     )
  }

Why react crashes when it access props of undefined cannot read property of undefined

ex One: Knowing that props.name is undefined this code will return an error.

const ponent = (props) => {
         return(
             <p>{props.name}</p>
     )
  }

ex Two: Why in this case it is ok for react to render without errors undefined which in this case would return invisible.

const ponentTwo = () => {
         return(
             <p>{undefined}</p>
     )
  }
Share Improve this question asked Aug 3, 2018 at 13:12 DodzDodz 3071 gold badge7 silver badges14 bronze badges 4
  • 1 You should not get an cannot read property 'name' of undefined error by writing props.name. If e.g. props.person is undefined, you will get that error if you write props.person.name. – Tholle Commented Aug 3, 2018 at 13:13
  • Does props.person is undefined means also props.person.name is also undefined ? – Dodz Commented Aug 3, 2018 at 13:15
  • If there is no person property in the props object, props.person will give you undefined. Trying to access name on undefined will give the error cannot read property 'name' of undefined, because you can't access a property on undefined. – Tholle Commented Aug 3, 2018 at 13:16
  • 1 @Dodz, unsure if your question is answered or not. – Chris Commented Aug 3, 2018 at 13:21
Add a ment  | 

2 Answers 2

Reset to default 8

Writing props.name should not give you the error cannot read property 'name' of undefined. If the name property doesn't exist on the props object, props.name will just be undefined.

However, if there is e.g. no person property on the props object, props.person will give you undefined. Trying to access name on undefined will then give rise to your error, because you can't access a property on undefined.

You could then use the && operator to make sure props.person is set before using props.person.name.

Example

const MyComponent = props => {
  return <p>{props.person && props.person.name}</p>;
};

Also make sure that your ponent names start with an uppercase letter, or the ponent will be interpreted as a string instead of as a variable when using it.

If you are not passing props to the ponent, then props will be undefined. If this is true (that you are passing no props), then trying to access props.name will throw that exact error! In order to ensure that props are given to the ponent, you can do as the other answer says, using the && operator.

const MyComponent = props => {
  return <p>{props && props.name}</p>;
};

To answer the other part, React will render {undefined} because there is nothing to render. Just saying undefined isn't trying to access a property of undefined. Similar to if you had an object like

var myObject = undefined

and then tried to access a name property of this object:

console.log(myObject.name) //throws 'can not read property name of undefined'

本文标签: javascriptRender undefined in react componentStack Overflow