【问题标题】:How to reassign const variable to true if condition is met in react如果在反应中满足条件,如何将 const 变量重新分配为 true
【发布时间】:2023-02-24 18:18:33
【问题描述】:

如果在反应中满足条件,我如何将 const 重新分配为 true?它一直显示错误'displayInternalForm' 已声明,但其值从未被读取.

export async function getStaticProps({ params }) {

  const displayInternalForm = false;

  const internalJob = internalcategory.categories.find((cat) => 
                      cat.id?.includes("a5c26877-e89c-440b-b7a0-31552865fff5"));

  if (internalJob !== undefined) {
    // It is not reading this line of code. 
    const displayInternalForm = true;
  }

}

【问题讨论】:

  • 不能用“let”代替const吗?

标签: reactjs react-native next


【解决方案1】:

如果重新分配变量,则应使用 let

export async function getStaticProps({ params }) {

  let displayInternalForm = false;

  const internalJob = internalcategory.categories.find((cat) => 
                      cat.id?.includes("a5c26877-e89c-440b-b7a0-31552865fff5"));

  if (internalJob !== undefined) {
    // It is not reading this line of code. 
   displayInternalForm = true;
  }

}

【讨论】:

    猜你喜欢
    • 2016-02-05
    • 2021-09-28
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    相关资源
    最近更新 更多