如何在 React 中根据条件满足重新将 const 变量重新赋值为 true

huangapple go评论74阅读模式
英文:

How to reassign const variable to true if condition is met in react

问题

如何在React中满足条件时将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) {
    // 它没有读取这行代码。
    const displayInternalForm = true;
  }

}
英文:

How can I reassign a const to true if a condition is met in react? It keeps showing an error of 'displayInternalForm' is declared but its value is never read.

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;
  }

}

答案1

得分: 5

你应该在变量需要重新赋值时使用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) {
    // 它不会执行这行代码。
    displayInternalForm = true;
  }

}
英文:

You should use let if a variable is reassigned.

Read More here

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;
  }

}

huangapple
  • 本文由 发表于 2023年2月24日 17:41:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定