Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Enhance ScopeProvider to allow selective store management #42

Closed
moriyuu opened this issue Jul 11, 2024 · 3 comments
Closed
Assignees

Comments

@moriyuu
Copy link

moriyuu commented Jul 11, 2024

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
Copy link
Member

Would pmndrs/jotai#2652 address your use case?

@dmaskasky dmaskasky self-assigned this Aug 1, 2024
@dmaskasky
Copy link
Member

dmaskasky commented Aug 1, 2024

manage specific atoms in a custom store

Following up on this, what does this management mean?

@dmaskasky
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants