Guest User

Untitled

a guest
Jan 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. import React from 'react';
  2. import { StyleSheet, Text, View, AppRegistry } from 'react-native';
  3. import TodoApp from './src/TodoApp'
  4. import store from './app/store/index'
  5. import { Provider } from 'react-redux'
  6. import {CounterR} from './app/counterR';
  7. import {Counter} from './app/counter';
  8. import {createStackNavigator, createAppContainer} from 'react-
  9. navigation'
  10.  
  11. export default class App extends React.Component {
  12. render() {
  13. return (
  14. <Provider store={store}>
  15. <AppContainer />
  16. </Provider>
  17. );
  18. }
  19. }
  20.  
  21. const stack = createStackNavigator({
  22. Home: CounterR,
  23. Second: Counter
  24. },{
  25. initialRouteName: 'Home'
  26. })
  27.  
  28. const AppContainer = createAppContainer(stack)
  29. const styles = StyleSheet.create({
  30. container: {
  31. flex: 1,
  32. backgroundColor: '#fff',
  33. alignItems: 'center',
  34. justifyContent: 'center',
  35. },
  36. });
  37.  
  38. import React, { Component } from 'react';
  39. import {View,
  40. TouchableOpacity,
  41. Text,
  42. Dimensions,
  43. StyleSheet,
  44. TextInput,
  45. FlatList,
  46. Keyboard} from 'react-native';
  47. import {connect} from 'react-redux'
  48. import { addTodo } from './actions/index'
  49. import { toggleTodo } from './actions/index'
  50. import {NavigationAction} from 'react-navigation'
  51. import store from './store/index'
  52. import { Provider } from 'react-redux'
  53. const { width, height } = Dimensions.get("window");
  54.  
  55. export class CounterR extends Component{
  56.  
  57. state = {
  58. text: ""
  59. }
  60.  
  61. add = text => {
  62. Keyboard.dismiss()
  63. this.props.addTodo(text)
  64. this.setState({text: ""})
  65. }
  66.  
  67. render(){
  68. return(
  69. <View style={styles.container}>
  70. <TextInput
  71. style={{borderWidth:.5, width: 300, height: 40}}
  72. defaultValue={this.state.text}
  73. onChangeText={(value) => this.setState({text:value})}/>
  74. <TouchableOpacity
  75. onPress={() => this.add(this.state.text)}>
  76. <Text
  77. style={{fontSize: 18}}>ADD TODO</Text>
  78. </TouchableOpacity>
  79. <TouchableOpacity
  80. onPress={() => this.props.navigation.navigate('Second')}>
  81. <Text
  82. style={{fontSize: 18}}>Second Screen</Text>
  83. </TouchableOpacity>
  84. <FlatList
  85. data= {this.props.todos}
  86. numColumns={1}
  87. keyExtractor= {item => item.id}
  88. renderItem ={({ item }) =>
  89. <TouchableOpacity
  90. onPress={() => this.props.toggleTodo(item)}
  91. style={styles.todoText}>
  92. <Text style={{textDecorationLine: item.completed ? 'line-through' : 'none', fontSize: 20}}>{item.text}</Text>
  93. </TouchableOpacity>
  94. }
  95. />
  96. </View>
  97. )
  98. }
  99. }
  100. const mapStateToProps = state => ({
  101. todos: state.todos
  102. })
  103.  
  104. const mapDispatchToProps = dispatch => ({
  105. addTodo: text => dispatch(addTodo(text)),
  106. toggleTodo: item => dispatch(toggleTodo(item))
  107. })
  108.  
  109.  
  110. export default connect(mapStateToProps, mapDispatchToProps)(CounterR)
  111.  
  112.  
  113.  
  114.  
  115. const styles = StyleSheet.create({
  116. container: {
  117. flex: 1,
  118. justifyContent: 'center',
  119. alignItems: 'center',
  120. backgroundColor: '#F5FCFF',
  121. },
  122. welcome: {
  123. fontSize: 20,
  124. textAlign: 'center',
  125. margin: 10,
  126. },
  127. instructions: {
  128. textAlign: 'center',
  129. color: '#333333',
  130. marginBottom: 5,
  131. },
  132. todoText: {
  133. width: width,
  134. margin: 10
  135. }
  136. });
  137.  
  138. import React, { Component } from 'react';
  139. import {View,
  140. TouchableOpacity,
  141. Text,
  142. Dimensions,
  143. StyleSheet,
  144. TextInput,
  145. FlatList,
  146. Keyboard} from 'react-native';
  147. import {connect} from 'react-redux'
  148. import { addTodo } from './actions/index'
  149. import { toggleTodo } from './actions/index'
  150. import { Provider } from 'react-redux'
  151. import store from './store/index'
  152. const { width, height } = Dimensions.get("window");
  153.  
  154. export class Counter extends Component{
  155.  
  156. state = {
  157. text: "",
  158. }
  159.  
  160. addTodo = text => {
  161.  
  162. Keyboard.dismiss()
  163. this.props.addTodo(text)
  164. this.setState({text: ""})
  165.  
  166. }
  167.  
  168. render(){
  169. return(
  170. <View style={styles.container}>
  171. <TextInput
  172. style={{borderWidth:.5, width: 300, height: 40}}
  173. defaultValue={this.state.text}
  174. onChangeText={(value) => this.setState({text: value})}/>
  175. <TouchableOpacity
  176. onPress={() => this.addTodo(this.state.text)}>
  177. <Text
  178. style={{fontSize: 18}}>ADD TODO</Text>
  179. </TouchableOpacity>
  180.  
  181. </View>
  182. )
  183. }
  184. }
  185.  
  186. const mapStateToProps = state => ({
  187. todos: state.todos
  188. })
  189.  
  190. const mapDispatchToProps = dispatch => ({
  191.  
  192. addTodo: text => dispatch(addTodo(text)),
  193. toggleTodo: item => dispatch(toggleTodo(item))
  194. })
  195.  
  196.  
  197. export default connect(mapStateToProps, mapDispatchToProps)(Counter)
  198.  
  199. const styles = StyleSheet.create({
  200. container: {
  201. flex: 1,
  202. justifyContent: 'center',
  203. alignItems: 'center',
  204. backgroundColor: '#F5FCFF',
  205. },
  206. welcome: {
  207. fontSize: 20,
  208. textAlign: 'center',
  209. margin: 10,
  210. },
  211. instructions: {
  212. textAlign: 'center',
  213. color: '#333333',
  214. marginBottom: 5,
  215. },
  216. todoText: {
  217. width: width,
  218. margin: 10
  219. }
  220. });
  221.  
  222. import {Keyboard} from 'react-native'
  223. import { ADD_TODO, TOGGLE_TODO } from './actionTypes'
  224. let x = 0
  225. export const addTodo = (text) => ({
  226. type: ADD_TODO,
  227. id: x++,
  228. text: text
  229. })
  230. export const toggleTodo = (item) => ({
  231.  
  232. type: TOGGLE_TODO,
  233. id: item.id
  234. })
  235.  
  236. export const ADD_TODO = 'ADD_TODO'
  237. export const TOGGLE_TODO = 'TOGGLE_TODO'
  238.  
  239. import { combineReducers } from 'redux'
  240. import todos from './todos'
  241.  
  242. export default combineReducers({
  243. todos
  244. })
  245.  
  246. const todos = (state =[], action) => {
  247. switch(action.type){
  248. case 'ADD_TODO':
  249. return [
  250. ...state, {
  251. id: action.id,
  252. text: action.text,
  253. completed: false
  254. }
  255. ]
  256.  
  257. case 'TOGGLE_TODO':
  258. return state.map(todo =>
  259. (todo.id === action.id)
  260. ?
  261. {...todo, completed: !todo.completed}
  262. :
  263. todo)
  264. default:
  265. return state
  266. }
  267. }
  268. export default todos
  269.  
  270. import { createStore } from 'redux'
  271. import rootReducer from '../reducers'
  272.  
  273. export default store = createStore(rootReducer)
Add Comment
Please, Sign In to add comment