Guest User

Untitled

a guest
Feb 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. import React, { Component } from "react"
  2. import { Text, View, TextInput, StyleSheet, Button } from "react-native"
  3. import LoginView from "./LoginView"
  4.  
  5. export default class LoginPage extends Component {
  6. state = {
  7. userName:"",
  8. password: "",
  9. isLoggedin: false
  10. }
  11.  
  12. handleUserName = (userName) => {
  13. this.setState({ userName })
  14. }
  15.  
  16. handlePassword = (password) => {
  17. this.setState({ password })
  18. }
  19.  
  20. toggleLoggedIn = () => this.setState(prevState => ({ isLoggedin: !prevState.isLoggedin }))
  21.  
  22. handleSubmit = () => {
  23. this.toggleLoggedIn()
  24. }
  25.  
  26. resetState = () => {
  27. this.setState({
  28. userName: "",
  29. password: ""
  30. })
  31. }
  32. render() {
  33. const { userName, password, isLoggedin } = this.state
  34. const { container, textinput } = styles
  35. const disable = userName && password ? false : true
  36. return(
  37. <View style={container}>
  38. {isLoggedin
  39. ? <LoginView userName={userName} toggleLoggedIn={this.toggleLoggedIn} resetState={this.resetState}/>
  40. : (
  41. <View>
  42. <Text>Enter Username And Password</Text>
  43. <TextInput
  44. style={textinput}
  45. value={userName}
  46. placeholder="UserName"
  47. onChangeText={this.handleUserName}
  48. />
  49. <TextInput
  50. style={textinput}
  51. value={password}
  52. placeholder="password"
  53. onChangeText={this.handlePassword}
  54. secureTextEntry={true}
  55. />
  56. <Button
  57. onPress={this.handleSubmit}
  58. title="Log In"
  59. color="#ff8500"
  60. disabled={disable}
  61. />
  62. </View>
  63. )
  64. }
  65. </View>
  66. )
  67. }
  68. }
  69.  
  70. const styles = StyleSheet.create({
  71. container: {
  72. flex: 1,
  73. backgroundColor: '#fff',
  74. alignItems: 'center',
  75. paddingTop: 100,
  76. },
  77. textinput: {
  78. height: 40,
  79. borderColor: 'gray',
  80. borderWidth: 1,
  81. width: 200,
  82. marginBottom: 20,
  83. marginTop: 10,
  84. },
  85. });
  86.  
  87.  
  88.  
  89.  
  90. loginView.js
  91.  
  92. import React from "react"
  93. import { View, Text, Button, StyleSheet } from "react-native"
  94.  
  95. const LoginView = ({ userName, toggleLoggedIn, resetState }) => (
  96. <View>
  97. <Text>You have succesfully logged in {userName}</Text>
  98. <Button
  99. onPress={() => {
  100. toggleLoggedIn()
  101. resetState()
  102. }}
  103. color="#ff8500"
  104. title="Logout"
  105. style={styles.buttonStyle}
  106. />
  107. </View>
  108. )
  109.  
  110. export default LoginView
Add Comment
Please, Sign In to add comment