[react] 如果组件的属性没有传值,那么它的默认值是什么?
undefined
true
默认值是undefined。
如果组件未设置defaultProps, 则为undefined, 否则为设置的默认值
我第一反应是true,因为在官方文档中看过,也实际使用过。然后又觉得也可能是undefined(不知为啥,就是迷)。然后亲自试验了下,又看了看文档,确实是true。
https://zh-hans.reactjs.org/docs/jsx-in-depth.html#props-default-to-true
如果某一个prop并没有初始化值,那么在没有设置默认值的情况下,是会直接返回undefined的,如果有默认值,那么就会返回默认值
我第一反应是true,因为在官方文档中看过,也实际使用过。然后又觉得也可能是undefined(不知为啥,就是迷)。然后亲自试验了下,又看了看文档,确实是true。
https://zh-hans.reactjs.org/docs/jsx-in-depth.html#props-default-to-true
Most helpful comment
我第一反应是true,因为在官方文档中看过,也实际使用过。然后又觉得也可能是undefined(不知为啥,就是迷)。然后亲自试验了下,又看了看文档,确实是true。
https://zh-hans.reactjs.org/docs/jsx-in-depth.html#props-default-to-true