私はさまざまな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.
];
}
のように見えるので、それは新たにインスタンス化されたReduxのストアと一緒に実行することができます。
import rootSaga from './sagas';
const sagaMiddleware = createSagaMiddleware();
const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));
// other stuff, and finally
sagaMiddleware.run(rootSaga).done.catch(console.error);
問題は、一度実行、店であります、サガはもちろん、決して更新することはできません。
Iは、最初の引数として佐賀をrootに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.
];
}
Iは、特定のアクションタイプごとに実行される機能の中から発生自体を参照しようとした:
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を再インスタンス化することは可能ですか?