Git Product home page Git Product logo

Comments (6)

timkindberg avatar timkindberg commented on May 2, 2024 1

I'm thinking more if someone already had a hook written.

function useMyState() {
  const [state, dispatch] = userReducer(...)
  const actions = {...}
  // maybe some more logic
  return someApi
}

Then they could just make it "shared" state, meaning it could be used in multiple components. Like a higher-order hook.

export useMySharedState = share(useMyState)

Anyway, sorry to beat a dead horse :) THANK YOU for this library, we've started using it in our startup's site. It's going well so far!

from zustand.

JeremyRH avatar JeremyRH commented on May 2, 2024 1

This would be nice but I don't know if your example is possible. State is shared without using a provider by collecting the setState and dispatch functions in a list and calling them when needed. It is possible for someone to create a package that does this though:

import { useReducer, useState } from 'react';
import share from 'react-share-state';
export const useSharedReducer = share(useReducer);
export const useSharedState = share(useState);

from zustand.

drcmda avatar drcmda commented on May 2, 2024

well it doesn't need a provider because it's not context based. these hooks work everywhere, even outside of react bounds, like reconcilers. the hook you create already is a shared connector, that can be used anywhere in your app.

or maybe i don't understand it yet, could you go into shared state a little more deeper?

from zustand.

timkindberg avatar timkindberg commented on May 2, 2024

Sorry, let me try to explain again.

Your library is two things to me:

  1. A way to share state with many components without using Provider
  2. An elegant state API

1 is a worthwhile discovery on its own. 2 is also good because you value simple APIs. Each could have been built independently, with 2 being built using 1.

But what if someone loves reducers or something else? I know you allow middleware for API customization, but your lib still has opinions. It would be cool if there was a lower-level share higher-order hook that converted any state-based hook to a "shared" state-based hook, without the need to use a Provider. Then your opinionated API would be built on top of this share hook, as a higher-level API.

from zustand.

drcmda avatar drcmda commented on May 2, 2024

reducers are no problem though

const types {
  increase: "INCREASE",
  decrease: "DECREASE"
}

const reducer = (state, { type, ...payload }) => {
  switch (type) {
    case types.increase: return { ...state, count: state.count + 1 }
    case types.decrease: return { ...state, count: state.count - 1 } 
  }
  return state
}

const [useStore] = create((set, get) => ({
  count: 0,
  dispatch: args => set(state => reducer(state, args)),
})

Maybe give me some code examples, need to see it to imagine how it works.

from zustand.

JeremyRH avatar JeremyRH commented on May 2, 2024

I'm going to close this for now as it seems like this is something another library could solve.

from zustand.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.