2016-09-27 5 views
0

ich bin ein sehr Neuling zu reagieren-native, so sry für diese Art von Frage.React-Native pass Texteingabewert zu anderen js

Ich muss eine App implementieren, mit der ich auf unserer Website einloggen kann. Mehr Details später.

Erstes Problem: LoginScreen.js

var Animated = require('Animated'); 
 
var Dimensions = require('Dimensions'); 
 
var Image = require('Image'); 
 
var React = require('React'); 
 
var StatusBar = require('StatusBar'); 
 
var StyleSheet = require('StyleSheet'); 
 
var View = require('View'); 
 
var { 
 
    Text 
 
} = require('OnTrackText'); 
 
var LoginButton = require('../common/LoginButton'); 
 
var TouchableOpacity = require('TouchableOpacity'); 
 
var TextInput = require('TextInput'); 
 
var { 
 
    skipLogin 
 
} = require('../actions'); 
 
var { 
 
    connect 
 
} = require('react-redux'); 
 
class LoginScreen extends React.Component { 
 
    state = { 
 
    anim: new Animated.Value(0), 
 
    name: '', 
 
    password: '' 
 
    }; 
 
    componentDidMount() { 
 
    Animated.timing(this.state.anim, { 
 
     toValue: 3000, 
 
     duration: 3000 
 
    }).start(); 
 
    } 
 
    render() { 
 
    return (< Image style = { 
 
     styles.container 
 
     } 
 
     source = { 
 
     require('./img/login-background.png') 
 
     } > 
 
     < StatusBar barStyle = "default"/> 
 
     < TouchableOpacity accessibilityLabel = "Skip login" 
 
     accessibilityTraits = "button" 
 
     style = { 
 
     styles.skip 
 
     } 
 
     onPress = { 
 
     () => this.props.dispatch(skipLogin()) 
 
     } > 
 
     < Animated.Image style = { 
 
     this.fadeIn(2800) 
 
     } 
 
     source = { 
 
     require('./img/x.png') 
 
     } 
 
     /> 
 
     </TouchableOpacity > 
 
     < View style = { 
 
     styles.section 
 
     } > 
 
     < Animated.Image style = { 
 
     this.fadeIn(0) 
 
     } 
 
     source = { 
 
     require('./img/[email protected]') 
 
     } 
 
     /> 
 
     </View > 
 
     < View style = { 
 
     styles.section 
 
     } > 
 
     < Animated.Text style = { 
 
     [styles.h1, this.fadeIn(700, -20)] 
 
     } > 
 
     Willkommen zur < /Animated.Text> 
 
      <Animated.Text style={[styles.h1, {marginTop: -10}, this.fadeIn(700, 20)]}> 
 
      OnTrack App 
 
      </Animated.Text > 
 
     < /View> 
 
     <View style={styles.section}> 
 
      <TextInput 
 
      style={styles.input} 
 
      onChangeText={(text) => this.setState({ name: text }) } 
 
      value={this.state.name} 
 
      placeholder={"Benutzername"} 
 
      /> 
 
     < TextInput style = { 
 
     styles.input 
 
     } 
 
     onChangeText = { 
 
     (text) => this.setState({ 
 
      password: text 
 
     }) 
 
     } 
 
     value = { 
 
     this.state.password 
 
     } 
 
     secureTextEntry = { 
 
     true 
 
     } 
 
     placeholder = { 
 
     "Password" 
 
     } 
 
     /> 
 
     </View > 
 
     < Animated.View style = { 
 
     [styles.section, styles.last, this.fadeIn(2500, 20)] 
 
     } > 
 
     < LoginButton name = { 
 
     this.state.name 
 
     } 
 
     password = { 
 
     this.state.password 
 
     } 
 
     source = "First screen"/> 
 
     < /Animated.View> 
 
     </Image > 
 
    ); 
 
    } 
 
    fadeIn(delay, from = 0) { 
 
    .... 
 
    } 
 
    const scale = Dimensions.get('window').width/375; 
 
    var styles = StyleSheet.create({ 
 
     .... 
 
    } 
 
    }); 
 
module.exports = connect()(LoginScreen);

Wie man sehen kann ich den Namen und das Kennwort in die Texteingabe eingeben möchte.

als

die LoginButton.js

'use strict'; 
 

 
const React = require('react'); 
 
const {StyleSheet} = require('react-native'); 
 

 
const { logInToWeb } = require('../actions'); 
 
const {connect} = require('react-redux'); 
 

 
class LoginButton extends React.Component { 
 
    props: { 
 
    style: any; 
 
    source?: string; // For Analytics 
 
    dispatch: (action: any) => Promise; 
 
    onLoggedIn: ?() => void; 
 
    }; 
 
    state: { 
 
    isLoading: boolean; 
 
    }; 
 
