我收到 React 中的错误:渲染的钩子比上一次渲染时更多。我检查了其他帖子,这些帖子使用带有反应钩子的条件来解决,但我无法在我的代码中诊断出类似的问题。如果我在 challenge.js 中注释掉代码的 Object.keys 部分,则不会显示错误,但如果我将其留在其中,则会收到错误消息。我相信这是由 keepsessionutils.js 文件中的错误引起的。请协助。
挑战.js
return (
<div className="about">
<div className="about-header">
<div className="about-headerText">
<h2> Dummy Challenge </h2>
<h2> Dummy Challenge </h2>
<h2> Dummy Challenge </h2>
<h2> Dummy Challenge </h2>
<h2> Dummy Challenge </h2>
<hr />
{Object.keys(valid_playlists).map(key => (
<button type="button" onClick={createChallengeUtil(key)}>
{valid_playlists[key]}
</button>
))}
</div>
</div>
</div>
);
}
createChallengeUtil.js
export default function createChallengeUtil(playlist_id) {
// Check if there are at least 20 songs
var token = KeepSession();
// Populate Firebase with challenge data
}
保持会话.js
export default function KeepSession() {
const [value, setValue] = React.useState(
localStorage.getItem('myValueInLocalStorage') || ''
);
// Here I am just checking to see if we need to retrieve a new token or not.
if (value === "undefined"){
var token = getLocalToken();
}
else{
var token = value;
}
// This block detects if the user refreshes the page and stores the current token if so.
window.onbeforeunload = (e) => {
// I'm about to refresh! do something...
localStorage.setItem('myValueInLocalStorage', token)
setValue(token);
};
return token
}
墨色风雨
慕妹3146593
相关分类