2016-05-19 20 views
3

これは一般的な問題です.ReactNativeは、値がAsyncStorageから取得される前にレンダリングしようとしています。私はいくつかの場所でこれのための解決策を見てきましたが、何らかの理由でそれはまったく私のためには機能しません。私はReact Native 25.1を使っているからかもしれませんね?それはちょうど「Loading ...」に無期限に立ち往生します。コンソールログをレンダリングしてisLoading(ifメソッドなし)を表示すると、falseが返され、次にtrueが返されるので、理論上は動作するはずです。しかし、ifメソッドが有効になっていると「読み込み中」が永久に停止し、ログだけがfalseを返します。React NativeがAsyncStorageが値を取得するのを待ちます

import React, { Component } from 'react'; 

import { 
    Text, 
    View, 
    AsyncStorage 
} from 'react-native'; 

    class MainPage extends Component { 
    constructor(props: Object): void { 
    super(); 
    this.state = { 
    isLoading: false, 
    }; 
} 

    componentWillMount() { 
    AsyncStorage.getItem('accessToken').then((token) => { 
     this.setState({ 
     isLoading: false 
     }); 
    }); 
    } 

    render() { 
    if (this.state.isLoading) { 
     return <View><Text>Loading...</Text></View>; 
    } 
    // this is the content you want to show after the promise has resolved 
    return <View/>; 
    } 

}); 
+0

...あなたはより多くの明確化が必要な場合は、私に教えてください...これを試してみてください、あなたは 'componentDidMount'にコードを入れてみましたか? – zavtra

答えて

4

ねえ

import React, { Component } from 'react'; 

import { 
    Text, 
    View, 
    AsyncStorage 
} from 'react-native'; 

class MainPage extends Component { 

    constructor(props: Object): void { 
     super(props); 
     this.state = { 
     isLoading: false, 
     }; 
    } 

    componentWillMount() { 
    AsyncStorage.getItem('accessToken').then((token) => { 
     this.setState({ 
     isLoading: false 
     }); 
    }); 
    } 

    render() { 
    if (this.state.isLoading) { 
     return <View><Text>Loading...</Text></View>; 
    } 
    // this is the content you want to show after the promise has resolved 
    return <View/>; 
    } 
} 

+0

あなたはちょうど私の誤植を訂正したようです。つまり、 "var component = React.createClass({" in accidently。)が残っていて、それは問題ではないか、瞬時にエラーを投げるでしょう。 あなたのコードに何か他のものがありますが、私はそれが同じであると見なす限り、誤字を取り除いていませんか? – Hasen

+0

ちょっと..同じコードと反応ネイティブ0.25.1 – Jickson

+0

私はこの警告を受けています:マウントされたコンポーネントまたはマウントされているコンポーネントのみをアップデートできます。これは通常、アンマウントされたコンポーネントに対してsetState、replaceState、またはforceUpdateを呼び出したことを意味します。 –

関連する問題