- Introduction
- Introduction to Mobservable
- The Basics: Making stuff Reactive
- Core API
- Advanced API
- Best Practices For Large Apps
- 5.1. Definging data stores
- 5.2. Writing (async) actions
- 5.3. Organizing React components
- 5.4. Universal applications —>
- How does Mobservable work?
- Tips & Tricks
- 7.1. DevTools
- 7.2. ES6 & TypeScript goodies
- 7.3. Tracking state changes
- 7.4. Predicates
- 7.5. Performance considerations
- 8. Resources
- 9. Frequently Asked Questions
- Published with GitBook
Introduction
@reactiveComponent
The reactiveComponent
function / decorator can be used to turn ReactJS components into reactive components. It wraps the component's render function in mobservable.observe
to make sure that any data that is used during the rendering of a component forces a rerendering upon change. It is available through the separate mobservable-react
package.
import {reactiveComponent} from "mobservable-react";
var timerData = {
secondsPassed: 0
};
setInterval(function() {
timerData.secondsPassed++;
}, 1000);
@reactiveComponent class Timer extends React.Component {
render() {
return (<span>Seconds passed: { this.props.timerData.secondsPassed } </span> )
}
});
React.render(<Timer timerData={timerData} />, document.body);
In ES5 environments, reactive components can be simple declared using reactiveComponent(React.createClass({ …
.
When using @reactiveComponent
, a lot of components will become stateless. In such cases you can also write reactive function components using @reactiveComponent
(this works also on ES5 and with React 0.13):
const Timer = @reactiveComponent( ({timerData}) =>
(<span>Seconds passed: { timerData.secondsPassed } </span> )
);
Note: when reactiveComponent
needs to be combined with other decorators or higher-order-components, make sure that reactiveComponent
is the most inner (first applied) decorator; otherwise it might do nothing at all.