2016-11-02 23 views
1

APIコールをローカルにキャッシュすることができるかどうかはわかりません。そのため、アプリケーションを閉じて再起動すると、まだそこに残っています。 Reduxののthisページでreact native/reduxでapiリクエストをキャッシュすることはできますか?

私はこのようにミドルウェアをキャッシュすることが可能であることを見た:

loadData(userId) { 
    // Injected into props by React Redux `connect()` call: 
    let { dispatch, posts } = this.props 

    if (posts[userId]) { 
     // There is cached data! Don't do anything. 
     return 
    } 

    // Reducer can react to this action by setting 
    // `isFetching` and thus letting us show a spinner. 
    dispatch(loadPostsRequest(userId)) 

    // Reducer can react to these actions by filling the `users`. 
    fetch(`http://myapi.com/users/${userId}/posts`).then(
     response => dispatch(loadPostsSuccess(userId, response)), 
     error => dispatch(loadPostsFailure(userId, error)) 
    ) 
    } 

は、私はそれを使用する必要がありますか?そして、アプリケーション終了後にキャッシュが残っていますか?

答えて

1

reduxストアを維持するには、Redux Persistライブラリを使用できます。デフォルトでは、このライブラリはreduxストア全体を永続化します。しかし、あなたのケースでは、いくつかのAPI呼び出しだけを持続させる必要があります。あなたがする必要があるのは、永続化する必要があるすべてのストアを下記のようにwhitelistに追加することです。

persistStore(
    store, { 
     storage: AsyncStorage, 
     whitelist: ['user','some_other_reducer'], 
    }, onComplete); 

あなたは持続Reduxのを設定する方法の実装の詳細については、Facebookのでf8Appを参照することができます。

+0

ありがとうございました!私は今試してみる。 –

関連する問題