使用PrevProps时如何将ComponentDidupDate转换为使用效果

发布于 2025-01-24 05:16:29 字数 1285 浏览 0 评论 0原文

我有一个遗产 preaeAct (不是错字)组件,我试图重构使用钩子而不是基于类的组件。我遇到了一个障碍,因为在componentDidupdate中它仅引用prevprops而不是任何特定的道具。遗产生命周期方法看起来像这样:

  componentDidUpdate(prevProps) {
    addListenersFromProps(this.layer, this.props, {
      filter: ({ prop }) => !prevProps[prop],
    });

    removeListenersFromProps(this.layer, prevProps, {
      filter: ({ prop }) => !this.props[prop],
    });
  }

我的useffect看起来像这样:

  useEffect(() => {
    addListenersFromProps(layer, props, {
      filter: ({ prop }) => ?
    });
    removeListenersFromProps(layer, ?, {
      filter: ({ prop }) => ?
    });

  }, [?]);

我不确定要添加到useffeft hook的依赖项数组中,我不确定如何要调用removelisTenersFromProps,因为它采用PrevProps

//编辑:

根据第一个评论,我可以做一些简单的事情:


  
  // track props
  const [_props, setProps] = useState(props);
  

  useEffect(() => {
    addListenersFromProps(layer, props, {
      filter: ({ prop }) =>  !_props[prop],
    });
    removeListenersFromProps(layer, _props, {
      filter: ({ prop }) => !props[prop],
    });

    setProps(props);

  });

I have a legacy preact (not a typo) component I am trying to refactor to use hooks instead of class based components. I have run into a snag because in the componentDidUpdate it only references prevProps and not any specific prop. The legacy life cycle method looks like this:

  componentDidUpdate(prevProps) {
    addListenersFromProps(this.layer, this.props, {
      filter: ({ prop }) => !prevProps[prop],
    });

    removeListenersFromProps(this.layer, prevProps, {
      filter: ({ prop }) => !this.props[prop],
    });
  }

My useEffect looks like this:

  useEffect(() => {
    addListenersFromProps(layer, props, {
      filter: ({ prop }) => ?
    });
    removeListenersFromProps(layer, ?, {
      filter: ({ prop }) => ?
    });

  }, [?]);

I am not sure what to add to the dependency array for the useEffect hook and I am not sure how to call removeListenersFromProps as it takes the prevProps.

// EDIT:

Based on the first comment, could I do something as simple as this:


  
  // track props
  const [_props, setProps] = useState(props);
  

  useEffect(() => {
    addListenersFromProps(layer, props, {
      filter: ({ prop }) =>  !_props[prop],
    });
    removeListenersFromProps(layer, _props, {
      filter: ({ prop }) => !props[prop],
    });

    setProps(props);

  });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深陷 2025-01-31 05:16:29

您的编辑非常接近解决方案。您应该将道具用作使用效率中的回调,并应将状态设置在使用Props值的内部使用效果。请记住使用效果不是async,因此您需要在内部创建async函数才能使用我的逻辑

const [_props, setProps] = useState(props);
  

  useEffect(() => {
    //Here do your logic but set always to states, if you need create new states
    //If you take states values as argument in your function before setting then it will take naturally previous values
    setProps(props);
  }, [props]);

You are pretty close to solution with your edit. You should use props as callback in useEffect and you should set states inside useEffect with props values. Keep in mind useEffect is not async so you need to create async functions inside to use my logic

const [_props, setProps] = useState(props);
  

  useEffect(() => {
    //Here do your logic but set always to states, if you need create new states
    //If you take states values as argument in your function before setting then it will take naturally previous values
    setProps(props);
  }, [props]);
夏の忆 2025-01-31 05:16:29

从评论和答案中获取建议,我决定进行以下操作:

  const prevProps = useRef<MyComponentProps>(props);

  useEffect(() => {
    addListenersFromProps(layer, props, {
      filter: ({ prop }) =>  !prevProps.current[prop],
    });
    removeListenersFromProps(layer, prevProps.current, {
      filter: ({ prop }) => !props[prop],
    });

    // update props
    prevProps.current = props;

  }, [props);


Taking advice from both comments and answers I decided to do the following:

  const prevProps = useRef<MyComponentProps>(props);

  useEffect(() => {
    addListenersFromProps(layer, props, {
      filter: ({ prop }) =>  !prevProps.current[prop],
    });
    removeListenersFromProps(layer, prevProps.current, {
      filter: ({ prop }) => !props[prop],
    });

    // update props
    prevProps.current = props;

  }, [props);


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文