Bạn có thể sử dụng tính năng autoRehydrate
của redux-persist
. Nó sẽ asyncronously chạy một hành động với loại REHYDRATE
sẽ rehydrate tất cả các cửa hàng trừ khi bạn danh sách đen chúng.
Nếu bạn cố thực thi truy vấn trước đó, Apollo sẽ kiểm tra cửa hàng Redux trước và bạn sẽ thấy hành động APOLLO_QUERY_RESULT_CLIENT
thực thi (nghĩa là nó trả về từ máy khách thay vì truy vấn máy chủ). Bạn có thể thay đổi fetchPolicy
để xác định cách truy vấn được dữ liệu của nó (mạng chỉ, bộ nhớ cache đầu tiên, vv)
Dưới đây là một thiết lập cơ bản:
import React, { Component } from 'react';
import { ApolloProvider } from 'react-apollo';
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import ApolloClient, { createNetworkInterface } from 'apollo-client';
import { persistStore, autoRehydrate } from 'redux-persist';
import { AsyncStorage } from 'react-native';
const networkInterface = createNetworkInterface({ uri: 'http://localhost:8080/graphql' });
export const client = new ApolloClient({
networkInterface: networkInterface,
});
const store = createStore(
combineReducers({
apollo: client.reducer(),
}),
{}, // initial state
compose(
applyMiddleware(client.middleware()),
autoRehydrate(),
),
);
// persistent storage
persistStore(store, {
storage: AsyncStorage, // or whatever storage you want
});
export default class App extends Component {
render() {
return (
<ApolloProvider store={store} client={client}>
<YourApp />
</ApolloProvider>
);
}
}
Cảm ơn @ srtucker22 !! Vấn đề của tôi là tôi đã sử dụng devTools(), và sau đó autoRehydrate(). Đối với một số lý do kỳ lạ này không hoạt động, tôi phải làm autoRehydrate() đầu tiên và sau đó devTools() .. và bây giờ nó hoạt động! – Carol
@Carol Ban phước cho bạn. Giải pháp của bạn chỉ cần 3 ngày cắn móng tay, và kéo tóc, để kết thúc. Đặt 'autoRehydrate()' trước '(cửa sổ typeof .__ REDUX_DEVTOOLS_EXTENSION__! == 'undefined' || process.env.NODE_ENV! == 'production')? cửa sổ .__ REDUX_DEVTOOLS_EXTENSION __(): (f) => f, 'dữ liệu đã giải quyết được duy trì sau khi tải lại trang. – TheoG