2017-12-12 23 views
0

私はちょうどdevelopevといくつかのアプリケーションを反応してネイティブと私のコードで私はちょうど州で携帯電話を受信して​​います。この状態をグローバルで設定し、OTPスクリーンのような別の画面で使用する方法別の画面。私は反応したネイティブルータフラックスを使用します。このデータを次の画面で2番目の変数に送信できますか? グローバル州はネイティブに反応します


export default class Login extends Component{ 
    constructor(props) { 
    super(props); 
    this.state = {mobile: ''}; 



    this._OtpButton = this._OtpButton.bind(this); 

} 



    _OtpButton() { 

      fetch("https://2d44f003.ngrok.io/mobileWebViews/v1/sendOtp/", { 
       method: "POST", 
       headers: { 
        Accept: "application/json", 
        "Content-Type": "application/json" 
       }, 
       body: JSON.stringify({ 
        mobile:this.state.mobile, 
        type:'otp' 
       }) 
      }) 
        .then(response => response.json()) 
        .then(responseJson => { 
         if(responseJson.status.toString() === 'ok'){ 
          // this.global.mobile = mobile; 

          Alert.alert('پیامک ارسال شد'); 
          Actions.replace('OTP'); 


         }else if(responseJson.status.toString() === 'fail') { 
          Alert.alert(responseJson.message.toString()); 
         }else{ 
          Alert.alert(responseJson.message.toString()); 
         console.log(responseJson.message) 
         } 

        }) 
        .catch(error => { 
         Alert.alert("Error"+JSON.stringify(error)); 
         // console.error(error); 
        }) 
     } 


    render() { 


    return (

     <View style={styles.container}> 
     <Image source={require("./assets/img/login.jpg")} 
     style={{ 
         flex: 1, 
         position: 'absolute', 
         width: '100%', 
         height: '100%', 
         justifyContent: 'center', 
        }} 

        /> 

       <View style={styles.LoginBox}> 
        <Text style={styles.LoginTitel}> ورود به بوتیک</Text> 
          <View style={styles.inputGroups}> 

          <TextInput 
           style={styles.inputText} 
           underlineColorAndroid='transparent' 
           placeholder="شماره موبایل (مثال ۰۹۱۲۱۲۳۴۵۶۷)" 
           keyboardType={'numeric','number-pad'} 
           onChangeText={(mobile) => this.setState({mobile})} 

          /> 

         </View> 
         <TouchableOpacity activeOpacity={.8} onPress={this._OtpButton}> 
         <Text style={styles.ButtonEnter}>دریافت رمز از طریق پیامک</Text> 
         </TouchableOpacity> 

         <TouchableOpacity activeOpacity={.8} onPress={()=>Actions.replace('Login2')}> 
         <Text style={styles.ButtonPass}>ورود با نام کاربری و پسورد</Text> 
         </TouchableOpacity> 

         <Text> </Text> 
         <TouchableOpacity activeOpacity={.8} onPress={()=>Actions.replace('register')} > 
         <Text style={styles.forgetPass}>ثبت نام فروشگاه جدید</Text> 
         </TouchableOpacity> 

       </View> 
       <Image/> 

     </View> 
    ); 
    } 
} 
+3

をReduxのに見て。 – gravityplanx

答えて

1

はい、あなたはシーン間小道具を渡すことができます.049
phpstorm
アンドロイドスタジオネイティブ反応します。

これは、あなたが反応するネイティブ・ルータ・フラックスでこれを実現する方法を次のとおりです。

Actions.yourSceneName({ whatever: responseFromServer }); 

続いて次のシーンでは、あなたが渡された値にアクセスします。あなたの場合は

this.props.whatever 

グローバルに保存し、MobXやReduxなどの状態管理ライブラリを使用しない場合は、AsyncStorageを使用して電話機のメモリに保存します。それは次のようになります。

AsyncStorage.setItem('whatever', JSON.stringify({ valToBeSaved })); 

をそして、あなたのようなものだろう、その値取得するには:

AsyncStorage.getItem('whatever'); 
関連する問題