首页 > 解决方案 > 使用 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

标签: javascriptreactjs

解决方案


您可以使用 es6 语法将其拉出到自己的常量中

const { example } = this.state

现在你可以在任何地方都使用 'example' 而不是 'this.state.example'


推荐阅读