Last active
July 4, 2017 15:45
-
-
Save ippa/0cfa6a50f4dd1ac827ff3bdd0d59af3e to your computer and use it in GitHub Desktop.
react-native meetup code samples
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
// ----------------------- | |
// Store.js | |
import {observable, action, computed} from "mobx"; | |
class Store { | |
@observable | |
todos = [ | |
{title: "Skapa gdocs presentation", status: "active"}, | |
{title: "Koda meetup-exempel", status: "active"}, | |
{title: "Koda meetup-exempel #2", status: "active"}, | |
{title: "Uppgradera XCode", status: "active"}, | |
]; | |
// Transaktion - Förhindrar 2 renderingar om state modifiers 2 ggr | |
@action | |
addTodo() { | |
this.todos.push({title: Math.random().toString()}); | |
} | |
@action | |
deleteTodo(title) { | |
const index = this.todos.findIndex(todo => todo.title === title); | |
this.todos.splice(index, 1); | |
} | |
@action | |
doneTodo(title) { | |
const index = this.todos.findIndex(todo => todo.title === title); | |
// Gotcha !! | |
// this.todos[index].status = "done"; | |
this.todos[index] = {title: title, status: "done"}; | |
} | |
filter = ""; | |
@computed | |
get filteredTodos() { | |
return this.todos.filter(todo => todo.title.includes(this.filter)); | |
} // MobX optimerar med cachade värden | |
} | |
export default new Store(); | |
// ----------------------- | |
// App.js | |
import React from "react"; | |
import {StyleSheet, Text, View, Button} from "react-native"; | |
import {observer} from "mobx-react"; | |
import Store from "./Store"; | |
@observer | |
export default class App extends React.Component { | |
render() { | |
return ( | |
<View style={styles.container}> | |
{Store.todos.map(todo => | |
<View key={todo.title} style={[styles.todo, styles[todo.status]]}> | |
<Text> | |
{todo.title} | |
</Text> | |
<Button title="Ta bort" onPress={() => Store.deleteTodo(todo.title)} /> | |
<Button title="Klar" onPress={() => Store.doneTodo(todo.title)} /> | |
</View> | |
)} | |
<Button title="Skapa TODO" onPress={() => Store.addTodo()} /> | |
</View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
margin: 10, | |
marginTop: 50, | |
backgroundColor: "#ffffff", | |
}, | |
todo: { | |
padding: 10, | |
margin: 5, | |
height: 110, | |
borderRadius: 5, | |
alignItems: "center", | |
backgroundColor: "#C4D2DC", | |
}, | |
done: { | |
backgroundColor: "#B4A09F", | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment