jotaijs / jotai-scope

MIT License
55 stars 4 forks source link

Proposal: Enhance ScopeProvider to allow selective store management #42

Closed moriyuu closed 2 weeks ago

moriyuu commented 2 months ago

Proposed Change

Enhance the ScopeProvider to allow selective store management. Specifically, add a store prop to ScopeProvider that would only apply to the atoms specified in the atoms prop. Atoms not listed would be managed by the parent store or the default store.

import { createStore } from 'jotai';
import { ScopeProvider } from 'jotai-scope';

const myStore = createStore();

function App() {
  return (
    <ScopeProvider atoms={[atom1, atom2]} store={myStore}>
      <ChildComponents />
    </ScopeProvider>
  );
}

Motivation

In complex applications, there's often a need to manage specific atoms in a custom store while leaving others to be managed by a parent or default store. The current implementation of using Provider and ScopeProvider together doesn't achieve this selective management.

For example, the following doesn't work as desired:

const storeMap = new Map<string, ReturnType<typeof createStore>>();
const getStore = (storeKey: string) => {
  if (storeMap.has(storeKey)) {
    return storeMap.get(storeKey);
  }
  const store = createStore();
  storeMap.set(storeKey, store);
  return store;
};

...

const myStore = getStore(myKey)

<Provider store={myStore}>
  <ScopeProvider atoms={[atom1, atom2]}>
    ...
  </ScopeProvider>
</Provider>

In this setup, all atoms are managed by myStore, not just atom1 and atom2.

dmaskasky commented 1 month ago

Would https://github.com/pmndrs/jotai/issues/2652 address your use case?

dmaskasky commented 1 month ago

manage specific atoms in a custom store

Following up on this, what does this management mean?

dmaskasky commented 2 weeks ago

Closing this ticket due to lack of activity. We can reopen if there is renewed interest.