英文:
How to resolve the following error? "Cannot read properties of undefined (reading 'toLowerCase')
问题
I am making a project on React and have come across an error which I have given below.
我正在进行一个React项目,遇到了下面的错误。
I am trying to resolve the error but am unable to do so.
我正在尝试解决这个错误,但无法成功。
Can someone please help?
请问有人能帮忙吗?
The error is as follows:
Cannot read properties of undefined (reading 'toLowerCase')
TypeError: Cannot read properties of undefined (reading 'toLowerCase')
错误如下:
无法读取未定义的属性(读取 'toLowerCase')
TypeError: 无法读取未定义的属性(读取 'toLowerCase')
App.js
import "./App.css";
import Navbar from "./Components/Navbar";
import TextForm from "./TextForm";
import React, { useState } from "react";
import Alert from "./Components/Alert";
function App() {
const [mode, setMode] = useState("light");
const [alert, setAlert] = useState(null);
const showAlert = (message, type) => {
setAlert({
msg: message,
type: type,
});
setTimeout(() => {
showAlert(null);
}, 1000);
};
const toggleMode = () => {
if (mode === "light") {
setMode("dark");
document.body.style.backgroundColor = "black";
showAlert("The dark mode has been set", "success");
} else {
setMode("light");
document.body.style.backgroundColor = "white";
showAlert("The light mode has been set", "success");
}
};
return (
<>
<Navbar toggleMode={toggleMode} mode={mode} />
<Alert alert={alert} />
<TextForm
heading="This is textarea"
mode={mode}
alert={alert}
showAlert={showAlert}
/>
</>
);
}
export default App;
Alert.js
import React from "react";
export default function Alert(props) {
const capitalize = (word) => {
const lower = word.toLowerCase();
return lower.charAt(0).toUpperCase() + lower.slice(1);
};
return (
props.alert &&
(
<div
class={`alert alert-${props.alert.type} alert-dismissible fade show`}
role="alert"
>
<strong>{capitalize(props.alert.type)}:</strong>{props.alert.msg}
<button
type="button"
class="btn-close"
data-bs-dismiss="alert"
aria-label="Close"
></button>
</div>
)
);
}
Alert.js 文件内容已提供。
英文:
I am making a project on React and have come across an error which I have given below.
I am trying to resolve the error but am unable to do so.
Can someone please help?
The error is as follows:
Cannot read properties of undefined (reading 'toLowerCase')
TypeError: Cannot read properties of undefined (reading 'toLowerCase')
App.js
import "./App.css";
import Navbar from "./Components/Navbar";
import TextForm from "./TextForm";
import React, { useState } from "react";
import Alert from "./Components/Alert";
function App() {
const [mode, setMode] = useState("light");
const [alert, setAlert] = useState(null);
const showAlert = (message, type) => {
setAlert({
msg: message,
type: type,
});
setTimeout(() => {
showAlert(null);
}, 1000);
};
const toggleMode = () => {
if (mode === "light") {
setMode("dark");
document.body.style.backgroundColor = "black";
showAlert("The dark mode has been set", "success");
} else {
setMode("light");
document.body.style.backgroundColor = "white";
showAlert("The light mode has been set", "success");
}
};
return (
<>
<Navbar toggleMode={toggleMode} mode={mode} />
<Alert alert={alert} />
<TextForm
heading="This is textarea"
mode={mode}
alert={alert}
showAlert={showAlert}
/>
</>
);
}
export default App;
Alert.js
import React from "react";
export default function Alert(props) {
const capitalize = (word) => {
const lower = word.toLowerCase();
return lower.charAt(0).toUpperCase() + lower.slice(1);
};
return (
props.alert && (
<div
class={`alert alert-${props.alert.type} alert-dismissible fade show`}
role="alert"
>
<strong>{capitalize(props.alert.type)}</strong>:{props.alert.msg}
<button
type="button"
class="btn-close"
data-bs-dismiss="alert"
aria-label="Close"
></button>
</div>
)
);
}
答案1
得分: 0
以下是翻译好的部分:
TL;DR
你可能想要在 setTimeout
函数中使用 setAlert
,而不是调用 showAlert
(后者还会导致无限循环):
const showAlert = (message, type) => {
setAlert({
msg: message,
type: type,
});
setTimeout(() => {
setAlert(null);
}, 1000);
}
const showAlert = (message, type) => {
setAlert({
msg: message,
type: type,
});
setTimeout(() => {
showAlert(null);
}, 1000);
}
当调用 showAlert(null)
时,它解析为 setAlert({ msg: null, type: undefined })
。
alert
本身不为空: { msg: null, type: undefined }
,这使得 props.alert
为真,但 props.alert.type
是 undefined
,因此 capitalize(props.alert.type)
收到一个 undefined
。
英文:
TL;DR
What you might want is to setAlert
instead of calling showAlert
(which also causes an infinite loop) in the setTimeout
function:
const showAlert = (message, type) => {
setAlert({
msg: message,
type: type,
});
setTimeout(() => {
// showAlert(null); // wrong
setAlert(null);
}, 1000);
}
const showAlert = (message, type) => {
setAlert({
msg: message,
type: type,
});
setTimeout(() => {
showAlert(null);
// ^^^^^^^^^^^^^^^
}, 1000);
}
When showAlert(null)
is called, it resolves into setAlert({ msg: null, type: undefined })
.
The alert
itself is not not empty: { msg: null, type: undefined }
, which makes props.alert
truthy, but props.alert.type
is undefined
, so capitalize(props.alert.type)
receives an undefined
.
答案2
得分: 0
错误的原因是因为在capitalize
函数中,你应该检查单词是否未定义或为空,然后执行进一步的操作,而在你的情况下,操作是.toLowerCase()
。
英文:
Error cause is because the in the capitalize
function you should check if word is not undefined or null then you should perform further operations, in your case operation is .toLowerCase()
答案3
得分: 0
使用 "showAlert(null)" 调用时,警报对象在技术上并不是空的,因为它具有值为 null 的 "msg" 属性,它的 "type" 属性是未定义的。这意味着 props.alert 是真值,props.alert.type 是未定义的,因此您会收到此错误。
英文:
With "showAlert(null)" call, the alert object is not technically empty as it has a "msg" property with a value of null, its "type" property is undefined.
That means props.alert is truthy, props.alert.type is undefined, hence you are getting this error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论