    _isMounted: boolean; 
 

 
    constructor() { 
 
    super(); 
 
    this.state = { isLoading: false }; 
 
    } 
 

 
    componentDidMount() { 
 
    this._isMounted = true; 
 
    } 
 

 
    componentWillUnmount() { 
 
    this._isMounted = false; 
 
    } 
 

 
    render() { 
 
    if (this.state.isLoading) { 
 
     return (
 
     <OnTrackButton 
 
      style={[styles.button, this.props.style]} 
 
      caption="Please wait..." 
 
      onPress={() => {}} 
 
     /> 
 
    ); 
 
    } 
 

 
    return (
 
     <OnTrackButton 
 
     style={[styles.button, this.props.style]} 
 
    // icon={require('../login/img/f-logo.png')} 
 
     caption="Login to OnTrack" 
 

 
     // onPress={this.props.onpress} 
 
     onPress={() => this.logIn()} 
 
     /> 
 
    ); 
 
    } 
 

 
    async logIn() { 
 
    const {dispatch, onLoggedIn, name, password} = this.props; 
 

 
    this.setState({isLoading: true}); 
 
    try { 
 
     await Promise.race([ 
 
     dispatch(logInToWeb(name,password)), 
 
     timeout(15000), 
 
     ]); 
 
    } catch (e) { 
 
     const message = e.message || e; 
 
     if (message !== 'Timed out' && message !== 'Canceled by user') { 
 
     alert(message); 
 
     console.warn(e); 
 
     } 
 
     return; 
 
    } finally { 
 
     this._isMounted && this.setState({isLoading: false}); 
 
    } 
 

 
    onLoggedIn && onLoggedIn(); 
 
    } 
 
} 
 

 
async function timeout(ms: number): Promise { 
 
    return new Promise((resolve, reject) => { 
 
    setTimeout(() => reject(new Error('Timed out')), ms); 
 
    }); 
 
} 
 

 
var styles = StyleSheet.create({ 
 
... 
 
}); 
 

 
module.exports = connect()(LoginButton);

als

der Versand (logInToWeb) -Methode in ./action/login.js aussieht so:

'use strict'; 
 

 
import type { Action, ThunkAction } from './types'; 
 

 
const Parse = require('parse/react-native'); 
 
const {Platform} = require('react-native'); 
 
const Alert = require('Alert'); 
 

 

 
function logInToWeb(data): ThunkAction { 
 
    const {name, password} = data 
 

 
     Alert.alert(
 
     `Hi, ${name} & ${password}`, 
 
     'möchten Sie sich ausloggen?', 
 
     [ 
 
      { text: 'Abbruch' }, 
 
      { text: 'Ausloggen' }, 
 
     ] 
 
    ) 
 
    
 
} 
 

 
function skipLogin(): Action { 
 
    return { 
 
    type: 'SKIPPED_LOGIN', 
 
    }; 
 
} 
 

 
function logOut(): ThunkAction { 
 
... 
 
} 
 

 
function logOutWithPrompt(): ThunkAction { 
 
.... 
 
} 
 

 
module.exports = {logInToWeb, skipLogin, logOut, logOutWithPrompt};

Die Frage ist also:

Wie kann ich den Wert der Texteingabe von dem LoginScreen.js auf Button Zur logInToWeb-Methode in der login.js passieren

Wie kann ich den Namen und das Passwort in der Warnung erhalten, die ich in login.js

aufgerufen habe 10

Das ist es. Später werde ich mehr über Bearer-Auth und Loggen zum Server fragen :)

+0

Ist 'onPress' Methode/Funktionalität writter im' LoginButton'? –

+0

ja die logIn() Methode ist in der LoginButton – Vitja

Antwort

0

Ich denke, was Sie fragen, ist, wie Sie den Namen und das Passwort an Ihre logIn() Methode senden? Vielleicht funktionieren würde, so etwas wie dieses:

// Login Button 
<LoginButton 
    name={this.state.name} 
    password={this.state.password} 
    source="First screen" /> 

// Login method 
async logIn() { 
    const {dispatch, onLoggedIn, name, password} = this.props; 
    this.setState({isLoading: true}); 
    try { 
    await Promise.race([ 
     dispatch(logInToWebk({name, password})), 
     timeout(15000), 
    ]); 
    } 
} 

dann

function logInToWebk(data): ThunkAction { 
    const { name, password } = data 
    // do something with name + password 
} 
+0

Ich habe versucht, und ich machte eine Warnung auf Name und Passwort-Eigenschaft auf der logintoweb() um zu sehen, ob Name und Passwort übergeben wird ... Aber es sagt mir, es ist undefined – Vitja

+0

Können Sie Zeigen Sie Ihren gesamten Code für die Komponenten an? –

+0

welche Komponente benötigen Sie? Sollte ich den gesamten Code von loginscreen posten, login und loginbutton? – Vitja