Guest User

Untitled

a guest
Jul 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. class ProfileScreen extends Component {
  2.  
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. text: ''
  7. }
  8. }
  9.  
  10. componentWillMount() {
  11. this.retrieveData()
  12. }
  13.  
  14. saveData = async () => {
  15. try {
  16. await AsyncStorage.setItem('MY_DATA', JSON.stringify(this.state.text))
  17. }catch(error) {
  18. console.log(error.message)
  19. }
  20. }
  21.  
  22. retrieveData = async () => {
  23. try{
  24. const MY_DATA = await AsyncStorage.getItem('MY_DATA')
  25. alert(MY_DATA)
  26. this.setState({text: MY_DATA})
  27. }catch(error) {
  28. console.log(error.message)
  29. }
  30. }
  31.  
  32. navigate() {
  33.  
  34. console.log('navigating')
  35. const { navigate } = this.props.navigation;
  36. navigate('Home')
  37.  
  38. }
  39.  
  40.  
  41.  
  42. render() {
  43. return (
  44. <View>
  45. <TextInput style = {{width: '100%', height: 50, borderWidth: 1, marginTop: 100, backgroundColor: 'pink'}} onChangeText = {text => this.setState({text: text})}/>
  46. <View style = {{width: '100%', flexDirection: 'row', justifyContent: 'space-around', marginTop: 40}}>
  47. <Button title = "Save" onPress = {() => this.saveData()} />
  48. <Button title = "Retrieve data" onPress = {() => this.retrieveData()} />
  49. </View>
  50. <Text>TEXT: {this.state.text}</Text>
  51. <View style = {{width: '100%', flexDirection: 'row', justifyContent: 'space-around', marginTop: 40}}>
  52. <Text onPress = {() => this.navigate()}>Navigate</Text>
  53. </View>
  54. </View>
  55.  
  56. )
  57. }
  58. }
  59.  
  60.  
  61. class HomeScreen extends Component {
  62. static navigationOptions = {
  63. header: null
  64. }
  65. constructor(props) {
  66. super(props);
  67. this.toggle = this.toggle.bind(this);
  68. this.state = {
  69. isOpen: false,
  70. text: ''
  71. };
  72. }
  73.  
  74. componentWillMount() {
  75. this.retrieveData()
  76. }
  77.  
  78.  
  79. toggle() {
  80. this.setState({
  81. isOpen: !this.state.isOpen,
  82. });
  83. }
  84.  
  85. updateMenuState(isOpen) {
  86. this.setState({ isOpen });
  87. }
  88.  
  89. navigate() {
  90.  
  91. console.log('navigating')
  92. const { navigate } = this.props.navigation;
  93. navigate('Profile')
  94.  
  95. }
  96.  
  97.  
  98. saveData = async () => {
  99. try {
  100. await AsyncStorage.setItem('MY_DATA', JSON.stringify(this.state.text))
  101. }catch(error) {
  102. console.log(error.message)
  103. }
  104. }
  105.  
  106. retrieveData = async () => {
  107. try{
  108. const MY_DATA = await AsyncStorage.getItem('MY_DATA')
  109. alert(MY_DATA)
  110. this.setState({text: MY_DATA})
  111. }catch(error) {
  112. console.log(error.message)
  113. }
  114. }
  115.  
  116. render() {
  117.  
  118. return (
  119.  
  120. <View style={styles.containerMenu}>
  121. <StatusBar barStyle = 'light-content' />
  122. <View style = {styles.container}>
  123. <ScrollView style = {[{width: '100%', height: '100%'}]}>
  124. <View>
  125. <TextInput style = {{width: '100%', height: 50, borderWidth: 1, marginTop: 100}} onChangeText = {text => this.setState({text: text})}/>
  126. <View style = {{width: '100%', flexDirection: 'row', justifyContent: 'space-around', marginTop: 40}}>
  127. <Button title = "Save" onPress = {() => this.saveData()} />
  128. <Button title = "Retrieve data" onPress = {() => this.retrieveData()} />
  129. </View>
  130. <Text>TEXT: {this.state.text}</Text>
  131. <View style = {{width: '100%', flexDirection: 'row', justifyContent: 'space-around', marginTop: 40}}>
  132. <Text onPress = {() => this.navigate()}>Navigate</Text>
  133. </View>
  134. </View>
  135. </ScrollView>
  136. </View>
  137. </View>
  138.  
  139. );
  140. }
  141. }
  142.  
  143. export default class App extends Component {
  144. render() {
  145. return (
  146. <RootStack/>
  147. )
  148. }
  149. }
  150.  
  151. const RootStack = createStackNavigator(
  152. {
  153. Home: {screen: HomeScreen},
  154. Profile: {screen: ProfileScreen},
  155.  
  156.  
  157.  
  158. },
  159. {
  160. initialRouteName: 'Home',
  161. navigationOptions: {
  162. headerStyle: {
  163. backgroundColor: 'rgb(167,167,167)',
  164. },
  165. headerTintColor: '#fff',
  166. headerTitleStyle: {
  167. fontWeight: 'bold',
  168. },
  169. header: null
  170. },
  171. }
  172. );
  173.  
  174.  
  175. const styles = StyleSheet.create({
  176. container: {
  177. flex: 1,
  178. width: 320,
  179. alignItems: 'center',
  180. justifyContent: 'flex-start',
  181. },
  182.  
  183.  
  184. button: {
  185. position: 'absolute',
  186. top: 20,
  187. padding: 10,
  188. },
  189. caption: {
  190. fontSize: 20,
  191. fontWeight: 'bold',
  192. alignItems: 'center',
  193. },
  194. containerMenu: {
  195. flex: 1,
  196. justifyContent: 'center',
  197. alignItems: 'center',
  198. backgroundColor: '#F5FCFF',
  199. },
  200. })
Add Comment
Please, Sign In to add comment