我注意到,在许多 React.js 示例中,props
在用于组件之前会被解构。例如,以下内容来自 React-Redux sample code :
class AsyncApp extends Component {
....
componentDidMount() {
const { dispatch, selectedSubreddit } = this.props // <-- destructuring
dispatch(fetchPostsIfNeeded(selectedSubreddit))
}
....
}
这段代码是不是等价于下面的代码?这只是风格问题吗?
componentDidMount() {
this.props.dispatch(fetchPostsIfNeeded(this.props.selectedSubreddit))
}
最佳答案
严格来说,它并不等同,但两个序列最终会产生相同的结果。对于许多人来说,使用解构的示例更容易阅读。理论上,具有解构的代码可以稍微快一些,但我怀疑现实生活中是否存在任何差异,或者在两种样式之间进行选择时通常会考虑到这一点。
关于javascript - React 应用程序中解构与点表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44645931/