Giter VIP home page Giter VIP logo

mobx-inferno's Introduction

mobx-inferno

Join the chat at https://gitter.im/mobxjs/mobx #mobservable channel on reactiflux discord

This is a fork of mobx-react for Inferno

 

 

Package with inferno component wrapper for combining Inferno with mobx. Exports the observer decorator and some development utilities. For documentation, see the mobx project. This package supports Inferno.

Installation

npm install mobx-inferno --save

import {observer} from 'mobx-inferno';

This package provides the bindings for MobX and Inferno. See the official documentation for how to get started.

API documentation

observer(componentClass)

Function (and decorator) that converts a Inferno component definition, Inferno component class or stand-alone render function into a reactive component. See the mobx documentation for more details.

import Inferno from 'inferno';
import createClass from 'inferno-create-class';
import {observer} from "mobx-inferno";

// ---- ES5 syntax ----

const TodoView = observer(createClass({
    displayName: "TodoView",
    render() {
        return <div>{this.props.todo.title}</div>   
    }
}));

// ---- ES6 syntax ----
import Inferno from 'inferno';
import Component from 'inferno-component';
import {observer} from "mobx-inferno";

@observer 
class TodoView extends Component {
    render() {
        return <div>{this.props.todo.title}</div>   
    }   
}

// ---- or just use a stateless component function: ----
import Inferno from 'inferno';
import {observer} from "mobx-inferno";

const TodoView = observer(props => (
    <div>{props.todo.title}</div>
))

It is possible to set a custom shouldComponentUpdate, but in general this should be avoid as MobX will by default provide a highly optimized shouldComponentUpdate implementation, based on PureRenderMixin. If a custom shouldComponentUpdate is provided, it is consulted when the props changes (because the parent passes new props) or the state changes (as a result of calling setState), but if an observable used by the rendering is changed, the component will be re-rendered and shouldComponent is not consulted.

componentWillReact (lifecycle hook)

React components usually render on a fresh stack, so that makes it often hard to figure out what caused a component to re-render. When using mobx-react you can define a new life cycle hook, componentWillReact (pun intended) that will be triggered when a component will be scheduled to re-render because data it observes has changed. This makes it easy to trace renders back to the action that caused the rendering.

import Inferno from 'inferno';
import Component from 'inferno-component';
import {observer} from "mobx-inferno";

@observer 
class TodoView extends Component {
    componentWillReact() {
        console.log("I will re-render, since the todo has changed!");    
    }

    render() {
        return <div>{this.props.todo.title}</div>   
    }   
}
  • componentWillReact doesn't take arguments
  • componentWillReact won't fire before the initial render (use componentWillMount instead)
  • componentWillReact won't fire when receiving new props or after setState calls (use componentWillUpdate instead)

FAQ

Should I use observer for each component?

You should use observer on every component that displays observable data. Even the small ones. observer allows components to render independently from their parent and in general this means that the more you use observer, the better the performance become. The overhead of observer itself is neglectable. See also Do child components need @observer?

mobx-inferno's People

Contributors

nightwolfz avatar

Watchers

James Cloos 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.