Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-state-class

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-state-class

A place for your react component state

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 08 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc