【发布时间】:2020-04-27 10:15:25
【问题描述】:
我想知道在 React 类组件中耦合/嵌套功能组件而不通过参数显式传递道具并通过父类使用 this.props 的效果是什么。我知道在 React 类组件之外拥有一个功能组件更容易测试和阅读,但我很想知道通过参数使用 this.props 与 props 在性能/渲染方面的确切区别。
例如:
class Foo extends React.Component {
bar = () => { return (<p>{this.props.baz}</p>) }
render() {
return (
<h1>Hello, {this.props.name}</h1>
{this.bar()}
)
}
}
对比
class Foo extends React.Component {
render() {
return (
<h1>Hello, {this.props.name}</h1>
<Bar baz={'foobar'}
)
}
}
function Bar(props) {
return <p>{props.baz}</p>
}
【问题讨论】:
标签: javascript reactjs react-functional-component