简体   繁体   English

您将如何从头开始使用 useState 实现 useCallback?

[英]How would you implement useCallback using useState from scratch?

This is how I have implemented it.这就是我实施它的方式。 Does this look ok?这看起来好吗?

function useCallback(fn, deps) {
    const [state, setState] = useState({ fn });

    useEffect(() => {
        setState({ fn });
    }, deps);

    return state.fn;
}

Nice try but not perfect.不错的尝试,但并不完美。 Your implementation calls setState() which triggers another round of re-render of component, that doesn't match up to the behavior of the real useCallback() hook.您的实现调用setState()触发另一轮重新渲染组件,这与真正的useCallback()钩子的行为不匹配。

OK challenge accepted. OK 接受挑战。

Actually useState() isn't a good building block for implementing useCallback() , useRef() is better.实际上useState()不是实现useCallback()的好构建块, useRef()更好。 But to meet your requirement, lemme first write useRef() with useState() .但是为了满足您的要求,让我先用useRef()编写useState()

function useRef(value) {
  const [ref] = useState({ current: value })
  ref.current = value
  return ref
}

Easy piecy.易碎。 We don't need useEffect() .我们不需要useEffect() All we want is just a function to compare the deps between re-render and see if they change.我们想要的只是一个函数来比较重新渲染之间的深度并查看它们是否发生变化。

function depsChanged(deps1, deps2) {
  if (deps1 === undefined || deps2 === undefined) return true
  if (deps1.length !== deps2.length) return true
  for (let i in deps1) {
    if (!Object.is(deps1[i], deps2[i])) return true
  }
  return false
}

Now we can implement useCallback()现在我们可以实现useCallback()

function useCallback(fn, deps) {
  const slots = useRef([fn, deps]).current
  if (depsChanged(slots[1], deps)) {
    slots[0] = fn
  }
  slots[1] = deps
  return slots[0]
}

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

相关问题 使用 useCallback 从 useState 发送 setState 作为道具 - Send setState from useState as a props using useCallback 将“useCallback”用于回调引用时,您将如何正确设置 TypeScript 声明? - How would you correctly set up a TypeScript declaration when using "useCallback" for a callback ref? 你可以在单个功能组件中多次调用 React 的 useState 和 useCallback 吗? 如果是这样,它是如何工作的? - Can you call React's useState and useCallback multiple times in a single functional component? If so, how does it work? 你将如何在 React 中使用 useEffect 实现 shouldComponentUpdate? - How would you implement shouldComponentUpdate using useEffect in React? “useState”不能在回调中调用。 如何在“useCallback”上使用 state? - “useState” cannot be called inside a callback. How to use state on “useCallback”? 当 useCallback 缺少依赖时 React useState 和 useCallback hook 如何工作 - How does React useState and useCallback hook work when useCallback lacks dependencies 您将如何在 Reactjs 中实现以下内容 - How would you implement the following in Reactjs 如何实现useState function? - How to implement useState function? 如何从零开始为 React 实现自己的 useMemo - How to implement your own useMemo from Scratch, for React 我对如何使用 useState 推送到数组感到困惑? - I am confused on how I would push to an array using useState?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM