简体   繁体   English

React hooks:回调作为 prop 在调用时根据它重新渲染每个钩子

[英]React hooks: callback as prop re-render every hooks depending on it when it's called

In the example below, I'm getting in a loop when I call the onError prop in fetchItems() .在下面的示例中,当我在fetchItems()中调用onError道具时,我陷入了循环。 I don't understand why, when called, it triggers hooks depending on it.我不明白为什么在调用时会根据它触发挂钩。 How can I fix this?我怎样才能解决这个问题? Thanks!谢谢!

const Component = ({onError}) => {
  const [items, setItems] = useState([]);
  const itemsRef = useRef(items);

  const fetchItems = useCallback(() => {
    const [first] = itemsRef.current;
    fetchNewItemsSince(first || 0).then((newItems) => {
      setItems((oldItems) => [...oldItems, ...newItems]);
    }).catch(onError);
  }, [onError]);

  // Update ref to dispose closure on `items` state
  useEffect(() => {
    itemsRef.current = items;
  }, [items]);

  // Call once on mount
  useEffect(() => {
    fetchItems();
  }, [fetchItems]);

  // Make an interval
  useEffect(() => {
    const id = setInterval(fetchItems, ONE_MINUTE);

    return () => {
      clearInterval(id);
    };
  }, [fetchItems]);
};

Try setting the initial state with a function尝试使用 function 设置初始 state

Const [foo, setFoo] = useState(() => 'foo') Const [foo, setFoo] = useState(() => 'foo')

Your useCallback for that state instance probably runs once, correct me if I'm wrong, so if you set a function for useState it will only run once, consider is a component did mount but no update.您针对该 state 实例的 useCallback 可能会运行一次,如果我错了请纠正我,因此如果您为 useState 设置 function 它只会运行一次,请考虑是否安装了一个组件但没有更新。

Maybe this is practice but I have never called something like [onError] without setting an error state, because that's what I think recognizes it.也许这是惯例,但我从来没有在不设置错误 state 的情况下调用 [onError] 之类的东西,因为我认为这是识别它的方式。

So React is this great thing that renders certain components etc. UseEffect is great, I personally don't use it to change the state, that for me is usually done in the JSX.所以 React 是渲染某些组件等的好东西。UseEffect 很棒,我个人不使用它来更改 state,对我来说通常是在 JSX 中完成的。

I would do like a onClick handler with the UseState method and watch for changes there.我想要一个带有 UseState 方法的 onClick 处理程序,并观察那里的变化。 Instead your running a function that runs a setState event and watches for an event.相反,您运行 function 运行 setState 事件并监视事件。

Let me know if that works or not if you need any explanation.如果您需要任何解释,请告诉我这是否有效。

As I said in the comments, onError is triggering a re-render on the parent and therefore the children will also render again.正如我在评论中所说, onError正在触发对父级的重新渲染,因此子级也将再次渲染。

Someone suggested removing onError from the useCallback array of dependencies.有人建议从依赖项的useCallback数组中删除onError Although it might work, it is considered a bad practice because can lead to memory-leak.虽然它可能有效,但它被认为是一种不好的做法,因为它会导致内存泄漏。 Have you tried to remove the useCallback wrap around your function?您是否尝试过删除useCallback周围的 useCallback 包装?

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

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