简体   繁体   English

将 Next.js 与 next-redux-wrapper 一起使用时,getInitialProps 似乎不起作用

[英]getInitialProps doesn't seem to work when using Next.js with next-redux-wrapper

I'm trying to hook my next.js web app with redux by using 'next-redux-wrapper' HOC.我正在尝试使用“next-redux-wrapper”HOC 将 next.js web 应用程序与 redux 应用程序挂钩。

I was able to fetch data from server from getInitialProps function but after wrapping my _app.js with next-redux-wrapper the getInitialProps functions doesn't seem to work.我能够从 getInitialProps function 从服务器获取数据,但是在用 next-redux-wrapper 包装我的 _app.js 之后,getInitialProps 函数似乎不起作用。

What do I do wrong here and how can I fix it?我在这里做错了什么,我该如何解决?

import React from 'react'
import fetch from 'isomorphic-unfetch'
import { connect } from 'react-redux'
import { getItems } from '../../store/actions/itemAction'

const Index = (props) => {
  console.log(props)

  return ()
}

Index.getInitialProps = async () => {
  let items
  await fetch('http://localhost:5000/item')
    .then(res => res.json())
    .then(data => { items = data })
    .catch(err => console.log(err))

  return { items }
}

const mapStatetoProps = state => ({
  item: state.item,
})

export default connect(mapStatetoProps, null)(Index)

The same problem also exists in the following example provided by zeit / next.js. zeit/next.js提供的以下示例也存在同样的问题。 See https://github.com/zeit/next.js/tree/master/examples/with-redux-wrapper In this example the function "getInitialProps" is not called either.https://github.com/zeit/next.js/tree/master/examples/with-redux-wrapper在这个例子中,function“getInitialProps”也没有被调用。 The effect mentioned in the readme, that the time is pre-rendered directly on the server, is therefore not visible.自述文件中提到的效果,即时间直接在服务器上预渲染,因此是不可见的。

I think you have to add getInitialProps on what the wrapper HOC withRedux spits out, and call it's original getInitialProps, because it's being used.我认为您必须在包装器 HOC withRedux 吐出的内容上添加 getInitialProps,并将其称为原始 getInitialProps,因为它正在被使用。 I didn't figure out how to do it though我不知道该怎么做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM