All Projects → skrivle → react-stateful-component

skrivle / react-stateful-component

Licence: MIT License
Functional stateful React components with sideEffect support

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-stateful-component

Redux Orm
A small, simple and immutable ORM to manage relational data in your Redux store.
Stars: ✭ 2,922 (+15278.95%)
Mutual labels:  state-management, reducer
Redhooks
Predictable state container for React apps written using Hooks
Stars: ✭ 149 (+684.21%)
Mutual labels:  state-management, reducer
Redux Machine
A tiny library (12 lines) for creating state machines in Redux apps
Stars: ✭ 338 (+1678.95%)
Mutual labels:  state-management, reducer
react-wisteria
Managing the State with the Golden Path
Stars: ✭ 18 (-5.26%)
Mutual labels:  functional, state-management
redux-leaves
Write once. Reduce anywhere.
Stars: ✭ 52 (+173.68%)
Mutual labels:  state-management, reducer
React Recomponent
🥫 Reason-style reducer components for React using ES6 classes.
Stars: ✭ 272 (+1331.58%)
Mutual labels:  state-management, reducer
Juicr.js
A simple (and tiny <1kb) redux inspired reducer for handling state changes.
Stars: ✭ 102 (+436.84%)
Mutual labels:  state-management, reducer
k-ramel
State manager for your components apps, the safe and easy way
Stars: ✭ 20 (+5.26%)
Mutual labels:  state-management, side-effects
flow-state
UI state management with RxJS.
Stars: ✭ 33 (+73.68%)
Mutual labels:  state-management, reducer
redux-interactions
Composing UI as a set of interactions
Stars: ✭ 22 (+15.79%)
Mutual labels:  state-management, reducer
Use Substate
🍙 Lightweight (<600B minified + gzipped) React Hook to subscribe to a subset of your single app state.
Stars: ✭ 97 (+410.53%)
Mutual labels:  state-management, reducer
riduce
Get rid of your reducer boilerplate! Zero hassle state management that's typed, flexible and scalable.
Stars: ✭ 14 (-26.32%)
Mutual labels:  state-management, reducer
grand central
State-management and action-dispatching for Ruby apps
Stars: ✭ 20 (+5.26%)
Mutual labels:  functional, state-management
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-21.05%)
Mutual labels:  functional, reducer
RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (+242.11%)
Mutual labels:  state-management, reducer
scala-iso
ISO 3166-1, ISO 3166-2, ISO 4217, E.164, ISO related types in Scala. Country codes, Country Subdivision, Country Currency, Calling Code, etc...
Stars: ✭ 34 (+78.95%)
Mutual labels:  functional
react-mui-pro-starter
Mix of Create React App and Material UI with set of reusable components and utilities to build professional React Applications faster.
Stars: ✭ 14 (-26.32%)
Mutual labels:  state-management
cra-redux-boilerplate
⚛️🔨create-react-app application with redux and another cool libraries to make your life easier.
Stars: ✭ 15 (-21.05%)
Mutual labels:  reducer
validation
Validation in Ruby objects
Stars: ✭ 18 (-5.26%)
Mutual labels:  functional
micro-observables
A simple Observable library that can be used for easy state management in React applications.
Stars: ✭ 78 (+310.53%)
Mutual labels:  state-management

React Stateful Component

Build Status

Create stateful React components in a functional way, with side effect support. Heavily inspired by ReasonReact's api.

  • Uses a reducer to manage state
  • The reducer can schedule side effects following the same pattern as Elm and Reason-React
  • Side effects are run outside of the component, meaning you can test your components without having to execute side effects
  • life cycle hooks
  • Subscriptions to handle communication with the "outside world"
  • Static type checking with Flow

> React Stateful Component Documentation

> TodoMVC example

Making the case for functional stateful components.

State management

React class components aren't always easy to keep maintainable, and it gets harder as the amount of state the component is managing grows. As setState() calls get spread across multiple methods, it becomes more difficult to have a good understanding of what state changes are happening within the component.

Redux has provided us with a very good solution to this problem. It uses pure reducer functions and a store to centralise your state management. However Redux also comes with some downsides when working on larger apps.

When building larger apps, you typically want to separate your app into multiple smaller apps or features. Each of these mini apps having their own responsibilities and thus their own state to manage. While Redux provides us with the ability to have separate state branches for these individual mini apps, the state still lives in the same single store. This can sometimes lead to state that gets shared and actions that get reduced by multiple mini apps, creating a dependency between different sub sections of your app. Of course this isn't always a downside, sometimes it's even desirable, but in some situations you want to enforce that separation a little bit more.

React Stateful Component allows you to do that because it's state is scoped to the component (it's just a React class component behind the scenes).

Another benefit of using component scoped state is that it can be wired on the fly. As features may live in different sub sections of your app, you don't want to wire all of your state up front. Instead you want it to be available whenever you need it. Having the state scoped to the component also helps with code splitting.

Side effects

In order to keep a component clean and testable we want to push everything that isn't related to reducing actions and managing state into a side effect. By doing this you can easily unit test a component without having to worry about api calls, timeouts, web sockets, etc ... This mean you can unit test your reducer and verify the state that is returned from it, or check if a certain action triggers the right side effect. But this also means that you can test the complete component using a tool like Airbnb's enzyme, without having to worry about the side effects.

React Stateful Component uses a SideEffectProvider to run side effects. This means that a component will never execute side effects itself it will only schedule them.

Using the MockSideEffectProvider provided by react-stateful-component-tools, you can intercept scheduled side effects, and if needed dispatch to desired actions in your tests.

Like in Elm and ReactReason, side effects are scheduled from within the reducer. Side effect are regular functions receiving reduce() as a parameter. Meaning you don't have to deal with complex side effect models.

Code sample

import React from 'react';
import ReactDOM from 'react-dom';
import createComponent, { update, SideEffectProvider } from 'react-stateful-component';

// Actions
const add = () => ({ type: 'ADD' });
const subtract = () => ({ type: 'SUBTRACT' });

const Counter = createComponent(() => ({
    initialState: () => ({
        counter: 0
    }),
    reducer: (state, action) => {
        const { counter } = state;

        switch (action.type) {
            case 'ADD':
                return update.state({ counter: counter + 1 });
            case 'SUBTRACT':
                return update.state({ counter: counter - 1 });
            default:
                return update.nothing();
        }
    },
    render: ({ state: { counter }, reduce }) => (
        <div>
            <button onClick={() => reduce(add())}>+</button>
            <span>{counter}</span>
            <button onClick={() => reduce(subtract())}>-</button>
        </div>
    )
}));

ReactDOM.render(
    <SideEffectProvider>
        <Counter />
    </SideEffectProvider>,
    document.getElementById('app')
);

Examples

An example directory is included in this repo. You can check it out locally by running:

git clone https://github.com/vejersele/react-stateful-component.git
cd react-stateful-component
npm install
npm run storybook

this will start storybook on http://localhost:6006/

Contributing

feel free to open issues and pr's!

running tests:

npm test

Run these two commands in two separate terminal instances to re-run tests on each change:

npm run build:watch
npm run test:watch

Inspiration

https://reasonml.github.io/reason-react/

http://elm-lang.org/

https://redux.js.org/

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].