Skip to content

allain/observable-react-state

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

observable-react-state

An observable state implementation based on React's Component State approach.

NPM

Install

npm install --save observable-react-state

Usage

// @flow

import ObservableReactState from 'observable-react-state'

// Flow type applied to state property
type CounterStateType = {
  count: number
}

class CounterState extends ObservableReactState<CounterStateType> {
  state = {
    count: 0
  }

  increment() {
    // setState with an object
    this.setState({count: this.state.count + 1})
  }

  decrement() {
    // setState with a function
    this.setState(state => ({...state, count: state.count - 1}))
  }
}

const counter = new CounterState()

// Because it's an observable
counter.subscribe(() => console.log('count', counter.state.count)

counter.increment()
counter.decrement()

License

ISC © allain

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published