Reuse React.useCallback() function across multiple components

Alex

I have a few components that all call the same function on an onPress handler, let's say it looks like the following:

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:

const updateThing = React.useCallback(myFunction)

However, it has a dependency of dispatch that I need to pass in and add to the dependency array.

How can I break this function out for reuse while also getting the performance gain from useCallback?

Shubham Khatri

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>
  )
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Reuse a function across multiple UIViewControllers

Reuse a function across multiple structs to satisfy an interface

Reuse a function in Go across multiple structs

How can I reuse variable values across components in react

Should we use useCallback in every function handler in React Functional Components

Sharing a JavaScript class across multiple React components

Send multiple props across components React

React multiple useEffect with useCallback

ReactJS useCallback not a function of react?

Reuse UICollectionView across multiple UICollectionViewControllers

How to reuse angular observable across multiple components? (To avoid having to duplicate a request twice?)

Can I reuse components across apps?

Reuse a 'mixin' with Styled Components across different files?

How to implement a gameloop with requestAnimationFrame across multiple React Redux components?

React setState in function or setState in useCallback

Return multiple react components from a javascript function

React -- function called on multiple incorrect components

React pass callback function to multiple child components

Passing FormGroup across multiple components

Multiple DataSources across 2 components

React useCallback: useCallback is still re-rendering function each time

React sharing method across components

Using props in React with styled components across components

How to move react event handlers to separate file ,export and then import for reuse in multiple different functional components?

React hook useCallback to avoid multiple renders

Vue 3 Composition API reuse in multiple components

Is there a way to render multiple React components in the React.render() function?

React useCallback function runs in infinite loop

How to use useCallback hook with onChange function REACT