【发布时间】:2020-03-27 12:47:57
【问题描述】:
我无法制作合适的服务器端。
当我启动我的服务器并进行提取时,例如 http://localhost:3000/last-movie-releases
toPromise() 不会等待第一次提取。但是接下来我做的其他的都好,服务器端就好了。
下面只是一个 sn-p 我的代码。我在类构造函数中调度我的 redux 操作。 (就像每个人一样)
store
.runSaga(rootSaga)
.toPromise()
.then(() => {
console.log('sagas complete');
const html = ReactDOMServer.renderToString(jsx);
const css = sheets.toString();
res.send(
renderFullPage(html, css, serialize(store.getState()))
);
})
.catch(e => {
console.log(e.message);
res.status(500).send(e.message);
});
ReactDOMServer.renderToString(jsx);
store.close();
这是正常行为吗?
PS:我所说的 fetch 的传奇故事从 fork 开始,然后是 fork,然后是 call。和real-word repo中的一模一样
我想知道我是否错过了某个地方的回报。
/***********************************************
***************** UPDATE BELOW *****************
***********************************************/
我给你我的传奇(只有 2 个)
import {
take,
put,
call,
fork,
select,
delay,
all,
takeEvery,
takeLatest
} from 'redux-saga/effects';
import * as api from './api';
import * as actions from '../actions';
// each entity defines 3 creators { request, success, failure }
const { movies, movie } = actions;
function* fetchEntity(entity, apiFn, body) {
yield put(entity.request(body));
const { response, error } = yield call(apiFn, body);
if (response) yield put(entity.success(body, response));
else yield put(entity.failure(body, error));
}
// yeah! we can also bind Generators
export const fetchMovies = fetchEntity.bind(null, movies, api.fetchMovies);
export const fetchMovie = fetchEntity.bind(null, movie, api.fetchMovie);
/******************************************************************************/
/********************************* SAGAS **************************************/
/******************************************************************************/
function* sagaFetchMovies() {
yield call(fetchMovies);
}
function* sagaFetchMovie(movieId) {
yield call(fetchMovie, movieId);
}
/******************************************************************************/
/******************************* WATCHERS *************************************/
/******************************************************************************/
function* watchFetchMovies() {
while (true) {
yield take(actions.FETCH_MOVIES);
yield fork(sagaFetchMovies);
}
}
function* watchFetchMovie() {
while (true) {
const { movieId } = yield take(actions.FETCH_MOVIE);
yield fork(sagaFetchMovie, movieId);
}
}
export default function* root() {
yield all([fork(watchFetchMovies), fork(watchFetchMovie)]);
}
/***********************************************
***************** UPDATE BELOW *****************
***********************************************/
我会告诉你我在哪里调度我的行动;)
...
class Movie extends Component {
constructor(props) {
super(props);
this.state = { isSlideShowOpened: false };
const movie = props.app.movie.results.filter(e => e.id === Number(props.match.params.movieId));
if (movie.length === 0) {
props.fetchMovie(props.match.params.movieId); // <---- I dispatch here because componentWillMount is deprecated :s
}
}
...
...
class Movies extends PureComponent {
constructor(props) {
super(props);
this.state = {};
if (props.app.movies.results.length === 0) {
props.fetchMovies(); // <---- I dispatch here because componentWillMount is deprecated :s
}
}
...
【问题讨论】:
-
你应该试试
yield。它允许您在 saga 中发出异步请求。
标签: reactjs redux redux-saga