Created
October 23, 2018 17:07
-
-
Save albttx/bf8e3b6df31872f60427cd9bed251eac to your computer and use it in GitHub Desktop.
React Native simple mobx
This file contains hidden or 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 { View, Button, Text } from 'react-native'; | |
import { observable, computed, action } from 'mobx'; | |
import { observer } from 'mobx-react'; | |
class _TodosService { | |
i = 1 | |
@observable todos = [ | |
{name: "Test_0", done: false} | |
] | |
@computed get todosList() { | |
return this.todos | |
} | |
@action addTodo() { | |
this.todos.push({name: "Test_" + this.i++, done: false}) | |
} | |
} | |
const TodosService = new _TodosService(); | |
@observer | |
export default class App extends React.Component { | |
render() { | |
return ( | |
<View style={{paddingTop: 30}}> | |
<Button onPress={ () => TodosService.addTodo() }> | |
<Text>Add todos</Text> | |
</Button> | |
<Text>{ JSON.stringify( TodosService.todosList ) }</Text> | |
</View> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment