如果我將瀏覽器網址:/uni-classi-virtuali/gestisci/1
如何在React組件裝入之前製作Saga watch事件?
組件DettaglioClasseVirtuale
(其中我有獲取資源的調用)的功能componentWillMount
的rootSaga
開始前被調用。
有沒有一種標準的方法讓saga在應用程序啓動時獲取資源?
這是我用來獲取資源 「classivirtualiuni」 的項目傳奇守望者:
export function* watchGetClasseVirtualiUni() {
yield takeEvery(GET_CLASSE.REQUEST, getClasseVirtualiUni)
}
export function* getClasseVirtualiUni(action) {
try {
const data = yield call(fetchResource, `classivirtualiuni/${action.id}`)
if (data.success) {
yield put(getClasse.success(data.content))
} else {
yield put(getClasse.failure(data.message))
}
} catch (error) {
yield put(getClasse.failure(error.message))
}
}
通過簡單的rootsaga叫:
export default function* rootSaga() {
yield [
// ...
// ...
watchGetClasseVirtualiUni()
]
}
這是我的App類的反應,路由器處理:
class App extends Component {
render() {
return (
<Router history={browserHistory}>
<Route path={'/uni-classi-virtuali'} component={Main} >
<IndexRedirect to={'/uni-classi-virtuali/gestisci'} />
<Route path={'crea'} component={CreateClasseVirtualeUni} />
<Route path={'gestisci'} component={ClasseVirtualeList} >
<Route path={':id'} component={DettaglioClasseVirtuale} />
</Route>
<Route path={'iscriviti'} component={IscrivitiClasseVirtuale} />
</Route>
</Router>
)
}
}
這是調用提取數據的組件:
class DettaglioClasseVirtuale extends Component {
componentWillMount() {
// dispatch the GET_CLASSE.REQUEST action
this.fetchClasseVirtuale(this.props.classeId)
}
// ...
}
這是應用程序的核心:
function configureStore(initialState) {
const sagaMiddleware = createSagaMiddleware()
let finalCreateStore = applyMiddleware(thunk, sagaMiddleware)(createStore)
const store = finalCreateStore(reducer, initialState);
store.runSaga = sagaMiddleware.run
store.close =() => store.dispatch(END)
return store;
}
const store = configureStore()
store.runSaga(rootSaga)
ReactDOM.render(
<div>
<Provider store={store}>
<App />
</Provider>
</div>,
document.getElementById('virtual-class')
)
感謝拉米。我爲另一個不需要參數的頁面使用了這種解決方法。即使我找到了傳遞參數classeId的方法,它仍然是一種解決方法。我對找到一個實際的解決方案感興趣 –