【问题标题】:Problems with "withRouter" and redux's "connect()"“withRouter”和 redux 的“connect()”问题
【发布时间】:2017-11-07 17:59:04
【问题描述】:

我正在尝试使用我的 react-redux 应用程序实现 react-router,但我收到以下错误消息:

Layout(...): 渲染没有返回任何内容。这通常意味着缺少 return 语句。或者,不渲染任何内容,返回 null。

我从 redux 网站上复制粘贴了 tutorial 中的所有内容,并添加了我已经存在的使用 redux 的组件。

我的 Layout.js:

import React from "react"
import { connect } from "react-redux"
import { withRouter } from 'react-router-dom'

import { fetchIdeas } from "../actions/ideasActions"

import Ideas from "../components/Ideas"


class Layout extends React.Component {
  componentWillMount() {
    this.props.dispatch(fetchIdeas())
  }

  render() {
    const { ideas } = this.props;
    return 
      <div>
        <h1>Test</h1>
        <Ideas ideas={ideas}/>
      </div>
  }
}

export default withRouter(
  connect((store) => {
	return {
	  ideas: store.ideas,
	};
  })(Layout)
)

我到底做错了什么?我找不到我的错误。

【问题讨论】:

  • 不确定 Ideas 组件中有什么。如果你注释掉它会运行吗?

标签: javascript reactjs react-router react-redux react-router-v4


【解决方案1】:

经典的 JavaScript 错误。 return 不能这样在自己的线路上。

return // returns undefined
  <div>
    <h1>Test</h1>
    <Ideas ideas={ideas}/>
  </div>

修复!

return (
  <div>
    <h1>Test</h1>
    <Ideas ideas={ideas}/>
  </div>
)

【讨论】:

  • 啊,Azium 是对的。如果您将“div”移动到与返回相同的行上,它也会起作用
  • @user2465134 是的,但看起来很难看:p 我建议更漂亮以获得更大的好处github.com/prettier/prettier
  • @azium 我同意:p
猜你喜欢
  • 2019-06-12
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 2019-11-20
相关资源
最近更新 更多