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 writingprops.name
. If e.g.props.person
isundefined
, you will get that error if you writeprops.person.name
. – Tholle Commented Aug 3, 2018 at 13:13 -
Does
props.person
isundefined
means alsoprops.person.name
is alsoundefined
? – Dodz Commented Aug 3, 2018 at 13:15 -
If there is no
person
property in theprops
object,props.person
will give youundefined
. Trying to accessname
onundefined
will give the errorcannot read property 'name' of undefined
, because you can't access a property onundefined
. – 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
2 Answers
Reset to default 8Writing 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
版权声明:本文标题:javascript - Render undefined in react component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741213684a2359630.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论