简体   繁体   English

React 重新渲染 State 问题

[英]React Re-Render State Issue

Wondering if anyone can help me.想知道是否有人可以帮助我。 I am running into this weird issue when a state updates to re-renders all states in a react file:当 state 更新以重新呈现反应文件中的所有状态时,我遇到了这个奇怪的问题:

Here is the main state that I don't want to update when doing something else:这是我在做其他事情时不想更新的主要 state:

{dashboard.layout.map((layout, i) => createContent(layout, i))}

However, when using Material UI Dialog I click on a button to open it which updates the state as follows:但是,当使用 Material UI 对话框时,我单击一个按钮将其打开,更新 state 如下:

onClick={handleDashletSelectorOpen}
const handleDashletSelectorOpen = () => {
    setDashletSelectorOpen(true);
  };

Here are the two states in question:这是有问题的两个州:

const [dashboard, setDashboard] = useState({
    layout: [],
    widgets: []
  });
const [dashletSelector, setDashletSelectorOpen] = useState(false);

What I would like to do is open the dialog box but without re-rendering the dashboard state. Is this possible?我想做的是打开对话框但不重新呈现仪表板 state。这可能吗? I'm sure there's something easy I'm missing but cannot figure it out.我确定我缺少一些简单但无法弄清楚的东西。

Any assistance would be great.任何帮助都会很棒。

Thanks谢谢

I was going about this all wrong.我的想法全错了。 The issue here is the different "states" aren't different states rather one state with multiple props.这里的问题是不同的“状态”不是不同的状态,而是一个带有多个道具的 state。 Once I realised this the solution was easy, to get around this I moved all of the other props that didn't matter to a child component that way if the parent re-rendered then that's fine but if the child re-renders then the parent doesn't.一旦我意识到这一点,解决方案就很简单,为了解决这个问题,我将所有其他无关紧要的道具移动到子组件,如果父组件重新渲染,那很好,但如果子组件重新渲染,那么父组件没有。 Worked a charm很有魅力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM