React Warning: Maximum update depth exceeded

huangapple go评论73阅读模式
英文:

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 = &quot;http://localhost:4010&quot;
const socket = io(address);
function App() {
const [MenuState, setMenuState] = useState(&quot;/&quot;)
const [UIinputs, setUIinputs] = useState({
progressState: &quot;stop&quot;
});
const [UIoutputs, setUIoutputs] = useState({
deliveredEnergy: 0,
estTime: &quot;00:00:00&quot;,
estCost: 0,
soc: 75,
chargerState: 4,
currentPower: 0,
progressState: 2,
current: 0,
voltage: 5,
carID: &quot;a&quot;,
debug: [{}, {}]
});
useEffect(() =&gt; {
socket.emit(&#39;UIinputs&#39;, UIinputs)
// eslint-disable-next-line
}, [UIinputs])
useEffect(() =&gt; {
socket.on(&#39;UIoutputs&#39;, (data) =&gt; {
setUIoutputs(UIoutputs =&gt; ({
...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 (
&lt;React.Fragment&gt;
&lt;CssBaseline /&gt;
&lt;div className=&#39;app-container&#39;&gt;
&lt;div className=&#39;top-bar&#39;&gt;
&lt;Header /&gt;
&lt;BackButton
MenuState={MenuState}
setMenuState={setMenuState} /&gt;
&lt;ScanQR /&gt;
&lt;/div&gt;
&lt;Routes&gt;
&lt;Route path=&#39;/&#39; element={
&lt;Menu
UIoutputs={UIoutputs}
setMenuState={setMenuState} /&gt;}&gt;
&lt;/Route&gt;
&lt;Route path=&#39;/profile&#39; element={
&lt;Profile
UIoutputs={UIoutputs} /&gt;}&gt;
&lt;/Route&gt;
&lt;Route path=&quot;/station&quot; element={
&lt;Charger
UIinputs={UIinputs}
setUIinputs={setUIinputs}
UIoutputs={UIoutputs}
setUIoutputs={setUIoutputs} /&gt;}&gt;
&lt;/Route&gt;
&lt;Route path=&quot;/dev&quot; element={
&lt;Dev
UIoutputs={UIoutputs} /&gt;}&gt;
&lt;/Route&gt;
&lt;/Routes&gt;
&lt;BottomBar /&gt;
&lt;/div&gt;
&lt;/React.Fragment&gt;
);
};

答案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(() =&gt; {
socket.on(&#39;UIoutputs&#39;, (data) =&gt; {
setUIoutputs(UIoutputs =&gt; ({
...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 -->

huangapple
  • 本文由 发表于 2023年7月31日 18:15:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76802628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定