GitXplorerGitXplorer
g

react-stateful

public
84 stars
4 forks
0 issues

Commits

List of commits on branch master.
Unverified
ed07559c8c53e8672d8e8028fe37e837dd8aecc1

Update README.md

ggaearon committed 9 years ago
Unverified
e7610aa76f55299eea488aafe542c53e8d812140

Update README.md

ggaearon committed 9 years ago
Unverified
4377acc28ce10e61f8bccde5b4484b136798ce46

Update README.md

ggaearon committed 9 years ago
Unverified
fa6ab85d0ba0c655675282b59e0e2607d70c3755

Update README.md

ggaearon committed 9 years ago
Unverified
b7b639bc99a11d1774254668b0cd51752cdbee06

1.0.1

ggaearon committed 9 years ago
Unverified
7c75287444e4588d4eebf3f47b1732e3d34ed922

Fix missing React import

ggaearon committed 9 years ago

README

The README file for this repository.

No Maintenance Intended

No Maintenance Intended

This project is no longer maintained. It does its job, but there are no plans to extend or change it.
We don’t recommend you to depend on it, but you are free to reimplement a similar idea if you’d like.

react-stateful

The last enemy that shall be destroyed is this.
— An old JavaScript tutorial

A higher-order React component for lifting simple state and keeping your components pure.

This API is tiny and experimental.
It may change a lot, or I may abandon it in the future. Feel free to play with it now!

Why

You want to move the state out of your own components and rely on props as much as possible.
You want to keep all possible mutations of your component's state declarative, co-located, and named.

Why Not

This won't help you with data subscriptions a la Flux.
We're only talking about local component UI state.
This also won't work with props triggering a state change (at least, not yet).

Inspiration

This repo was inspired by react-controllables and Reduce State proposal from react-future.

Usage

This example assumes ES7 decorators (Babel supports them with "stage": 1). However they're easy and clean to write in desugared form, so try Babel REPL if you're curious.

import React, { PropTypes } from 'react';
import Stateful from 'react-stateful';

// So what's your state like?
const CounterState = {
  // like getInitialState
  initialize(props) {
    return {
      counter: 0
    };
  },

  // kinda like Flux actions
  reducers: {
    // how does your state change?
    increment(props, state) {
      return {
        counter: state.counter + 1
      };
    }
  }
};

@Stateful(CounterState) // injects state and reducers as props! component stays pure.
class Counter {
  static propTypes = {
    counter: PropTypes.number.isRequired,
    increment: PropTypes.func.isRequired
  };

  render() {
    const { counter, increment } = this.props;
    return <div onClick={increment}>{counter}</div>;
  }
}

export default class App extends React.Component {
  render() {
    return (
      <Counter />
    );
  }
}