Git Product home page Git Product logo

sors's Introduction

Simple Observable Reactjs Store (SORS)

Simple Observable Reactjs Store is a simple library to manage reactjs store. It uses HOC and javascript observable pattern. It can be usefull if you looking for a simple way to manage your reactjs app.

NPM JavaScript Style Guide

Install

npm install --save sors

Usage

Simple Example

counter.js

import React, { Component } from 'react'

import { InjectStore, Store } from 'sors'

const demoInitialState = {
  count: 0,
}

export const counterStore = new Store('demo', demoInitialState)

class Counter extends Component {
  constructor(props) {
    super(props);
    this.onClick = this.onClick.bind(this)
  }

  onClick() {
    let val = Number(this.props.getStore(appStore, 'id'));
    val += 1;
    this.props.setStore(appStore, 'id', val)
  }

  render() {
    return (
      <div style={{width: '100%', height: '300px'}}>
      <button onClick={this.onClick}>Increase</button>
      </div>
    )
  }
}

export default InjectStore(Counter, counterStore)

example.js

import React, { Component } from 'react'

import { InjectStore, Store } from 'sors'

import { counterStore } from './counter'

export const mainStore = new Store('main', {})

class Example extends Component {
  constructor(props) {
    super(props);
    this.state = {
      counter: props.getStore(counterStore, 'count')
    }
    this.onChange = this.onChange.bind(this)
    props.onStore(counterStore, 'count', this.onChange)
  }

  onChange(newVal) {
    this.setState({ counter: newVal })
  }

  render() {
    return (
      <div style={{width: '100%', height: '300px'}}>{this.state.counter}</div>
    )
  }
}

export default InjectStore(Example, mainStore, )

API

props.getStore(store, node): used to get node value from store

props.setStore(store, node, data): used to set node value = data in store

props.on(node, fn): subscribe "fn" to get updated value when node(s) in the current store changes

props.onStore(store,node, fn): subscribe "fn" to get updated value when node(s) in store changes

License

MIT © Mojahed Elbakre

sors's People

Contributors

mmmmoj 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.