You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
A variable can be assigned a default, in the case that the value unpacked from the object is undefined.
MDN 下的例子非常生动形象的解释了:
In the second call in this example, even if the first argument is set explicitly to undefined (though not null or other falsy values), the value of the num argument is still the default.
functiontest(num=1){console.log(typeofnum)}test()// 'number' (num is set to 1)test(undefined)// 'number' (num is set to 1 too)// test with other falsy values:test('')// 'string' (num is set to '')test(null)// 'object' (num is set to null)
水文一篇...
场景
两个组件:
App
和Hello
我当时天真的以为这里会渲染
Hello mike
. 实际上最后渲染的是Hello
...另这里用 es6 的对象解构默认参数还是用 react 提供的
defaultProps
效果都是一样的原因
去查了一下 MDN 的 Default parameters 以及 Destructuring assignment
, 果然是我疏忽了:
MDN 下的例子非常生动形象的解释了:
以及, React 官方有一个 issue 专门对于这个现象的讨论: null props considered differently in getDefaultProps vs. isRequired #2166
总结来讲就是(具体细节不细纠结了...网上应该很多文章大讲特讲这两个的区别):
null
是一个定义过的的值, 这个值是存在的, 虽然这个值代表了啥都没有undefined
这个值根本不存在所以上面的组件想要触发默认 props 需要这么做:
解决办法?
其实我没找到最佳实践, 因为有好多种写法可以规避 null 的出现. 但是我在 stackoverflow 上搜到了一个类似的问题: React.js - default prop is not used with
null
is passed, 最高票答案是这样的(56 个赞), 以我的例子举例:好的...
The text was updated successfully, but these errors were encountered: