【问题标题】:Use recompose setStatic and connect it to redux (nextjs)使用 recompose setStatic 并将其连接到 redux (nextjs)
【发布时间】:2019-04-18 09:24:00
【问题描述】:

我正在使用以下组合,并期望 a 作为道具传递。虽然它是未定义的。

请问如何解决?

import { setStatic, compose } from 'recompose'
import { connect } from 'react-redux'

export const getInitialProps = function() {
  console.log(this.props.a);
}

const mapDispatchToProps = (dispatch) => ({
  a: url => dispatch({type: 'a'})
})

export default compose(
  connect(null, mapDispatchToProps),
  setStatic('getInitialProps', getInitialProps),
)

【问题讨论】:

  • this 未定义或a 是?
  • 很好的问题。这似乎已定义。道具不是。
  • 我认为你在构造函数中缺少这个绑定,或者你需要在 getintialprops 中传递这个
  • 也许将 getInitialProps 更改为箭头函数并尝试。我认为您在 getInitialProps 中丢失了这个上下文,因为它是正常功能
  • 箭头函数即使this消失了

标签: reactjs redux next.js recompose


【解决方案1】:

以下是调查结果: getInitialProps 在服务器端被调用,这意味着它是在挂载到 redux 之前的方式。我尝试注入的道具在 componentDidMount 生命周期中可供我使用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2021-12-11
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    相关资源
    最近更新 更多