英文:
React Warning: Maximum update depth exceeded
问题
我是React.js的新手,我有一个与用Python编写的服务器通信的应用程序。我使用**useEffect
**来持续更新来自服务器的信息,但是我遇到了“Maximum update depth exceeded”错误。这是我的代码:
const address = "http://localhost:4010";
const socket = io(address);
function App() {
const [MenuState, setMenuState] = useState("/");
const [UIinputs, setUIinputs] = useState({
progressState: "stop"
});
const [UIoutputs, setUIoutputs] = useState({
deliveredEnergy: 0,
estTime: "00:00:00",
estCost: 0,
soc: 75,
chargerState: 4,
currentPower: 0,
progressState: 2,
current: 0,
voltage: 5,
carID: "a",
debug: [{}, {}]
});
useEffect(() => {
socket.emit('UIinputs', UIinputs);
// eslint-disable-next-line
}, [UIinputs]);
useEffect(() => {
socket.on('UIoutputs', (data) => {
setUIoutputs(UIoutputs => ({
...UIoutputs,
deliveredEnergy: data.deliveredEnergy,
estTime: data.estTime,
soc: data.soc,
chargerState: data.chargerState,
currentPower: data.currentPower,
progressState: data.progressState,
current: data.current,
carID: data.carID,
voltage: data.voltage,
debug: data.debug,
}));
});
// eslint-disable-next-line
}, [UIoutputs]);
return (
<React.Fragment>
<CssBaseline />
<div className='app-container'>
<div className='top-bar'>
<Header />
<BackButton
MenuState={MenuState}
setMenuState={setMenuState} />
<ScanQR />
</div>
<Routes>
<Route path='/' element={
<Menu
UIoutputs={UIoutputs}
setMenuState={setMenuState} />} />
<Route path='/profile' element={
<Profile
UIoutputs={UIoutputs} />} />
<Route path="/station" element={
<Charger
UIinputs={UIinputs}
setUIinputs={setUIinputs}
UIoutputs={UIoutputs}
setUIoutputs={setUIoutputs} />} />
<Route path="/dev" element={
<Dev
UIoutputs={UIoutputs} />} />
</Routes>
<BottomBar />
</div>
</React.Fragment>
);
};
希望这可以帮助到你!如果你有任何其他问题,请随时问我。
英文:
I am new to React.js and I have an app that communicates with a server written in Python. I use useEffect
to continuously update the information coming from the server But I'm getting the 'Maximum update depth exceeded' error. This is my code:
const address = "http://localhost:4010"
const socket = io(address);
function App() {
const [MenuState, setMenuState] = useState("/")
const [UIinputs, setUIinputs] = useState({
progressState: "stop"
});
const [UIoutputs, setUIoutputs] = useState({
deliveredEnergy: 0,
estTime: "00:00:00",
estCost: 0,
soc: 75,
chargerState: 4,
currentPower: 0,
progressState: 2,
current: 0,
voltage: 5,
carID: "a",
debug: [{}, {}]
});
useEffect(() => {
socket.emit('UIinputs', UIinputs)
// eslint-disable-next-line
}, [UIinputs])
useEffect(() => {
socket.on('UIoutputs', (data) => {
setUIoutputs(UIoutputs => ({
...UIoutputs,
deliveredEnergy: data.deliveredEnergy,
estTime: data.estTime,
soc: data.soc,
chargerState: data.chargerState,
currentPower: data.currentPower,
progressState: data.progressState,
current: data.current,
carID: data.carID,
voltage: data.voltage,
debug: data.debug,
}));
});
// eslint-disable-next-line
}, [UIoutputs])
return (
<React.Fragment>
<CssBaseline />
<div className='app-container'>
<div className='top-bar'>
<Header />
<BackButton
MenuState={MenuState}
setMenuState={setMenuState} />
<ScanQR />
</div>
<Routes>
<Route path='/' element={
<Menu
UIoutputs={UIoutputs}
setMenuState={setMenuState} />}>
</Route>
<Route path='/profile' element={
<Profile
UIoutputs={UIoutputs} />}>
</Route>
<Route path="/station" element={
<Charger
UIinputs={UIinputs}
setUIinputs={setUIinputs}
UIoutputs={UIoutputs}
setUIoutputs={setUIoutputs} />}>
</Route>
<Route path="/dev" element={
<Dev
UIoutputs={UIoutputs} />}>
</Route>
</Routes>
<BottomBar />
</div>
</React.Fragment>
);
};
答案1
得分: 0
当你在useEffect中更新UIoutputs状态时,它会触发重新渲染,进而导致useEffect再次运行,从而创建了一个无限循环的状态更新和重新渲染。为了解决这个问题,你需要确保只在必要时更新状态,并避免引起不必要的重新渲染。
所以像这样移除依赖项:
useEffect(() => {
socket.on('UIoutputs', (data) => {
setUIoutputs(UIoutputs => ({
...UIoutputs,
deliveredEnergy: data.deliveredEnergy,
estTime: data.estTime,
soc: data.soc,
chargerState: data.chargerState,
currentPower: data.currentPower,
progressState: data.progressState,
current: data.current,
carID: data.carID,
voltage: data.voltage,
debug: data.debug,
}));
});
// eslint-disable-next-line
}, [])
英文:
When you update the UIoutputs state inside the useEffect, it triggers a re-render, which in turn causes the useEffect to run again, creating an infinite loop of state updates and re-renders. To fix this issue, you need to ensure that you only update the state when necessary and avoid causing unnecessary re-renders.
so remove the dependency like this
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
useEffect(() => {
socket.on('UIoutputs', (data) => {
setUIoutputs(UIoutputs => ({
...UIoutputs,
deliveredEnergy: data.deliveredEnergy,
estTime: data.estTime,
soc: data.soc,
chargerState: data.chargerState,
currentPower: data.currentPower,
progressState: data.progressState,
current: data.current,
carID: data.carID,
voltage: data.voltage,
debug: data.debug,
}));
});
// eslint-disable-next-line
}, [])
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论