Coder Social home page Coder Social logo

react-magic-state's Introduction

React Magic State

Version License

React state management without the hassle.

Introduction

React Magic State was created with simplicity and efficiency in mind. Its tiny API, comprised of just three methods, is enough to express all possible reactive state-component relationships.

The way React Magic State works is by wrapping your store(s) with ES6 proxies, and using them to keep track of every property that is accessed by your React components. Whenever an observed property changes, the relevant components are re-rendered by updating their internal state.

The use of ES6 proxies means that only current browsers are supported. This means no Internet Explorer support, sorry.

Installation

npm install --save react-magic-state

API

useStore()

Simply wrap your store(s) with useStore():

const store = useStore({
    foo: 42
});

Private Store in class component

Inside class components, you can call useStore() to track local state:

const Hello = view(class Hello extends React.PureComponent {
    constructor(props) {
        super(props);

        this.store = useStore({
            name: "World"
        });
    }

    render() {
        return <h1>Hello {this.store.name}!</h1>;
    }
});

Private store in function component

Inside function components, you can call useStore() to track local state:

const Hello = view(function Hello() {
    const store = useStore({
        name: "World"
    });

    return <h1>Hello {store.name}!</h1>
});

view()

Wrap your components with view() to track store(s) and react to changes:

const Hello = view(class Hello extends React.PureComponent {
    render() {
        ...
    }
});

You can also use view() as a decorator with Babel or TypeScript:

@view
class Hello extends React.Component {
    render() {
        ...
    }
}

batch()

Wrap your bulk actions with batch() to minimise re-renders and improve performance:

class Store {
    bulkAction() {
        batch(() => {
            ...
        });
    }
}

react-magic-state's People

Contributors

diegobg avatar

Watchers

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