更新到状态后,React组件不会重新呈现

时间:2018-05-13 20:23:02

标签: javascript reactjs redux react-redux

我有一个表单可以向评论数组this.state.comments提交新评论。

在同一个组件中,我想呈现这些新评论。但由于某些原因,当我添加新评论时,我的组件不会重新渲染。当我导航到另一个页面并回到它时,我只能看到它。所以国家肯定会更新。

以下是我的组件,任何想法为什么?

class SinglePost extends React.Component {
    constructor(props) {
        super(props)
        const id = props.params.id - 1;
        this.state = props.messages[id];

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

    submitHandler(event) {
        event.preventDefault();
        let target = event.target;
        let comment = target.comment.value,
            user = target.user.value,
            timestamp = new Date,
            id = this.state.id,
            cmtId = this.state.comments.length ? (this.state.comments.length + 1) : 1

        this.props.postNewComment({ id, user, comment, cmtId });
    }

    render() {
        const post = this.state;
        let hour = post.timestamp.getHours();
        hour = hour > 12 ? `${hour - 12}` : hour === 0 ? '12' : `${hour}`;

        let minute = post.timestamp.getMinutes();
        minute = hour > 12 ? `${minute}pm` : `${minute}am`;

        return (
            <div>
                <Link to='/'><button className="btn btn-secondary">Back to Posts</button></Link>
                <h3>{post.title}</h3>
                <p>By: {post.user} on {hour}:{minute}</p>
                <p>{post.message}</p>
                <h4>Responses</h4>
                {post.comments.length > 0 && post.comments.map(cmt => <SingleCommentBox cmt = {cmt} key = {cmt.id}/>)}
                <div>
                    <form id="new-comment-form" onSubmit={this.submitHandler}>
                        <label className="required">Message:</label>
                        <textarea
                            className="form-control"
                            name="comment"
                            type="text"
                            required />
                        <label className="required">User:</label>
                        <input
                            className="form-control"
                            name="user"
                            type="text"
                            required />
                        <button className="btn btn-success" type="submit">Post Reply</button>
                    </form>
                </div>
            </div>
        )
    }
}

const mapStateToProps = state => {
    return {
        messages: state.messages.messages
    }
}

const mapDispatchersToProps = {
    postNewComment
}

export default connect(mapStateToProps, mapDispatchersToProps)(SinglePost)

repo的其余部分。

2 个答案:

答案 0 :(得分:0)

看起来你正在将状态初始化为构造函数中的props,但是当props更改时永远不会更新状态,因此状态永远不会改变。对我来说,就像你根本不需要使用state一样。

这样的话,你不需要构造函数:

class SinglePost extends React.Component {

    submitHandler = (event) => {
      event.preventDefault();
      let target = event.target;
      let comment = target.comment.value,
        user = target.user.value,
        timestamp = new Date(),
        id = this.props.params.id - 1;
      const comments = this.props.messages[id].comments;
      const cmtId = comments.length ? (comments.length + 1) : 1;

      this.props.postNewComment({ id, user, comment, cmtId });
    }

    render() {
      const id = props.params.id - 1;
      const post = this.props.messages[id];
      let hour = post.timestamp.getHours();

答案 1 :(得分:0)

我同意奥斯汀的说法,你不应该在这里使用州。您的输入不受控制(您不会处理来自父组件的输入的状态更改)。

但是,如果您这样做,请使用componentWillReceiveProps

componentWillReceiveProps(nextProps) {
  const id = nextProps.params.id - 1;
  this.setState(() => ({post: nextProps.messages[id]});
}

另一个建议。你实际上可以传下去&#39; post&#39;通过访问ownProps并传入id,从mapStateToProps函数中获取。一个例子:

const mapStateToProps = (state, ownProps) => {
 return {
   post: state.messages[ownProps.id - 1)
  }
 }    

这里的主要优点是你不会将整个消息数组传递给每个组件,如果你在一个页面上有很多这些组件,这可能对性能有害。

相关问题