const { observable } = mobx;
const { observer } = mobxReact;
const { Component } = React;
// Make an observable collection in our data
const data = {
@observable bestMovies: ["LOTR", "The Godfather", "Frozen"]
};
// Create the React component observing that data
@observer
class MovieList extends React.Component {
render() {
return <ul>
{data.bestMovies.map(movie => <li>{movie}</li>)}
</ul>;
}
};
// Render the movie list
const appDiv = document.getElementById('app');
ReactDOM.render(<MovieList />, appDiv);
// Add a movie to the list directly! without triggering React setState or forceUpdate
data.bestMovies.push('The Lion King')
<script src="https://unpkg.com/react@15/dist/react.min.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mobx/3.1.9/mobx.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mobx-react/4.1.8/index.min.js"></script>
<div id="app"></div>