All Projects → Niryo → Controllerim

Niryo / Controllerim

Licence: mit
A state management library for React

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Controllerim

Hgplaceholders
Nice library to show placeholders and Empty States for any UITableView/UICollectionView in your project
Stars: ✭ 2,048 (+861.5%)
Mutual labels:  state-management
Statefulviewcontroller
Placeholder views based on content, loading, error or empty states
Stars: ✭ 2,139 (+904.23%)
Mutual labels:  state-management
Laco
⚡️Ultra lightweight state management for React and Inferno
Stars: ✭ 203 (-4.69%)
Mutual labels:  state-management
Graphql Normalizr
Normalize GraphQL responses for persisting in the client cache/state
Stars: ✭ 175 (-17.84%)
Mutual labels:  state-management
Create flutter provider app
A boilerplate project created in Flutter using Provider and Firebase.
Stars: ✭ 180 (-15.49%)
Mutual labels:  state-management
Frideos flutter
An all-in-one Fllutter package for state management, reactive objects, animations, effects, timed widgets etc.
Stars: ✭ 187 (-12.21%)
Mutual labels:  state-management
Dop
JavaScript implementation for Distributed Object Protocol
Stars: ✭ 163 (-23.47%)
Mutual labels:  state-management
Tinystate
A tiny, yet powerful state management library for Angular
Stars: ✭ 207 (-2.82%)
Mutual labels:  state-management
Hydrated bloc
An extension to the bloc state management library which automatically persists and restores bloc states.
Stars: ✭ 181 (-15.02%)
Mutual labels:  state-management
Stamen
A React state management library based on Hooks
Stars: ✭ 196 (-7.98%)
Mutual labels:  state-management
Documentation
How does it all fit together?
Stars: ✭ 177 (-16.9%)
Mutual labels:  state-management
Observable Slim
Observable Slim is a singleton that utilizes ES6 Proxies to observe changes made to an object and any nested children of that object. It is intended to assist with state management and one-way data binding.
Stars: ✭ 178 (-16.43%)
Mutual labels:  state-management
Flutter book
Flutter1.17.x book App,使用Mobx数据管理器支持Android和iOS,使用库json_serializable、json_annotation、dio。
Stars: ✭ 190 (-10.8%)
Mutual labels:  state-management
Spaceace
A fancy immutable storage library for JavaScript
Stars: ✭ 167 (-21.6%)
Mutual labels:  state-management
Eiffel
Redux-inspired Android architecture library leveraging Architecture Components and Kotlin Coroutines
Stars: ✭ 203 (-4.69%)
Mutual labels:  state-management
Mobx Rest
REST conventions for Mobx
Stars: ✭ 164 (-23%)
Mutual labels:  state-management
Jquery History
Super-seeded by https://github.com/browserstate/history.js
Stars: ✭ 183 (-14.08%)
Mutual labels:  state-management
React Easy State
Simple React state management. Made with ❤️ and ES6 Proxies.
Stars: ✭ 2,459 (+1054.46%)
Mutual labels:  state-management
Reusable
Reusable is a library for state management using React hooks
Stars: ✭ 207 (-2.82%)
Mutual labels:  state-management
Reto
Flexible and efficient React Store with hooks.
Stars: ✭ 194 (-8.92%)
Mutual labels:  state-management

Controllerim

A simple, clean and well structured state management library for react

npm version

Installation

npm install controllerim --save

Migrating from Controllerim v2 to v3

The migration process should be very easy. Follow the docs and the example project for understanding the new API.

Basic usage example

Inside ParentController.js:

import { controller } from 'controllerim';

class _ParentController {
  constructor() {
    this.state = {
      message: 'hello' 
    };
  }
  getMessage() {
    return this.state.message;
  }
  setMessage(value) {
    this.state.message = value;
  }
}

export const ParentController = controller(_ParentController);

Inside Parent.jsx:

import React, { Component } from 'react';
import { observer } from 'controllerim';
import { ParentController } from './ParentController';

class Parent extends Component {
  constructor(props) {
    super(props);
    this.controller = ComponentController.create(); //returns a fresh controller instance
  }

  render() {
    return (
      <div>
        <h1>{this.controller.getMessage()}</h1>
        <Child/>
        <button onClick={() => this.controller.setMessage('hello world!')}>Click me to change message</button>
      </div>
    );
  }
};

export default observer(Parent);

Inside Child.jsx:

import React, { Component } from 'react';
import { observer} from 'controllerim';
import {ParentController} from './ParentController'

class Child extends Component {
  constructor(props){
    super(props);
    this.parentController = ParentController.getInstance(); //returns an existing instance of the parentController. You could supply an id if you you have more than one instances of the parent controller.
  }
 
  render() {
    return (
      <div>
        <span>This is a message from parent: {this.parentController.getMessage()}</span>
      </div>
    );
  }
};

export default observer(Child);

Example project

Here is a Simple example project You can see the source code under the example folder: demo-app/src If you want to run it locally: After cloning the repository, navigate to the demo-app folder and type in your terminal:

npm install
npm start

How

Controllerim utilizes Mobx behind the scenes. You don't need to learn Mobx in order to use Controllerim, but a basic understanding of Mobx is recommended.

Api

controller(controllerClass)

Arguments:
  • controllerClass: any es6 class with a state member.
Returns:
  • Object: { create(), getInstance() }: an object with two factory methods for getting a new controller instance

A controller is a plain Javascript class that holds a state and methods for manipulating the state. All the methods of the controller are smartly memoized and computed, thus if you do some heavy calculation, it will be re-calculated when really needed.

The observers (React Components that you wrapped within observer) will react to any change in the state, even changes of deep nested properties.

create(id?: string (default: 'globalInstance') ):

Returns a new instance of the given controller. You should use this method when you know for sure that you need a fresh instance and not an existing one (most of the time you should prefer create over getInstance). You can pass an id, for being used later by getInstance.

getInstance(id?: string (default: 'globalInstance')):

Returns an existing instance of the given controller, or a new one if there isn't any existing instance yet. If you don't supply an id, the return value will be the default global instance.

controller Usage example:

import {controller} from 'controllerim';

class _AppController {
  constructor() {
    this.state = {totalNotesCount: 2};                                 
  }

  getTotalNotesCount() {
    return this.state.totalNotesCount;
  }

  increaseCounter() {
    this.state.totalNotesCount++;
  }
}

export const AppController = controller(_AppController);

Your React component will create an instance of the controller like this:

import {AppController} from 'controllerim';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.controller = AppController.create();
  }

  render(){
    <div>
      <div>{this.controller.getTotalNotesCount()}</div>
      <div onPress={() => this.controller.increaseCounter()}>click me</div>
    </div>
  }
}

useController(controllerInstance)

Allows you to use controller inside a functional component. Note that you still have to wrap your functional component within observer.

import React from 'react';
import {observer, useController} from 'controllerim';
import {FunctionalComponentController} from './TestComponentController';

export const FunctionalComponent = observer(() => {
  const controller = useController(FunctionalComponentController.create());
  return (
    <div>
      <div>{controller.getUserName()}</div>
      <div onClick={() => controller.changeUserName()}>click me</div>
    </div>
  );
});

observer(ReactComponent)

To become reactive, every React component that uses a controller, should be wrapped within observer.

import {observer} from 'controllerim';

export const SomeSmartComponent = observer(class extends React.Component {
...
})

store(storeClass)

A store is just a global singleton controller that is not conceptually bound to any specific component.

inside AppStore.js:

  import {store} from 'controllerim';

  class _AppStore {
    constructor(){
      this.state = {useName: 'bla'};
    }

    getUserName() {
      return this.state.userName;
    }
  }

  export const AppStore = store(_AppStore);

Inside component.jsx:

import React from 'react';
import {observer} from 'controllerim'
import {AppStore} from './AppStore';

class SomeComponent extends React.Component {
  render(){
    <div>{AppStore.getUserName()}</div> // <== The component will re-render on any change in getUserName
  }
}

export default observer(SomeComponent);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].