我正在尝试通过使用‘next.js - redux -wrapper’来将我的next web应用程序与redux连接起来。
我能够从getInitialProps函数从服务器获取数据,但是在用next-redux-wrapper包装我的_app.js之后,getInitialProps函数似乎不能工作。
我在这里做错了什么?我如何才能修复它?
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)发布于 2020-05-06 23:10:16
在由zeit / next.js提供的以下示例中也存在相同的问题。参见https://github.com/zeit/next.js/tree/master/examples/with-redux-wrapper在本例中,函数"getInitialProps“也不会被调用。因此,自述文件中提到的时间直接预先呈现在服务器上的效果是不可见的。
https://stackoverflow.com/questions/61453832
复制相似问题