【问题标题】:React with Redux Warning: Failed propType: Required prop `posts` was not specified in `Posts`. Check the render method of `PostsContainer`对 Redux 警告做出反应:propType 失败:在 `Posts` 中未指定必需的 prop `posts`。检查 `PostsContainer` 的渲染方法
【发布时间】:2016-06-16 13:04:43
【问题描述】:

我正在尝试使用 redux 在反应组件中显示帖子列表,但收到以下警告:

警告:propType 失败:Posts 中未指定必需的 prop posts。检查PostsContainer的渲染方法。

我有以下代码:

帖子容器:

import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
import { getInitalPosts } from 'actions/postsActions'
import { Link } from 'react-router'
import _ from 'lodash'
import Posts from 'components/PostApp/Posts'

class PostsContainer extends Component {
  static fetchData({ store }) {
    return store.dispatch(getInitalPosts())
  }

  componentDidMount() {
    this.props.getInitalPosts()
  }
  render() {
    return (
      <div>
        <h2>Posts</h2>
        <Posts posts={this.props.posts} />
        <Link to="/">Back to Home</Link>
      </div>
    )
  }
}

function mapStateToProps (state) {
  return { posts: state.posts }
}

export { PostsContainer }
export default connect(mapStateToProps, { getInitalPosts })(PostsContainer)

和帖子组件:

import React, { Component, PropTypes } from 'react'
import { List } from 'immutable'

class Posts extends Component {

  render() {
    return (
      <div>
        Posts component
        {  
          this.props.posts
        }
      </div>
    )
  }
}

Posts.propTypes = {

  posts: PropTypes.instanceOf(List).isRequired
  //posts: PropTypes.posts
}

export default Posts

我做错了什么?

[编辑]

这里是post reducer:

import * as ActionType from 'actions/postsActions'
import Immutable from 'immutable'

let defaultState = Immutable.fromJS([])
function postsReducers (state = defaultState, action) {
  switch(action.type) {
    case ActionType.INITIAL_POSTS:
      return Immutable.fromJS(action.response)
      break
    default:
      return state
  }
}

export default postsReducers

就导入容器而言,它位于路由索引中:

 8  import Questions from 'containers/Questions'
    9  import Question from 'containers/Question'
   10: import Posts from 'containers/Posts'
   11  
   12  export default function(history) {
   ..
   14      <Router history={history}>
   15        <Route path="/" component={App}>
   16:         <Route path="posts" component={Posts} />
   17          <Route path="questions" component={Questions} />
   18          <Route path="questions/:id" component={Question} />

【问题讨论】:

  • 您应该检查您的状态,例如使用 redux 开发工具。您的状态似乎由immutable.js 组成。如果您的顶级实例是 Map,则必须使用 get 来接收属性:state =&gt; ({ posts: state.get('posts') })
  • 嗯,是的,它是一个不可变的列表。

标签: javascript reactjs ecmascript-6 redux


【解决方案1】:

你是如何导入PostsContainer的?

如果你这样做:

import { PostsContainer } from PostsContainer; 

您不会导入 connected 版本,因此不会有您尝试在 PostsContainer 中使用的 posts 属性。

你能发布导入代码吗?

【讨论】:

    【解决方案2】:

    由于您在componentDidMountPostsContainer 上运行getInitialPosts,因此它第一次呈现时还没有任何帖子,因此this.props.posts 的值是null。这就是传递给Posts 的内容,打破了您的propTypes 要求。

    您可以在PostsContainerrender 方法中添加if 语句,以便仅在有帖子时渲染Posts,否则渲染加载微调器。

    【讨论】:

    • 嗯,是的,它是一个不可变的列表。我正在尝试从以下样板中构建,并且示例中的应用程序不使用 get 方法来呈现组件列表中的特定项目。这是链接(我还在原始问题中发布了减速器):github.com/mz026/universal-redux-template
    • 这是一个通用渲染。我正在尝试在服务器端渲染。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 2015-09-26
    • 2016-03-01
    • 1970-01-01
    相关资源
    最近更新 更多