【发布时间】:2018-12-09 13:02:18
【问题描述】:
我在查看高阶组件的反应文档时偶然发现了一个示例,您可以在其中为特定组件记录道具
function logProps(WrappedComponent) {
return class extends React.Component {
componentWillReceiveProps(nextProps) {
console.log('Current props: ', this.props);
console.log('Next props: ', nextProps);
}
render() {
// Wraps the input component in a container, without mutating it. Good!
return <WrappedComponent {...this.props} />;
}
}
}
当你渲染组件时插入和插入有区别
{...this.props}
和
{props.children}
它们本质上都会将给定的道具返回给组件,是否有最佳实践或特定用例何时比另一个更好?
来自关于 props.children 的 react 文档
you can use props.children on components that represent ‘generic boxes’ and that ‘don’t know their children ahead of time’.
对我来说,这似乎与 {...this.props}
中的用例相同【问题讨论】:
标签: javascript reactjs