2016-11-02 5 views
1

api 호출을 로컬에서 캐시 할 수 있는지 알아낼 수 없었기 때문에 앱을 닫고 다시 시작하면 여전히 호출됩니다. 나는 그것을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)) 
    ) 
    } 

사용해야합니다

는 돌아 오는의 this 페이지에서 나는이 같은 미들웨어와 캐시하는 것이 가능하다는 것을 보았다? 그리고 앱을 닫은 후에도 캐시가 남아 있습니까?

답변

1

우리는 redux 저장소를 유지하기 위해 Redux Persist 라이브러리를 사용할 수 있습니다. 기본적으로이 라이브러리는 전체 redux 저장소를 유지합니다. 하지만 귀하의 경우에는 일부 API 호출 만 유지해야합니다. 당신이해야 할 일은 아래와 같이 whitelist에 보존해야하는 모든 상점을 추가하는 것입니다.

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

당신은 지속 REDUX을 설정하는 방법에 구현 자세한 내용은 페이스 북에 의해 f8App을 참조 할 수 있습니다.

+0

고마워요! 나는 그것을 지금 시도 할 것이다. –

관련 문제