英文:
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论