【发布时间】:2019-04-11 11:30:20
【问题描述】:
当我发现自己在 react component 中多次使用相同的 props(例如,在组件的不同方法中)时,我最终会多次解构 props。
这是不好的做法吗?我是否应该将多次解构的prop 分配给实例本身(类似于constructor() 中的this.propOfInterest = this.props.propOfInterest?
class MyComponent extends React.Component {
myMethod() {
const {
propOfInterest,
} = this.props
// do something with propOfInterest
}
render() {
const {
propOfInterest,
} = this.props
return (
<div className={propOfInterest}>
</div>
)
}
}
【问题讨论】:
-
软件工程将帮助您获得有关“最佳实践”问题的答案。因此,我投票决定将此问题作为离题结束,因为它属于“softwareengineering.stackexchange.com”
标签: javascript reactjs frontend web-frontend react-props