Closed MicahZoltu closed 1 year ago
Yup, that's the correct and expected behavior. Creating component functions inside another component is a big no no, as this will create a fresh new function reference on every single render. When the framework compares the component during rendering it sees that the function reference has changed which means it's a different component. They just happen to have the same function body, but are completely different functions. You can check that in the console yourself:
const a = () => {};
const b = () => {};
console.log(a === b); // returns false
So yeah, this is the expected behavior with any framework that is based around diffing like virtual-dom based ones. You can fix this by not creating components definitions inside components.
Ah, I see. Thanks! I didn't realize two things:
function a() {
function b() {}
return b
}
a() === a() // false; I thought this was true
Describe the bug Local function components do not persist across re-renders.
To Reproduce
https://codesandbox.io/s/eager-paper-3zy0tl?file=/index.js
Steps to reproduce the behavior:
Increment
button repeatedly.Inner
andMyComponent
gets re-initialized every time you click the button.Inner
function to a file-level function rather than a local function.Inner
and instead inline the component directly intoOuter
.Expected behavior Local function components are correctly re-used on each re-render.
Notes