react-state-class

0.0.3 • Public • Published

react-state-class

Build Status

The main idea is to decouple state logic from component code and simplify testing.

Installation

npm install --save react-state-class

Usage

import { StateContainer } from 'react-state-class'

export default class UserMenuState extends StateContainer {
  constructor({authenticated}) {
    super()

    this.authenticated = authenticated
  }

  get menuOptions() {
    return this.authenticated ? 'Log Out' : 'Press authenticate'
  }

  authenticate() {
    this.authenticated = true
    this.emit('change')
  }
}

Stateful component definition. Here we wrap our UserMenu in higher order component (HOC), which will provide all data and state handlers to UserMenu via props.

import { connect } from 'react-state-class'
import UserMenuState from './UserMenuState'

// Props may be given by react_compoment view helper.
// Object returned by this function will be used for
// state container initialization
const initState = props => ({
  authenticated: props.authenticated
})

// data getters
const mapStateToProps = stateContainer => ({
  menuOptions: stateContainer.menuOptions
})

// data setters
const mapStateHandlersToProps = stateContainer => ({
  authenticate: stateContainer.authenticate
})

const StatefulUserMenu = connect({
  initState: initState,
  stateHandler: UserMenuState
})(mapStateToProps, mapStateHandlersToProps)(UserMenu)

export default StatefulUserMenu

Using state containers makes our components really simple and state itself very testable.

const UserMenu = ({ menuOptions, authenticate }) => (
  <div>
    <div>
      { menuOptions }
    </div>
    <a onClick={authenticate}>
      Authenticate
    </a>
  </div>
)

export default UserMenu

Readme

Keywords

none

Package Sidebar

Install

npm i react-state-class

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

217 kB

Total Files

16

Last publish

Collaborators

  • dra1n