Skip to content

Instantly share code, notes, and snippets.

@alexandrebodin
Last active August 10, 2018 10:11
Show Gist options
  • Save alexandrebodin/25f5e35a57b94a1daaa9a2516663497a to your computer and use it in GitHub Desktop.
Save alexandrebodin/25f5e35a57b94a1daaa9a2516663497a to your computer and use it in GitHub Desktop.
import React from 'react';
import { observable, action, observe } from 'mobx';
// you can also use class and decorators but that's not really important here
// STORES
const createTodo = title =>
observable(
{
id: uuid(),
checked: false,
title,
setChecked() {
this.checked = true;
}
},
{
setChecked: action
}
);
const createTodosStore = () =>
observable(
{
todos: [],
add(title) {
this.todos.push(createTodo(title));
}
},
{
add: action
}
);
// COMPONENTS
const Todo = observe(({ todo }) => {
return (
<li>
<input type="checbox" value={todo.checked} onClick={todo.setChecked()} /> {todo.title}
</li>
);
});
const Todos = observe(({ todoStore }) => {
return (
<div>
<div>Todos:</div>
<ul>
todos.map(todo => <Todo todo={todo} />)
</ul>
<button
onClick={() => {
todoStore.add('Random Name');
}}
>
Add a todo
</button>
</div>
);
});
const App = () => {
return <Todos todoStore={createTodosStore()} />;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment