简体   繁体   English

跨多个组件重用 React.useCallback() function

[英]Reuse React.useCallback() function across multiple components

I have a few components that all call the same function on an onPress handler, let's say it looks like the following:我有几个组件都在onPress处理程序上调用相同的 function,假设它如下所示:

function MyComponent () {
  const dispatch = useDispatch()

  const updateThing = React.useCallback((thingId: string) => {
    dispatch(someActionCreator(thingId))
    someGlobalFunction(thingId)
  }, [dispatch])

  return (
    <View>
      <NestedComponent onUpdate={updateThing} />
    </View>
  )
}

I want to move this function outside of the component so I can re-use it, thinking it would look something like this:我想将这个 function 移到组件之外,以便我可以重新使用它,我认为它看起来像这样:

const updateThing = React.useCallback(myFunction)

However, it has a dependency of dispatch that I need to pass in and add to the dependency array.但是,它有一个dispatch依赖项,我需要传入并添加到依赖项数组中。

How can I break this function out for reuse while also getting the performance gain from useCallback ?我怎样才能打破这个 function 以供重用,同时还能从useCallback获得性能提升?

You can write a custom hook like您可以编写一个自定义钩子,例如

export const useUpdateThinkg = () => {
  const dispatch = useDispatch()

  const updateThing = React.useCallback((thingId: string) => {
    dispatch(someActionCreator(thingId))
    someGlobalFunction(thingId)
  }, [dispatch])
  return { updateThing };
}

And then use it like然后像这样使用它

import { useUpdateThing } from 'path/to/updateThing'
function MyComponent () {
  const { updateThing} = useUpdateThing();

  return (
    <View>
      <NestedComponent onUpdate={updateThing} />
    </View>
  )
}

暂无
暂无

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

相关问题 如何使用 React.usecallback? - How to use React.usecallback? React.useCallback() 用于链式匿名函数 - React.useCallback() for chained anonymous functions 如何确定 React.useCallback 的依赖列表? - How to decide the dependency list for React.useCallback? TypeError: undefined is not a function(靠近'...(0,_react.useCallBack)...')。 我不知道我的代码中的 usCallBack() function 有什么问题 - TypeError: undefined is not a function (near '...(0, _react.useCallBack)...'). I don't know what's wrong with the usCallBack() function in my code 为什么 React.useCallback 触发重新渲染,你不应该? - Why does React.useCallback trigger rerender, thouh it should not? Should we use useCallback in every function handler in React Functional Components - Should we use useCallback in every function handler in React Functional Components 使用 React.memo 和 React.useCallback 防止重新渲染 - Prevent re-render using React.memo and React.useCallback Cube.js React Hook React.useCallback 缺少一个依赖项:'pivotConfig'。 + - Cube.js React Hook React.useCallback has a missing dependency: 'pivotConfig'. + 在组件道具中使用 React.useMemo 或 React.useCallback 好吗? - Is good to use React.useMemo or React.useCallback inside component props? React.useCallback:我是否需要将依赖项数组中的函数与依赖项一起传递? - React.useCallback: do I need to pass functions in array of dependencies along with dependencies?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM