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?