Why is the molecule's value constructor called every time useMolecule
is evaluated?
#40
-
Hi folks, I'm just getting started with Bunshi, and I'm a little confused by the behavior I'm seeing. If I create a molecule with no molecule or scope dependencies: const expensiveObjMol = molecule(() => {
// expensive object creation
// ...
return expensiveObj
}) And then I use that with: export const MyComponent = () => {
const expensiveObj = useMolecule(expensiveObjMol);
return (
// ...
);
}; My observation is that the molecule value constructor function is called every time the My expectation would be that a dependency injection framework would avoid doing the work of creating new objects when they wouldn't be used. Is my expectation wrong for Bunshi? Do I have the facts about the behavior right? What am I missing? Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @mfurtak I believe that this was reported as a bug in #39 and has been resolved in Bunshi 2.1.0 There are also new lifecycle hooks ( |
Beta Was this translation helpful? Give feedback.
Hi @mfurtak I believe that this was reported as a bug in #39 and has been resolved in Bunshi 2.1.0
There are also new lifecycle hooks (
onMount
andonUnmount
) to do more fine-grained control of memory-related operations.