Package with react component wrapper for combining React with mobx.
Exports the observer
decorator and some development utilities.
For documentation, see the mobx project.
This package supports both React and React-Native.
npm install mobx-react --save
import {observer} from 'mobx-react';
// - or -
import {observer} from 'mobx-react/native';
// - or, for custom renderers without DOM: -
import {observer} from 'mobx-react/custom';
This package provides the bindings for MobX and React. See the official documentation for how to get started.
- Minimal MobX, React, ES6, JSX, Hot reloading: MobX-React-Boilerplate
- TodoMVC MobX, React, ES6, JSX, Hot reloading: MobX-React-TodoMVC
- Minimal MobX, React, Typescript, TSX: MobX-React-Typescript-Boilerplate
- Minimal MobX, React, ES6(babel), JSPM with hot reloading modules: jspm-react
- React-Native Counter: Mobx-React-Native-Counter
Function (and decorator) that converts a React component definition, React component class or stand-alone render function into a reactive component. See the mobx documentation for more details.
import {observer} from "mobx-react";
// ---- ES5 syntax ----
const TodoView = observer(React.createClass({
displayName: "TodoView",
render() {
return <div>{this.props.todo.title}</div>
}
}));
// ---- ES6 syntax ----
@observer class TodoView extends React.Component {
render() {
return <div>{this.props.todo.title}</div>
}
}
// ---- or just use a stateless component function: ----
const TodoView = observer(({todo}) => <div>{todo.title}</div>)
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 {observer} from "mobx-react";
@observer class TodoView extends React.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 argumentscomponentWillReact
won't fire before the initial render (usecomponentWillMount
instead)componentWillReact
won't fire when receiving new props or aftersetState
calls (usecomponentWillUpdate
instead)
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
?
I see React warnings about forceUpdate
/ setState
from React
The following warning will appear if you trigger a re-rendering between instantiating and rendering a component:
Warning: forceUpdate(...): Cannot update during an existing state transition (such as within
render). Render methods should be a pure function of props and state.
Usually this means that (another) component is trying to modify observables used by this components in their constructor
or getInitialState
methods.
This violates the React Lifecycle, componentWillMount
should be used instead if state needs to be modified before mounting.
Enables the tracking from components. Each rendered reactive component will be added to the componentByNodeRegistery
and its renderings will be reported through the renderReporter
event emitter.
Event emitter that reports render timings and component destructions. Only available after invoking trackComponents()
.
New listeners can be added through renderReporter.on(function(data) { /* */ })
.
Data will have one of the following formats:
{
event: 'render',
renderTime: /* time spend in the .render function of a component, in ms. */,
totalTime: /* time between starting a .render and flushing the changes to the DOM, in ms. */,
component: /* component instance */,
node: /* DOM node */
}
{
event: 'destroy',
component: /* component instance */,
node: /* DOM Node */
}
WeakMap. It's get
function returns the associated reactive component of the given node. The node needs to be precisely the root node of the component.
This map is only available after invoking trackComponents
.