Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Hooks - How to avoid redeclaring functions on every render

On a react class I would write something like this

class Myclass extends React.Component {
  handleUpdate = info => {
    //do the update
  }

  render() {
    return (
      <SomeMarkup>
        <SomeComponent onUpdate={this.handleUpdate} />
      </SomeMarkup>
    )
  }
}

If using a function I could just write the following

function MyFunction() {
  function handleUpdate(info) {
    // do the update
  }
  return (
    <SomeMarkup>
      <SomeComponent onUpdate={handleUpdate} />
    </SomeMarkup>
  )
}

...but with that I'd be redeclaring a function on every render. Is there any sort of trick that would memoize the handler function between renders? Or should I just move the handler out of the render scope? (Moving it out of the render scope requires me that I explicitly pass more parameters since I wont directly have access to the function scope.)

like image 334
wkrueger Avatar asked Feb 28 '19 22:02

wkrueger


People also ask

How do you prevent functional components from rendering?

But, is there an option to prevent re-rendering with functional components? The answer is yes! Use React. memo() to prevent re-rendering on React function components.

How do you prevent unnecessary rendering in React functional component?

1. Memoization using useMemo() and UseCallback() Hooks. Memoization enables your code to re-render components only if there's a change in the props. With this technique, developers can avoid unnecessary renderings and reduce the computational load in applications.

How do you prevent re-rendering of components that have not changed?

Preventing Re-Renders: The Old Way To prevent the render method from being called, set the return to false, which cancels the render. This method gets called before the component gets rendered. Sometimes you may want to prevent re-render even if a component's state or prop has changed.

How do you avoid multiple render in React?

React shouldComponentUpdate is a performance optimization method, and it tells React to avoid re-rendering a component, even if state or prop values may have changed. Only use this method if when a component will stay static or pure. The React shouldComponentUpdate method requires you to return a boolean value.

How to prevent re-renders on react functional components with react?

How to prevent re-renders on React functional components with React.memo () If you’re using a React class component you can use the shouldComponentUpdate method or a React.PureComponent class extension to prevent a component from re-rendering. But, is there an option to prevent re-rendering with functional components? The answer is yes!

How to avoid conditional rendering of react hooks?

Here’s a good practice that helps to avoid conditional rendering of hooks: Execute the hooks at the top of the component body, the conditional rendering logic move to the bottom. eslint-plugin-react-hooks can also help you enforce the correct hooks execution order. 2.

What is the render method in react?

The render method is required whenever you’re creating a new React component. React render requires you to return a value. This may either be null, undefined or JSX markup.

Are hooks the only way to write React components?

Whether a component is a class or a function that uses Hooks is an implementation detail of that component. In the longer term, we expect Hooks to be the primary way people write React components. Do Hooks cover all use cases for classes? Our goal is for Hooks to cover all use cases for classes as soon as possible.


1 Answers

This is exactly the scenario that useCallback is for. The function still gets declared every time with useCallback, but the function returned is memoized so that if it is passed as a property to children, the children will receive a consistent function unless it has to change due to dependencies changing.

Please see my recent related answer here that demonstrates in detail how useCallback works: Trouble with simple example of React Hooks useCallback

Here's another related answer: React Hooks useCallback causes child to re-render

like image 187
Ryan Cogswell Avatar answered Oct 16 '22 06:10

Ryan Cogswell