2016-09-27 47 views
0

我是一個非常新手來反應,原生的,所以這種問題。React-Native傳遞給其他js的Textinputvalue

我必須實現一個應用程序,我可以登錄到我們的網站。稍後更詳細。

第一個問題: 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);

正如你可以看到我想輸入用戶名和密碼輸入到TextInput。

的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);

調度(logInToWeb)方法./action/login.js外觀像這樣:

'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};

所以現在的問題是:

我怎樣才能從LoginScreen.js通過TextInput的值上ButtonClick到logInToWeb法在login.js

我如何獲取名爲和密碼的警報,我在login.js中調用了

就是這樣。後來我會問更多關於持證人身份驗證和登錄到服務器:)

+0

是在'LoginButton'的'onPress'方法/功能writter? –

+0

是的logIn()方法在LoginButton – Vitja

回答

0

我想你問的是如何發送名稱和密碼到logIn()方法?也許像這樣的工作:

// 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), 
    ]); 
    } 
} 

然後

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

我試過了,我在logintoweb()的名字和密碼屬性上做了一個提示,看是否通過了名字和密碼......但它告訴我它沒有定義 – Vitja

+0

你能顯示組件的所有代碼? –

+0

你需要哪個組件?我應該發佈登錄屏幕,登錄和登錄按鈕的所有代碼嗎? – Vitja