Giter VIP home page Giter VIP logo

redux-xstate-poc's Introduction

XState Redux

Manage your Redux side effects with XState. Use 100% of XState's features.

export const counterSlice = createXStateSlice({
  // Pass in a unique, descriptive name for the slice
  name: "counter",
  // Pass in the machine
  machine: counterMachine,
  /**
   * Get the state we want to pass from the machine
   * to Redux.
   */
  getSelectedState: (state) => {
    return {
      count: state.context.count,
    };
  },
});

export const store = configureStore({
  reducer: {
    counter: counterSlice.reducer,
  },
  middleware: [createXStateMiddleware(counterSlice)],
});

Getting Started

  1. See lib/counterSlice.ts to see how to create an 'XState' slice
  2. See lib/store.ts to see how to configure your Redux store to use XState.
  3. Try creating your own slice! You can use any XState feature.

Features

Use statecharts as reducers: Call createMachine, then pass it into createXStateSlice, and you're good to go. You can use 100% of XState's features right alongside Redux.

Use sendParent to send events to Redux: The Redux store is your machine's 'parent', meaning you can call sendParent to send actions to Redux. In future versions of XState, you'll be able to call parent.getSnapshot() to grab the Redux store's state.

Colocate side effects with your Redux logic: No more splitting out side effects from your app logic. Use invoke, actions and even spawn actors in your XState machines.

redux-xstate-poc's People

Contributors

mattpocock avatar mwarger avatar

Stargazers

Valentin Popescu avatar Morteza avatar Rexford Essilfie avatar Albert Muravtsev avatar Valkouski Anton avatar Nathan Jones avatar Andrew avatar Brandon Young avatar Mahmoud Ashraf avatar Federico Mastrini avatar  avatar Sam Jakos avatar saebyn avatar brandon flowers avatar Carlos Ricardo Ziegler avatar  avatar Jim Deola avatar Brian Curliss avatar Rodrigo Fernandes avatar Ivan Brajković avatar Aro ANDRIAMARO avatar Michael Lefebvre avatar  avatar Andrey avatar Jack Moore avatar Thales avatar Andrey Bakuta avatar Mikey avatar Simon Tegg avatar  avatar Lewis Liu avatar  avatar Adam Zell avatar sudo rm -rf --no-preserve-root / avatar sam bacha avatar Andy Hiew avatar Károly Török avatar Akhil Sai Ram avatar JKPark avatar David Meade avatar Calin Ennis avatar Vladimir Milojevic avatar Hannu Pousi avatar Talysson de Oliveira Cassiano avatar Maciej Leszczyński avatar Bernardo Raposo avatar Dan Couper avatar  avatar meluskyc avatar  avatar Chris Lonardo avatar Ragan Webber avatar Kohwo Orien avatar  avatar Nate Higgins avatar Chris Lonardo avatar  avatar Olga Streltsova avatar Cameron Yick avatar Jatin Sandilya avatar Felipe Gustavo avatar  avatar david harvey avatar Asher Cohen avatar  avatar Raymond Tijhaar avatar  avatar Andrejs Agejevs avatar Koen Romers avatar Nick de Rozarieux avatar Alexey avatar Efrem Ropelato avatar Deyan Atanasov avatar Bojan Matić avatar Kyle Chamberlain avatar Eric Pharr avatar Radu Cugut avatar Jesus Franco avatar Tony Seing avatar Francois KERISIT avatar Matteo Ronchi avatar Aaron Dancer 傅子威 avatar Hyeseong Kim avatar Sung Jeon avatar  avatar Arut Jinadit avatar Michael Carter avatar danielsdesk avatar  avatar

Watchers

saebyn avatar James Cloos avatar Valentin Popescu avatar  avatar  avatar

redux-xstate-poc's Issues

Benefits?

Really interesting approach. I will ask what is the benefit using like that ?

Thanks

Update for XState v5

I would do this myself, but I didn't see an equivalent of parent: { send: ..., getSnapshot: ... } in the v5 API.

Happy to help out otherwise!

Error when invoking a service on initial state

When invoking a service on the initial state of a machine, I see this error:

Error: Dispatching while constructing your middleware is not allowed. Other middleware would not be applied to this dispatch.

image

I have a reproduction here:
https://github.com/mwarger/redux-xstate-poc/blob/main/lib/counterSlice.ts

I have updated the counterSlice example to consist of the authentication machine from xstate catalogue. I have also reproduced this in other machines, and it appears that if you invoke anything in the initial state you will see the error by redux.

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.