POST请求后,子组件不会重新渲染父组件

squeekyDave

发布新评论时,我想重新渲染评论列表。当我从子组件发布评论时,我的父组件中的状态设置为false,我想更新该状态。我可以在POST请求后console.log状态,我可以看到它从false变为true,但是它没有重新呈现我的父组件。到目前为止,只有当我手动刷新页面时,我才能看到新评论。我尝试通过父级的回调函数更改状态,但是它不起作用。我相信这可能是一个异步问题,但我不知道如何解决。

任何想法有什么问题吗?

父母

import React, { Component } from 'react';
import PostCommentForArticle from './PostCommentForArticle'
import axios from 'axios';
import './Comments.css'

class CommentsForArticle extends Component {
  state = {
    isPosted: false
  }
  render() {
    console.log(this.state);
    return (
      <div>
        {this.props.comments.map(comment => {
          return (
            <div class="container" id="container-comments">
              <div class="row">
                <div class="col-sm">
                  {comment.created_by}
                </div>
                <div class="col-sm">
                  {comment.created_at}
                </div>
              </div>
              <div class="row-combody">{comment.body}</div>
              <div class="row-belongsto">{comment.belongs_to}</div>
              <div class="row-votes">{comment.votes}
                <i class="far fa-thumbs-up" id="icon" onClick={() => {
                  this.incrementCommentVote(comment._id)
                  this.setState({ currCommentVote: comment.votes++ })
                }}></i>
                <i class="far fa-thumbs-down" id="icon" onClick={() => {
                  this.decrementCommentVote(comment._id)
                  this.setState({ currCommentVote: comment.votes-- })
                }}></i>
                <i class="far fa-times-circle" id="icon" onClick={() => {
                  this.deleteComment(comment._id)
                  this.setState({ isCommentDeleted: true })
                }}></i>
              </div>
            </div>
          )
        })}
        <PostCommentForArticle
          article={this.props.article}
          func={this.checkIfPosted} />
      </div>
    );
  }
  incrementCommentVote = (id) => {
    axios
      .put(
        `https://ncnewsapp.herokuapp.com/api/comments/${id}?vote=up`,
        "mytoken",
        { headers: { "Content-Type": "text/plain" } }
      )
      .then(result => console.log(result))
      .catch(e => console.log(e));
    //insert error page component
  }
  decrementCommentVote = (id) => {
    axios
      .put(
        `https://ncnewsapp.herokuapp.com/api/comments/${id}?vote=up`,
        "mytoken",
        { headers: { "Content-Type": "text/plain" } }
      )
      .then(result => console.log(result))
      .catch(e => console.log(e));
    //insert error page component
  }
  deleteComment = (data) => {
    axios
      .delete(
        `https://ncnewsapp.herokuapp.com/api/comments/${data}`,
        "mytoken",
        { headers: { "Content-Type": "text/plain" } }
      )
      .then(result => {
        console.log(result);
      })
      .catch(e => console.log(e));
  }

  checkIfPosted = (data) => {
    console.log(data);
    this.setState({ isPosted: data })
  }

}
export default CommentsForArticle;

儿童

import React, { Component } from 'react';
import Articles from './Article';
import axios from 'axios';
import './PostCommentForArticle.css';

class PostCommentForArticle extends Component {
  state = {
    userName: '',
    input: '',
  }
  render() {
    return (
      <div>
        <form id="post-comment-form">
          <div class="form-group">
            <label for="exampleInputEmail1">Username</label>
            <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter username" value={this.state.userName} onChange={this.getUserName} />
            <small id="emailHelp" class="form-text text-muted">Post a comment.</small>
          </div>

          <div class="input-group">
            <textarea class="form-control" aria-label="With textarea" value={this.state.input} onChange={this.getUserInput}></textarea>
            <br />
          </div>
          <button type="button" class="btn btn-primary" onClick={() => this.postComment()}>Submit</button>
        </form>
      </div>
    );
  }

  getUserName = (event) => {
    this.setState({ userName: event.target.value });
  }
  getUserInput = (event) => {
    this.setState({ input: event.target.value });
  }
  postComment = () => {
    let id = this.props.article[0]._id;
    const { userName } = this.state;
    const { input } = this.state
    axios.post(`https://ncnewsapp.herokuapp.com/api/articles/${id}/comments?user=${userName}`,
      {
        "comment": `${input}`,

      })
      .then((data) => {
        this.props.func(true)
      })
  }

}
export default PostCommentForArticle;
GSSwain

你只是更新标记isPostedCommentsForArticle,但意见从生成this.props.comments您必须确保将新注释添加到comments列表中,当然,无论从何处通过它,否则您都可以考虑将注释添加到CommentsForArticle的状态并添加新注释,而不仅仅是更新标志。

刷新页面时,您会收到这些新注释,因此它将显示所有注释。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React 16.8钩子-更新父状态后子组件不会重新渲染

Polymer / Lit-element,当父级属性修改后,子级组件不会重新渲染

重新渲染兄弟组件后在父组件中渲染子组件

在其上级组件更新其道具后,功能组件子组件不会重新渲染

当父组件的状态发生变化时,子组件不会重新渲染

父功能组件中的子组件不会在 Props 更改时重新渲染

来自子组件的事件处理程序设置状态不会重新渲染父组件

setState 后组件不会重新渲染

子组件不会被重新渲染

在父组件的状态更改时重新渲染子组件

为什么在 React 中,当父组件重新渲染时,子组件不会重新渲染(子组件没有被 React.memo 包裹)?

在父组件componentDidMount完成后渲染子组件

渲染子组件后如何保留父组件事件

即使使用钩子更新了父状态,子组件也不会重新渲染

在 Blazor 中更新父组件后重新加载子组件

更改父级的道具后,React子级组件未重新渲染

在重新渲染父组件时,React如何重用子组件/保持子组件的状态?

父组件重新渲染时如何停止重新渲染子组件

如何从子组件重新渲染父组件

componentWillReceiveProps不会重新渲染组件

我更改状态后,React组件不会重新渲染

状态更改后,React组件不会重新渲染

等待反应上下文后组件不会重新渲染

重新渲染后,样式化组件不会更新<div>

使用 UseState Hook 后 React 组件不会重新渲染

react-rails:创建记录后组件不会重新渲染

反应父组件不渲染子组件

Nuxt 父组件在子组件下渲染

React 16.13.1 - 当 props 改变时子组件不会重新渲染