简体   繁体   English

带有道具的 MUI Popper 组件在重新渲染时保持关闭组件

[英]MUI Popper Component with Props keeps closing component on re-render

I am curious how to architect a component leveraging MUI's Popover component when there are dynamic props getting passed to a controlled Slider component inside of the Popover component — as well as the anchor element also getting dynamically updated as the value changes getting passed-down from a higher order component.我很好奇当有动态道具被传递到 Popover 组件内部的受控 Slider 组件时,如何利用 MUI 的 Popover 组件构建一个组件——以及随着值的变化从高阶分量。

What is happening is that when the controlled child is updated by the user, it dispatches the change higher up the chain, driving new values down, which then re-renders the component, setting the anchorEl back to null. Here's a quick video in action:正在发生的事情是,当用户更新受控子项时,它会将更改发送到链的上游,将新值向下驱动,然后重新渲染组件,将anchorEl设置回 null。这是一个快速操作视频:

在此处输入图像描述

I'm sure there is something straightforward I could do to avoid this.我确信我可以做一些简单的事情来避免这种情况。 Any help is appreciated!任何帮助表示赞赏!

Here is abbreviated code:这是缩写代码:

function Component({ dynamicProps }) {
  const [anchorEl, setAnchorEl] = React.useState(null);
  const { dispatch } = useContext();

  const handleClick = (event) => {
    setAnchorEl(event.currentTarget);
  };

  const handleClose = () => {
    setAnchorEl(null);
  };

  const handleChange = (_, newValue) => {
      dispatch({
        body: newValue
      });
  };

  const open = Boolean(anchorEl);
  const id = open ? "simple-popover" : undefined;

  return (
    <div>
      <Button
        onClick={handleClick}
        label={dynamicProps.label}
      ></Button>
      <Popover
        id={id}
        open={open}
        anchorEl={anchorEl}
        onClose={handleClose}
      >
        <Box sx={{ minWidth: "200px", mx: 2 }}>
          <Slider
            value={dynamicProps.value}
            onChange={handleChange}
          />
        </Box>
      </Popover>
    </div>
  );
}

I have tried separating the Slider into another component, to avoid the re-render, and using my context's state to grab the values that I need, hover that point seems moot, since I still need to reference the anchorEl in the child, and since the trigger also is leveraging dynamic props, it will re-render and keep null-ing the anchorEl.我已经尝试将 Slider 分离到另一个组件中,以避免重新渲染,并使用我的上下文的 state 来获取我需要的值,hover 这一点似乎没有实际意义,因为我仍然需要在孩子中引用 anchorEl,并且因为触发器也利用动态道具,它将重新渲染并保持 anchorEl 为空。

Ok team.好的团队。 Figured this one all-by-myself我自己想出了这个

Here's what you don't want to do: If you're going to use context — use it both for dispatching and grabbing state. Don't drill-down state from a parent component that will trigger a re-render.以下是您不想做的事情:如果您要使用上下文 — 将它用于分派获取 state。不要从将触发重新渲染的父组件中向下钻取 state。 For both the button label and the controlled Slider, as long as you use the state insider the Component function through your context hook, you won't trigger a re-render, making your popper disappear from the re-render.对于按钮 label 和受控 Slider,只要您通过上下文挂钩使用组件 function 内部的 state,就不会触发重新渲染,使您的 popper 从重新渲染中消失。

Do this做这个

export default function Assumption({ notDynamicProps }) {
  const [anchorEl, setAnchorEl] = React.useState(null);
  const { dispatch, state } = useRentalCalculator();

Not this不是这个

export default function Assumption({ dynamicProps, notDynamicProps }) {
  const [anchorEl, setAnchorEl] = React.useState(null);
  const { dispatch } = useRentalCalculator();

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

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