簡體   English   中英

laravel反應,表單提交后數據需要刷新

[英]laravel react, data need refresh after form submit

我已經制作了一個帶有2列部分的組件。 左邊的是表格,右邊的是顯示數據庫中的所有帖子。 提交表單后,除非重新刷新,否則不會在右側顯示新插入的數據,有人可以幫忙嗎? k

您需要使用componentWillReceiveProps(nextProps)在你的組件,並設置自己的帖子狀態componentWillReceiveProps並自動將您的組件將重新呈現。

例如:

import React from 'react';

class Post extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      posts: [],
    };
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.posts) {
      this.setState({ posts: nextProps.posts });
    }
  }

  render() {
    return (
      <div>
        {this.state.posts.map((post, index) => (
          <p>{post.title}</p>
        ))}
      </div>
    );
  }
}
const mapStateToProps = state => ({
  posts: state.posts,
});

export default Post;

注意:如果您的React版本小於17,這將起作用

如果您使用的是最新版本的react, componentDidUpdate將起作用。 這是比較

這很容易,在存儲到db中之后,從db / api發出請求可以獲取所有相關記錄並顯示所有列表,並更新組件的狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM