通常,将使用Webpack的 DefinePlugin 方法将 NODE_ENV 设置为 production。这将剥离像 propType 验证和额外的警告。除此之外,还有一个好主意,可以减少你的代码,因为React使用 Uglify 的 dead-code 来消除开发代码和注释,这将大大减少你的包的大小。
为什么要使用 React.Children.map(props.children,()=>) 而不是 props.children.map(()=>)
因为不能保证props.children将是一个数组。
以此代码为例,
<Parent>
<h1>Welcome.</h1>
</Parent>
在父组件内部,如果我们尝试使用 props.children.map 映射孩子,则会抛出错误,因为 props.children 是一个对象,而不是一个数组。
如果有多个子元素,React 只会使props.children成为一个数组。就像下面这样:
<Parent>
<h1>Welcome.</h1>
<h2>props.children will now be an array</h2>
</Parent>
这就是为什么你喜欢 React.Children.map ,因为它的实现考虑到 props.children 可能是一个数组或一个对象。
网友评论