Fe-interview: [react] 如果组件的属性没有传值,那么它的默认值是什么?

Created on 16 Jul 2019  ·  7Comments  ·  Source: haizlin/fe-interview

[react] 如果组件的属性没有传值,那么它的默认值是什么?

react

Most helpful comment

我第一反应是true,因为在官方文档中看过,也实际使用过。然后又觉得也可能是undefined(不知为啥,就是迷)。然后亲自试验了下,又看了看文档,确实是true。
https://zh-hans.reactjs.org/docs/jsx-in-depth.html#props-default-to-true

All 7 comments

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

这两个是相等的

Was this page helpful?
0 / 5 - 0 ratings