0
我有一個可以連接到不同API端點的React Native應用程序。有些用戶可能需要在運行時更改API端點,而無需重新啓動應用程序。所有的API請求被綁定到傳奇,和根傳奇看起來像如何在運行時更換傳奇?
export default function* rootSaga() {
yield [
takeLatest([
ONE_REQUEST,
ANOTHER_REQUEST,
// a bunch of sagas that are responsible for API querying
], api); // <- here, api is global.
];
}
,因此它可以隨着新實例化的終極版的存儲運行:
import rootSaga from './sagas';
const sagaMiddleware = createSagaMiddleware();
const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));
// other stuff, and finally
sagaMiddleware.run(rootSaga).done.catch(console.error);
,一旦執行的問題,商店,更不用說薩加斯,永遠不會更新。
我試圖通過api
根佐賀作爲第一個參數:
export default function* rootSaga(baseUrl = DEFAULT_API_URL) {
const api = create({
baseUrl,
// other stuff that is required by apisauce
});
yield [
takeLatest([
ONE_REQUEST,
ANOTHER_REQUEST,
// a bunch of sagas that are responsible for API querying
], api); // <- here, api is instantiated per every yield* of rootSaga.
];
}
我試圖每某個動作型執行的功能中是指從所述發電機本身:
yield [
takeLatest([
ONE_REQUEST,
ANOTHER_REQUEST,
// a bunch of sagas that are responsible for API querying
], api); // <- here, api is instantiated per every yield* of rootSaga.
takeEvery([
REPLACE_API // <- the action I would dispatch to replace API endpoint
], ({endpoint}) => {
yield cancel(rootSaga);
yield* rootSaga(endpoint); // <- the new API endpoint
});
];
但它沒有奏效。我也嘗試了其他一些策略,但都沒有效果。我查閱了Redux的replaceReducer
類似的文檔,但是對於redux-saga來說沒有什麼這樣的,這讓我覺得它只能使用root saga生成器生成的正確序列來完成。
那麼,是否有一個通用的方法來解決這個問題?是否有可能在運行時重新實例化根傳奇?