With nested molecules and multiple ScopeProviders, the order that components are rendered changes the behavior in a strange way.
Reproduction on CodeSandbox
https://codesandbox.io/p/sandbox/focused-oskar-9z7ytd?file=%2FApp.tsx
Setup
const NameScope = createScope<string>("eve");
const NameMolecule = molecule((_mol, scope) => {
const name = scope(NameScope);
return atom(name);
});
const CombinedMolecule = molecule((mol) => {
const nameAtom = mol(NameMolecule);
return nameAtom;
});
function BaseCounter({ nameAtom }: { nameAtom: PrimitiveAtom<string> }) {
const [name, setName] = useAtom(nameAtom);
return (
<div>
<p>({name})</p>
<button onClick={() => setName((c) => c + 1)}>Update Name</button>
</div>
);
}
function Counter1() {
const nameAtom = useMolecule(CombinedMolecule);
return <BaseCounter nameAtom={nameAtom} />;
}
function Counter2() {
const nameAtom = useMolecule(NameMolecule);
return <BaseCounter nameAtom={nameAtom} />;
}
Bug
The first 2 names are synced together. The third and fourth names are not synced to anything.
{/******* Uncommenting this would cause the names to by synced *********/}
{/* <ScopeProvider scope={NameScope} value="bob">
<Counter1 />
</ScopeProvider> */}
<ScopeProvider scope={NameScope} value="bob">
<Counter2 />
</ScopeProvider>
<ScopeProvider scope={NameScope} value="bob">
<Counter2 />
</ScopeProvider>
<ScopeProvider scope={NameScope} value="bob">
<Counter1 />
</ScopeProvider>
<ScopeProvider scope={NameScope} value="bob">
<Counter1 />
</ScopeProvider>
Notes
- If the default value for NameScope is "bob", the names are all synced
- If you use one scope provider, the names are all synced
With nested molecules and multiple ScopeProviders, the order that components are rendered changes the behavior in a strange way.
Reproduction on CodeSandbox
https://codesandbox.io/p/sandbox/focused-oskar-9z7ytd?file=%2FApp.tsx
Setup
Bug
The first 2 names are synced together. The third and fourth names are not synced to anything.
Notes