javascript - 在渲染中干净地分配嵌套的 this.props const
问题描述
在render()
即时传递许多变量。最佳实践似乎指向在调用 a 之前做这样的事情return <div>...etc</div
:
const {
products: mainProducts,
parent,
getProductsByOffset,
offset,
count,
formOnChange,
isGettingMissingProducts
} = this.props
这一切都很好,但我有一个特定的对象需要获取嵌套很深的对象。而不是让对象/ s做:
const assignProducts = this.props.parent.props.input.values
我将如何很好地提取它。我认为它可能并且在最佳实践中能够按照我的做法做到这一点products: mainProducts
解决方案
我确实认为您的const assignProducts = this.props.parent.props.input.values
外观比任何替代方案都好,但是如果您想继续解构以提取该嵌套属性,则可以使用以下语法:
const {
products: mainProducts,
parent, // if you only need the `assignProducts`, remove this line
parent: { props: { input: { values: assignProducts }}},
getProductsByOffset,
offset,
count,
formOnChange,
isGettingMissingProducts
} = this.props
请注意,您可以同时提取parent
属性(如果需要)并同时提取其中的属性parent
。但这看起来比您的原始代码 IMO 更难看。
推荐阅读
- scala - Spark结构化流:获取1分钟窗口聚合的第二行
- excel - 在列中满足值后发送电子邮件
- javascript - 如何自动检测 DOM 中的元素以填充 const 数组?
- visual-studio-code - 每次在 VSCode 中启动 RLS 时出现“重复的 RLS 配置”
- python - 对 Pandas df 中的列组合进行详尽搜索
- mysql - MariaDB 查询使用 IN 和 LIMIT by row
- python - PyCharm 社区版
- oozie - OOZIE workflow.xml 没有函数映射到名称 coord:nominalTime
- python - 运行 python 脚本时,我得到 - 而不是连字符
- linux - QProcess::用 su 命令执行