javascript - 使用 this.state.example 太多次,如何将其放入变量中?
问题描述
我目前在我的一个 JS 文件中使用 this.state.example 进行反应。我怎样才能继续将它添加到变量中,这样它就不会那么混乱了?
return (
<div className="wrapper">
<header className="example">
<h1>{this.state.test.products[i].name}</h1>
</header>
我想替换 this.state.test.products
解决方案
您可以使用 es6 语法将其拉出到自己的常量中
const { example } = this.state
现在你可以在任何地方都使用 'example' 而不是 'this.state.example'
推荐阅读
- javascript - 在 Promise 在 componentDidMount 中解析后反应 setState
- python - 在哪里放置 sys.exit() 以终止代码
- python-3.x - 数字识别:我从画布图像中得到相同的输出
- reactjs - 在reactjs甜甜圈图中,如何减小图表的宽度
- android - 使用 AWS API 的应用程序是否需要身份验证?
- python - 一个模型的多个反向 URL
- python - 如何使用python将xml文件转换为csv文件
- c# - 我无法在 C# 中使用 Jquery 和 Ajax 从控制器向 Razor 视图发送值
- django - 从 Apple 快捷方式访问时,对 API 的调用始终返回“未提供身份验证凭据”,但从 Postman 访问时则不返回
- android - 如何调整画廊中的高质量图像以显示在图像视图中?