Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do React Hooks use more memory than Class Components?

My understanding is that Javascript classes store their methods on the Class prototype and therefore all Class instances use the same function definition in memory when invoking those methods. i.e. an single function definition in memory is used by every instance.

For React Hooks, a functional component can update state through the function returned by useState(). e.g.

import React, { useState } from 'react'

function MyComponent(){
    const [greeting, setGreeting] = useState("Hello")

    return <h1>{greeting}</h1>
}

If my application was to render 100 MyComponents, would the setGreeting() function in all 100 of the components refer to the same setGreeting() function in memory or would there be 100 copies of the same function in memory?

like image 799
blackhaj Avatar asked Aug 15 '20 21:08

blackhaj


People also ask

Which is better React Hooks or class component?

Hooks allow you to use local state and other React features without writing a class. Hooks are special functions that let you “hook onto” React state and lifecycle features inside function components. Important: React internally can't keep track of hooks that run out of order.

How does the performance of using Hooks compare to that of classes?

With the introduction of Hooks, you can now do everything you would normally do in a class, inside a functional component. This is a game-changer because you have fewer concepts to learn and fewer lines of code to write too.

Should I always use React Hooks?

Instead, always use Hooks at the top level of your React function, before any early returns. By following this rule, you ensure that Hooks are called in the same order each time a component renders. That's what allows React to correctly preserve the state of Hooks between multiple useState and useEffect calls.

Why are React Hooks better than the alternatives?

Though rumored to do so in the community, classes are not being replaced in React. Rather, Hooks can work more efficiently with the same React concepts, allowing users to access props, state, context, refs, and lifecycle with a functional approach. It is not required to refactor class components into functional ones.


1 Answers

No, for 100 Components it will be 100 setGreeting would be created.SetGreeting is refrence to the function. So there will be 100 refrences.

Please refer below sandbox link: https://codesandbox.io/s/eager-kowalevski-x20nl

Explanation:

In below code I am storing references to setName function, just to verify whether it's same function or not. I am storing in two variables at window level. If the first variable is being stored, I store it in second one so that later I can compare. When I compare these two, they are different. Not a single occurence I get the console message saying "true". So each time a different function is being created.

import React, { useState } from "react";
import "./styles.css";

export default function App() {
  const [name, setName] = useState("asutosh");
  if (window.s1) {
    window.s2 = setName;
  } else {
    window.s1 = setName;
  }
  console.log(window.s1 === window.s2);

  return (
    <div className="App">
      <h1>Hello {name}</h1>
    </div>
  );
}
like image 52
Asutosh Avatar answered Sep 30 '22 07:09

Asutosh