这是来self 的文章组件的代码片段:
const [articles, setArticles] = useState([]);
const [err, setErr] = useState("");
const token = sessionStorage.getItem("token");
const axiosWithToken = axios.create({
headers: { Authorization: `Bearer ${token}` },
});
const getArticles = async () => {
const res = await axiosWithToken.get("http://localhost:5000/user/articles");
if (res.data.message === "All Articles") {
setArticles(res.data.payload);
} else {
setErr(res.data.message);
}
};
useEffect(() => {
getArticles();
}, []);
此代码正在运行,没有任何错误.但我认为使用带大括号的箭头函数运行一个函数太多了,所以我将useEffect挂接中的箭头函数更改为:
useEffect(() => getArticles(), []);
但是现在,当我加载文章组件并转到任何其他路径时,页面抛出一个未知错误,它只说明是'n' is not a function
.我不明白为什么会发生这样的事情,因为我认为这两者在功能上是一样的.