我正在使用react-navigation,但无法弄清楚此语法的含义。
React.useEffect(() => {
if (route.params?.post) { <<<<<WHAT IS THIS ?
// Post updated, do something with `route.params.post`
// For example, send the post to the server
}
}, [route.params?.post]);
它像对象一样工作。 doesPropertyExist .subProperty还是其他?
我尝试遍历MDN文档,但是找不到对这种语法的任何引用。我无法在节点REPL中的随机对象上使用类似的语法。
答案 0 :(得分:2)
它称为可选链接。在MDN文档中提供:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
可选的链接运算符(?。)允许读取a的值 属性位于连接对象链的深处,没有 必须明确验证链中的每个参考文献都是 有效。 ?。操作符的功能类似于。链运算符, 除非引用无效,否则不会引起错误 (null或undefined),则表达式会因返回值而短路 的未定义。与函数调用一起使用时,如果返回,则返回undefined 给定的功能不存在。
if(route.params.post)
然后执行一些操作。
它检查route.params
是否有对象post
答案 1 :(得分:1)
它称为Optional Chaining operator。这是
的简写route.params && route.params.post