【问题标题】:How to make Next.js server waiting until action creator is fully done?如何让 Next.js 服务器等到动作创建者完全完成?
【发布时间】:2018-09-27 15:27:42
【问题描述】:

我将 Next.jsRedux 一起使用,我需要等到动作创建者完全完成(包括对外部服务器的请求),这样数据就已经完成了在初始渲染中。我会试着用例子来解释这个案例。

我在文档中使用来自 example 的 HOC,并用它包装 page

所以,下面你可以看到代码示例:

index.jspages 文件夹中。

import withRedux from '../utils/withRedux'
import RunningLineContainer from '../src/containers/news/RunningLineContainer'

const IndexPage = () => <RunningLineContainer />

export default withRedux()(IndexPage)

RunningLineContainer.jsx

import React, { Component } from 'react'
import { connect } from 'react-redux'

import RunningLine from '../../components/RunningLine'
import { fetchLatestNews } from '../../actions/wp_news'

class RunningLineContainer extends Component {

    componentDidMount() {
        if (!this.props.lastestNews) {
            this.props.fetchLatestNews('&count=6')
        }
    }

    render() {
        const { latestNews, isLoading } = this.props
        return <RunningLine data={latestNews} isLoading={isLoading} />
    }
}

const mapStateToProps = ({ newsState }) => {
    const { latestNews, isLoading } = newsState
    return {
        latestNews,
        isLoading
    }
}

export default connect(mapStateToProps, { fetchLatestNews })(RunningLineContainer)

目前,请求是在客户端发出的,因此来自请求的数据不会被服务器呈现,数据内容也不会被搜索引擎看到。我的目标是让这些数据在初始加载时得到响应(由服务器呈现)。

下面的动作创建者代码:

export function fetchLatestNews(params) {
    return function (dispatch) {
        dispatch({ type: newsTypes.FETCH_WP_LATEST_NEWS_REQUEST })
        newsApi.fetchLatestNews(params)
            .then(response => {
                dispatch({
                    type: newsTypes.FETCH_WP_LATEST_NEWS_SUCCESS,
                    payload: response.data,
                })
            })
            .catch(error => {
                dispatch({
                    type: newsTypes.FETCH_WP_LATEST_NEWS_FAILURE,
                    payload: error,
                })
            })
    }
}

我尝试在 index.js page 中使用静态方法 getInitialProps,如下所示:

import withRedux from '../utils/withRedux'
import RunningLineContainer from '../src/containers/news/RunningLineContainer'

const IndexPage = () => <RunningLineContainer />

IndexPage.getInitialProps = async ({ store }) => {
    await store.dispatch(fetchLatestNews())
}

export default withRedux()(IndexPage)

不幸的是,它对我不起作用。

我该如何解决这个任务?有什么解决办法吗?

提前致谢!

【问题讨论】:

    标签: javascript reactjs redux async-await nextjs


    【解决方案1】:

    您的动作创建者不会返回 Promise,因此它不能是 awaited。修复:

    export function fetchLatestNews(params) {
        return function (dispatch) {
            dispatch({ type: newsTypes.FETCH_WP_LATEST_NEWS_REQUEST })
    
            // return statement
            return newsApi.fetchLatestNews(params)
                .then(response => {
                    dispatch({
                        type: newsTypes.FETCH_WP_LATEST_NEWS_SUCCESS,
                        payload: response.data,
                    })
                })
                .catch(error => {
                    dispatch({
                        type: newsTypes.FETCH_WP_LATEST_NEWS_FAILURE,
                        payload: error,
                    })
                })
        }
    }
    

    【讨论】:

    • 感谢您的帮助!我已经发布了完整的答案。
    【解决方案2】:

    终于,我解决了这个任务。

    实际的解决方案是让action creator中的返回函数异步,并将await添加到API请求中。

    所以,我像上面提到的那样使用静态方法:

    import withRedux from '../utils/withRedux'
    import RunningLineContainer from '../src/containers/news/RunningLineContainer'
    
    const IndexPage = () => <RunningLineContainer />
    
    IndexPage.getInitialProps = async ({ store }) => {
        await store.dispatch(fetchLatestNews())
    }
    
    export default withRedux()(IndexPage)  
    

    我的动作创建者如下所示:

    export const fetchLatestNews = params => {
        return async dispatch => {
            dispatch({ type: newsTypes.FETCH_WP_LATEST_NEWS_REQUEST })
            await newsApi.fetchLatestNews(params)
                .then(response => {
                    dispatch({
                        type: newsTypes.FETCH_WP_LATEST_NEWS_SUCCESS,
                        payload: response.data,
                    })
                })
                .catch(error => {
                    dispatch({
                        type: newsTypes.FETCH_WP_LATEST_NEWS_FAILURE,
                        payload: error,
                    })
                })
        }
    }
    

    【讨论】:

    • @thanks,但是为什么我需要打电话给store.dispatch(fetchLatestNews()) 为什么不用await linkLead3(data)
    • @AbdullaZulqarnain linkLead3 是什么?
    • 抱歉,它是await fetchLatestNews(),因为在内部我们已经调用了dispatch,但是为什么我们需要**dispatch`操作...?
    猜你喜欢
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多