如何在react redux表单中成功创建post后刷新列表

时间:2017-01-27 08:38:15

标签: javascript reactjs redux redux-form

我很反应和反应redux并尝试测试https://github.com/rajaraodv/react-redux-blog。我想做的是我想在PostsIndex中整理新博文和博客文章列表,如下所示,

PostsIndex.js

class PostsIndex extends Component {
  render() {
    return (
      <div>
        <HeaderContainer type="posts_index"/>
        <ValidateEmailAlertContainer/>
        <PostsNew />
        <PostsList />
      </div>
    );
  }
}

我的问题是如何在成功保存PostsNew后刷新PostsList。

1 个答案:

答案 0 :(得分:0)

您做出反应的方式是通过propsPostsIndex应保留帖子列表并将其传递给PostsListPostsNew可以作为道具接收回调,并在添加新帖子时调用它。像这样:

  class PostsIndex extends Component {
        constructor() {
            this.state = { posts: [] };
            this.postAdded = this.postAdded.bind(this);
        }

        postAdded(newPost) {
            let { posts } = this.state;
            posts.push(newPost);
            //This will trigger a rerender and the PostList will 
            //receive the new posts list.
            this.setState({posts: posts});
        }

        render() {
            let { posts } = this.state;
            return (
            <div>
                <HeaderContainer type="posts_index"/>
                <ValidateEmailAlertContainer/>
                {/* Inside the PostsNew component you should call this.props.onSave */}
                <PostsNew onSave={this.postAdded}/>
                {/* Here you pass along the list of posts */}
                <PostsList posts={posts}/>
            </div>
            );
        }
    }