Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react';
- import { View, Text , TouchableOpacity , TextInput } from 'react-native';
- import { connect } from "react-redux";
- import { bindActionCreators } from "redux";
- import * as signUpActions from "../actions/SignUpActions";
- class SignUp extends React.Component {
- constructor(){
- super();
- this.state = {
- name : '',
- password : '',
- };
- }
- saveUser(){
- let user = {};
- user.name = this.state.name;
- user.password = this.state.password;
- this.props.registerUser(user);
- }
- static navigationOptions = {
- title : 'Sign Up',
- };
- render(){
- return (
- <View>
- <TextInput
- placeholder="Username"
- onChangeText={(text) => this.setState({name : text})}
- />
- <TextInput
- placeholder="Password"
- onChangeText={(text) => this.setState({password : text})}
- />
- <TouchableOpacity onPress = {() => this.saveUser()} >
- <Text>DONE</Text>
- </TouchableOpacity>
- </View>
- );
- }
- }
- export default connect(
- state => ({
- user : state.user
- }),
- dispatch => bindActionCreators(signUpActions, dispatch)
- )(SignUp);
- function storeUser(user) {
- return {
- type : 'REGISTER_USER',
- payload : user,
- };
- };
- export function registerUser(user) {
- return function (dispatch, getState) {
- fetch(<the-url>)
- .then((response) => {return response.json()})
- .then((responseData) => dispatch(storeUser(responseData)))
- .catch((err) => console.log(err));
- };
- };
- const initialState = {
- data : {},
- };
- export default function signUpReducer(state = initialState, action) {
- console.log(action.payload)
- switch (action.type) {
- case 'REGISTER_USER' :
- return {
- ...state ,
- user : action.payload
- }
- default :
- return state;
- }
- }
Add Comment
Please, Sign In to add comment