Last active
August 10, 2018 10:11
-
-
Save alexandrebodin/25f5e35a57b94a1daaa9a2516663497a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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