处理 TypeError Cannot read 属性 'something' of null 的可能方法是什么?
What are the possible way to handle TypeError Cannot read property 'something' of null?
在 Reactjs 中处理这个问题的可能方法是什么?
TypeError
Cannot read property 'something' of null
您有两个选择:
1- 始终检查 null
:
let blah;
if (obj) {
blah = obj.something;
}
2- 使用 Optional Chaining
const blah = obj?.something;
来自文档:
The ?. operator functions similarly to the . chaining operator, except
that instead of causing an error if a reference is nullish (null or
undefined), the expression short-circuits with a return value of
undefined.
在 Reactjs 中处理这个问题的可能方法是什么?
TypeError
Cannot read property 'something' of null
您有两个选择:
1- 始终检查 null
:
let blah;
if (obj) {
blah = obj.something;
}
2- 使用 Optional Chaining
const blah = obj?.something;
来自文档:
The ?. operator functions similarly to the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined.