简体   繁体   English

更新UI中未正确反映的状态

[英]Updating state not reflecting correctly in UI

I am working on an application that allows users to post and like messages on a page. 我正在开发一个允许用户在页面上发布和喜欢消息的应用程序。 The application orders the posts from newest to oldest. 该应用程序将帖子从最新到最旧命令。 The issue I am running in to is, if for example there are ten posts on the page and the top one is liked, and then a new post is created - the new post is added to the top of the page and the likes from the previous topmost post carries over to the new post (which should have zero likes by default) - incorrectly rendering likes on the page. 我正在运行的问题是,如果例如页面上有十个帖子并且顶部的帖子是喜欢的,则会创建一个新帖子 - 新帖子将添加到页面顶部,而来自上一个最顶层的帖子会转到新帖子(默认情况下应该为0) - 错误地在页面上呈现相似内容。

Note, posts and likes are updated correctly on the server side and the state is updated correctly to reflect these changes, but the state and the UI aren't in sync as they should be following the execution of the onSubmit() method in the CreatePostForm component. 注意,帖子和喜欢在服务器端正确更新,状态正确更新以反映这些更改,但状态和UI不同步,因为它们应该在CreatePostForm中执行onSubmit()方法之后零件。

Did anyone else run into a similar issue? 有没有其他人遇到过类似的问题? I appreciate any suggestions on how to resolve. 我很感激有关如何解决的任何建议。

PostsPage.js PostsPage.js

class PostsPage extends React.Component {

    constructor(props) {
        super(props);

        this.state = {
            posts: [],
            user: this.props.auth.user,
            location: "main"
        };

        this.loadPosts = this.loadPosts.bind(this);
        this.loadPosts();
    };


    loadPosts() {

        this.props.loadPostsRequest(this.state).then(
            (results) => {
                console.log(results.data.posts);
                this.setState({
                    posts: results.data.posts
                });
            });
    }



    render() {
        const { createPostRequest } = this.props;

        const posts = this.state.posts.map( (post, i) =>
            <Post     
                loadPostsRequest={loadPostsRequest}
                key={i}
                postId={post.postId}
                userId={this.state.user.userId}
                content={post.postBody}
            />
        );

        return (
            <div className="container-fluid" id="posts-container">
                <div className="row">
                    <div className="col-md-4 col-md-offset-4 posts-wrapper">

                        <div id="create-post-form">
                            <CreatePostForm
                                createPostRequest={createPostRequest}
                                history={this.props.history}
                                loadPosts={this.loadPosts}
                            />
                        </div>
                        { posts }
                    </div>
                </div>
            </div>
        );
    }
}

CreatePostForm.js CreatePostForm.js

export class CreatePostForm extends React.Component {

    constructor(props) {
        super(props);

        this.state = {
            postBody : "",
            user: this.props.auth.user
        };

        this.onSubmit = this.onSubmit.bind(this);
        this.onChange = this.onChange.bind(this);
    }

    onChange(e) {
        this.setState({
            [e.target.name] : e.target.value
        });
    }

    onSubmit(e) {
        e.preventDefault();

        this.props.createPostRequest(this.state).then(
            () => {
                this.props.loadPosts();
            })
    }

    render() {

        return (
            <div className="create-post-inner col-md-12">
                <form id="createPost" onSubmit={ this.onSubmit } >
                    <textarea value={this.state.postBody} className="form-control postInput" name="postBody" onChange={ this.onChange } placeholder="Write something on this page..." >
                    </textarea>
                    <input type="submit" className="submit btn btn-primary" />
                </form>
            </div>
        );
    }
}

Don't use index for a key in your posts map. 不要在帖子地图中使用index作为key When a new post shows up the new post has the same key as the previous post so React treats it as the same element. 当新帖子显示时,新帖子与上一篇文章具有相同的key ,因此React将其视为相同的元素。 Assuming postId is unique to each post, use that instead. 假设postId对每个帖子都是唯一的,请改用它。

https://facebook.github.io/react/docs/lists-and-keys.html https://facebook.github.io/react/docs/lists-and-keys.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM