Wednesday 17 May 2017

Pass data from one js to another js file React native

i am trying with react native , i have studied about it and now i am trying to learn it , This is my first day in this , i got some hints that how does it work form bellow mentioned tutorial

https://www.tutorialspoint.com/react_native/react_native_text_input.htm

I am just trying with login page and handling the submit button click , just want to capture input data and want to app it to my Button.js file. I know it may looks silly to you all there , but i really want to know it , i studied about props and was trying to use that in the same but when i replace component with props i start getting red screen , Please guide me little on this that if in case we are having two different components then how do we can pass our data between them.

Here i am posting both of the JS files :-

Form.js

import React, { Component, PropTypes } from 'react';
import Dimensions from 'Dimensions';
import {
        StyleSheet,
        KeyboardAvoidingView,
        View,
        ActivityIndicator,
        TouchableOpacity,
        Image,
} from 'react-native';

import UserInput from './UserInput';
import ButtonSubmit from './ButtonSubmit';
import SignupSection from './SignupSection';

import usernameImg from '../images/username.png';
import passwordImg from '../images/password.png';
import eyeImg  from '../images/eye_black.png';

export default class Form extends Component {
        constructor(props) {
    super(props);
    this.state = {
                        showPass: true,
                        press: false,
                        username: '',
                        password: ''
                };
                this.showPass = this.showPass.bind(this);

            this.handleChange = this.handleChange.bind(this);
        }

        showPass() {
  this.state.press === false ? this.setState({ showPass: false, press: true }) :this.setState({ showPass: true, press: false });
  }
handleChange(event) {
//    this.setState({usernamevalue: event.target.usernamevalue , passwordvalue : event.target.passwordvalue });
    alert('A name was submitted: ' + this.state.password);
  }
        render() {
                return (
                        <KeyboardAvoidingView behavior='padding'
                                style={styles.container}>
                                <UserInput source={usernameImg}
                                        placeholder='Username'
                                        autoCapitalize={'none'}
                                        returnKeyType={'done'}
                                        value={this.state.username}
                                        onChangeText={(text) => this.setState({username:text})}
                                        autoCorrect={false} />
                                <UserInput source={passwordImg}
                                        secureTextEntry={this.state.showPass}
                                        placeholder='Password'
                                        returnKeyType={'done'}
                                        value={this.state.password}
                    onChangeText={(text) => this.setState({password:text})}
                                        autoCapitalize={'none'}
                                        autoCorrect={false} />
                                        <TouchableOpacity
                                                activeOpacity={0.7}
                                                style={styles.btnEye}
                                                onPress={this.handleChange}
                                        >
                                                <Image source={eyeImg} style={styles.iconEye} />
                                        </TouchableOpacity>
                        </KeyboardAvoidingView>
                );
        }
}

const DEVICE_WIDTH = Dimensions.get('window').width;
const DEVICE_HEIGHT = Dimensions.get('window').height;

const styles = StyleSheet.create({
        container: {
                flex: 1,
                alignItems: 'center',
        },
        btnEye: {
    position: 'absolute',
    top: 55,
    right: 28,
  },
  iconEye: {
    width: 25,
    height: 25,
    tintColor: 'rgba(0,0,0,0.2)',
  },
});

ButtonSubmit.JS

import React, { Component, PropTypes } from 'react';
import Dimensions from 'Dimensions';
import {
        StyleSheet,
        TouchableOpacity,
        Text,
        Animated,
        Easing,
        Image,
        Alert,
        View,
} from 'react-native';
import { Actions, ActionConst } from 'react-native-router-flux';

import spinner from '../images/loading.gif';

const DEVICE_WIDTH = Dimensions.get('window').width;
const DEVICE_HEIGHT = Dimensions.get('window').height;
const MARGIN = 40;

export default class ButtonSubmit extends Component {
        constructor() {
                super();

                this.state = {
                        isLoading: false,
                };

                this.buttonAnimated = new Animated.Value(0);
                this.growAnimated = new Animated.Value(0);
                this._onPress = this._onPress.bind(this);
        }

        _onPress() {
                if (this.state.isLoading) return;

                this.setState({ isLoading: true });
                Animated.timing(
                        this.buttonAnimated,
                        {
                                toValue: 1,
                                duration: 200,
                                easing: Easing.linear
                        }
                ).start();

                setTimeout(() => {
                        this._onGrow();
                }, 2000);

                setTimeout(() => {
                        Actions.secondScreen();
                        this.setState({ isLoading: false });
                        this.buttonAnimated.setValue(0);
                        this.growAnimated.setValue(0);
                }, 2300);
        }

        _onGrow() {
                Animated.timing(
                        this.growAnimated,
                        {
                                toValue: 1,
                                duration: 200,
                                easing: Easing.linear
                        }
                ).start();
        }

        render() {
                const changeWidth = this.buttonAnimated.interpolate({
            inputRange: [0, 1],
            outputRange: [DEVICE_WIDTH - MARGIN, MARGIN]
          });
          const changeScale = this.growAnimated.interpolate({
            inputRange: [0, 1],
            outputRange: [1, MARGIN]
          });

                return (
                        <View style={styles.container}>
                                <Animated.View style=>
                                        <TouchableOpacity style={styles.button}
                                                onPress={this._onPress}
                                                activeOpacity={1} >
                                                        {this.state.isLoading ?
                                                                <Image source={spinner} style={styles.image} />
                                                                :
                                                                <Text style={styles.text}>LOGIN</Text>
                                                        }
                                        </TouchableOpacity>
                                        <Animated.View style={[ styles.circle, {transform: [{scale: changeScale}]} ]} />
                                </Animated.View>
                        </View>
                );
        }
}

const styles = StyleSheet.create({
        container: {
                flex: 1,
                top: -95,
                alignItems: 'center',
                justifyContent: 'flex-start',
        },
        button: {
                alignItems: 'center',
                justifyContent: 'center',
                backgroundColor: '#F035E0',
                height: MARGIN,
                borderRadius: 20,
                zIndex: 100,
        },
        circle: {
                height: MARGIN,
                width: MARGIN,
                marginTop: -MARGIN,
                borderWidth: 1,
                borderColor: '#F035E0',
                borderRadius: 100,
                alignSelf: 'center',
                zIndex: 99,
                backgroundColor: '#F035E0',
        },
        text: {
                color: 'white',
                backgroundColor: 'transparent',
        },
        image: {
                width: 24,
                height: 24,
        },
});

Should i use something like this.props.state.username to pass data to SubmitButton.js file.

The above code is from :- https://github.com/dwicao/react-native-login-screen

i am playing with this demo to understand the flow and concepts , Please provide me some suggestions here.

Your little help would be very much appreciated

Regards.



via Siddharth

No comments:

Post a Comment