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

Add Combine Store Feature #301

Closed
wants to merge 1 commit into from
Closed

Add Combine Store Feature #301

wants to merge 1 commit into from

Conversation

ragokan
Copy link

@ragokan ragokan commented Feb 9, 2021

I made a way to combine unlimited amount of stores to create one base store. I tested it, everything works amazing, even the setState function. Yet, as you can see from the code, it can't totally infer the types if we don't provide an interface. I hope it can be fixed.

interface IAge extends State {
  age: number
}

interface IName extends State {
  name: string
}

interface IHealth extends State {
  status: boolean
}

const ageState = create<IAge>(() => ({ age: 5 }))
const nameState = create<IName>(() => ({ name: 'zustand' }))
const healthState = create<IHealth>(() => ({ status: true }))

interface combined extends IAge, IName, IHealth {}

const combined = combineStores<combined>(nameState, ageState, healthState)

console.log(combined)
console.log(combined.getState())
console.log(combined.getState().name)

Console Results:
image

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 9, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit e02dcc6:

Sandbox Source
React Configuration
React Typescript Configuration

@dai-shi
Copy link
Member

dai-shi commented Feb 9, 2021

Hey, thanks for your interest and your trial.
Non technical issue: We already have something called "combine", so maybe we need to have a different name to avoid confusion.

Looking at the code, it's not very useful as is. What is nice is to accept set in the sub stores.

So, it's more like this:

combineStores({ name: nameState, age: ageState, health: healthState })

We have some recent discussions in #291. You want to take a look at #178 especially.
(Hm, analogous to combineReducers, combineCreators may work. Now, I regret naming the bare combine...)
Yeah, combining creators sounds better that combining stores.

TypeScript support is very important, I guess. Would you see how combine works?

Hope you get some ideas.

@ragokan
Copy link
Author

ragokan commented Feb 10, 2021

Hey, thanks for your interest and your trial.
Non technical issue: We already have something called "combine", so maybe we need to have a different name to avoid confusion.

Looking at the code, it's not very useful as is. What is nice is to accept set in the sub stores.

So, it's more like this:

combineStores({ name: nameState, age: ageState, health: healthState })

We have some recent discussions in #291. You want to take a look at #178 especially.
(Hm, analogous to combineReducers, combineCreators may work. Now, I regret naming the bare combine...)
Yeah, combining creators sounds better that combining stores.

TypeScript support is very important, I guess. Would you see how combine works?

Hope you get some ideas.

Thank for your comment, I will try to implement this with a better, other than that, I will try to help others who are trying the same thing, you can close this code if you want.

@dai-shi
Copy link
Member

dai-shi commented Feb 11, 2021

Let me close this. Would be better to keep discussion in #178.

@dai-shi dai-shi closed this Feb 11, 2021
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

Successfully merging this pull request may close these issues.

2 participants