簡體   English   中英

使用redux-saga反應服務器端渲染

[英]React server side rendering with redux-saga

我的傳奇實現為

import { put, takeLatest } from 'redux-saga/effects';
import axios from 'axios';

import { FETCH_CARD_LIST_DATA_FROM_API, SET_CARD_LIST  } from 
'./CardList.actions';
import constants from '../../../../constants';

export default function * fetchCardListSaga(){
 yield takeLatest(FETCH_CARD_LIST_DATA_FROM_API, fetchDataFromAPI)
}



function * fetchDataFromAPI(action){
const { payload } = action;
const response = yield 
axios.get(`${constants.API_URL}/hotels/${payload[0]}/${payload[1]}`);
  console.log('data recieved from api server', response.data.length);
if(response.status === 200){
  yield put({
    type: SET_CARD_LIST,
    payload: response.data
  });
 }
}

在server.js

const sagaMiddleWare = createSagaMiddleware();
const store =  createStore(rootReducer, initialState, 
applyMiddleware(sagaMiddleWare));
sagaMiddleWare.run(fetchCardListSaga);

現在我該如何調度操作,以便從API服務器獲取數據,然后在服務器上調用res.send。

我知道如何使用react-redux在客戶端運行它。 服務器端的問題是如何調度動作以及如何等待從服務器發送響應直到存儲更新。

棘手。

您可以通過獲取store引用並調用store.dispatch來調度操作,但是您不能等待這個傳奇。 而且您不能手動調用saga並使其yield ,因為它取決於saga中間件。

我會將請求放入一個輔助函數中,並在不加傳奇的情況下在服務器上進行數據獲取,並以此方式分配響應。 並在傳奇中使用相同的輔助函數。

老實說,我不會在SSE中使用中間件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM