GithubHelp home page GithubHelp logo

vijaypemmaraju / statify Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 64 KB

React state management without all the boilerplate

HTML 14.51% JavaScript 71.91% CSS 13.58%
react reactjs state-management

statify's Introduction

Intro

Statify is a state management framework that keeps all state inside a single tree/store, but with as little boilerplate as possible.

Initialization

import React from 'react';
import ReactDOM from 'react-dom';
import { Map } from 'immutable'
import Todo from './Todo'
import './index.css';
import { initializeStatify } from './statify'

initializeStatify(new Map(), (stateTree, keyPath, updates) => stateTree.mergeIn(keyPath, updates))

ReactDOM.render(
  <Todo />,
  document.getElementById('root')
);

Usage

Todo.js

import React, { Component } from 'react';
import { Map, List } from 'immutable'
import './Todo.scss';
import TodoItem from './TodoItem'
import { statify } from './statify'
import TodoItemRecord from './models/TodoItemRecord'

@statify(
  // State definition (i.e. what will show up in this.props)
  (stateTree) => {
    return {
      items: stateTree.getIn(['items'], new List()),
      currentTextboxValue: stateTree.getIn(['currentTextboxValue'], '')
    }
  },
  // Updater methods. These return a new state tree and get hoisted to the component (this.updaters)
  (getStateTree) => {
    return {
      handleTodoInputChange: e => getStateTree().set('currentTextboxValue', e.target.value),
      addTodoItem: () => {
        const currentTextboxValue = getStateTree().getIn(['currentTextboxValue'], '')
        if (currentTextboxValue.length === 0) return getStateTree()
        return getStateTree().withMutations((stateTree) => {
          stateTree
            .updateIn(['items'], items => (items || new List()).push(new TodoItemRecord({ text: currentTextboxValue })))
            .set('currentTextboxValue', '')
        })
      },
      handleToggleCompleted: (index, e) => getStateTree().mergeIn(['items', index], { completed: e.target.checked }),
      removeTodoItem: index => getStateTree().updateIn(['items'], items => items.remove(index))
    }
  },
  // Keypath of the subtree that this component will use (i.e stateTree will be Root->Todo)
  ['Todo'],
  // How to navigate to the subtree given a keypath
  (stateTree, keyPath) => stateTree.getIn(keyPath, new Map())
)
class Todo extends Component {
  render() {
    return (
      <div className="App">
        {this.props.items.map((item, index) => {
          return (
            <TodoItem
              key={index}
              item={item}
              index={index}
              handleToggleCompleted={this.updaters.handleToggleCompleted}
              removeTodoItem={this.updaters.removeTodoItem}
            />
          )
        })}
        <input
          type="textbox"
          value={this.props.currentTextboxValue}
          onChange={this.updaters.handleTodoInputChange}
          onKeyPress={(e) => { if (e.key === 'Enter') { this.updaters.addTodoItem() } }}
        />
        <button onClick={this.updaters.addTodoItem}>Add</button>
      </div>
    );
  }
}

export default Todo;

TodoItem.js

import React from 'react';

const TodoItem = ({ item, index, handleToggleCompleted, removeTodoItem }) => {
  return (
    <div>
      <input type="checkbox" checked={item.completed} onChange={handleToggleCompleted.bind(this, index)} />
      {item.completed ? <strike>{item.text}</strike> : item.text}
      <button onClick={removeTodoItem.bind(this, index)}>Remove</button>
    </div>
  )
}

export default TodoItem

statify's People

Contributors

vijaypemmaraju avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.