如何在ReactJS中添加或删除新信息时再次调用状态而不需要手动刷新页面?

我一直面临一个问题。我已经尝试解决它了。但是,我不知道如何让它发挥作用。我知道我的问题是在推送或删除 API 后我没有重新更新列表新状态?我怎样才能解决这个问题?这样我就不用手动刷新页面了?

我有 2 个组件,但在这篇文章中,我将仅展示我用于删除并从 API 读取这篇文章的组件,该组件称为“产品”。PostForm 组件是一个单独的组件,导入到创建/删除组件中。

我在 setState 中添加了这段代码以进行删除。我想我需要一个函数来重新更新状态,但不知道如何操作。希望得到帮助 // ProductList: filter((item) => item.id !== ProductId),

class Products extends Component {

  state = {

    productList: [],

    statusMsg: "",

  };


  // READ

  getDataFromProductsApi() {

    axios

      .get("https://localhost:44366/api/Products/")

      .then((res) => {

        console.log(res.data);

        this.setState({

          productList: res.data,

        });

      })

      .catch((error) => {

        console.log(error);

        this.setState({ statusMsg: "Error retreiving data" });

        if (axios.isCancel(error)) return;

      });

  }

  componentDidMount() {

    this.getDataFromProductsApi();

  }


  // DELETE


  deleteProduct = (productId, productName) => {

    if (window.confirm("Are you sure? you want to delete")) {

      axios

        .delete(`https://localhost:44366/api/Products/${productId}`)

        .then((response) => {

          console.log(response);

          this.setState({

            statusMsg: `Product name: ${productName} With the ID: ${productId} was removed!`,

            // productList: filter((item) => item.id !== productId),

          });

        });

    }

  };


  showProducts = () => {

    return (

      <table className="customers">

        <tbody>

          <tr>

            <th>ID</th>

            <th>Name</th>

            <th>Category</th>

            <th>Price</th>

            <th>Delete</th>

          </tr>

青春有我
浏览 120回答 1
1回答

泛舟湖上清波郎朗

你的意思是这样的吗?https://jsfiddle.net/bq8h7ns9/class Products extends React.Component {&nbsp; state = {&nbsp; &nbsp; productList : [],&nbsp; &nbsp; statusMsg: "",&nbsp; };&nbsp; // READ&nbsp; getDataFromProductsApi() {&nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; productList: [{ id : 0, name: 'blah1', price: 12.3, category : 'blah' }, { id : 1, name: 'blah2', price: 32.1, category : 'blah' }],&nbsp; &nbsp; });&nbsp; }&nbsp;&nbsp;&nbsp; componentDidMount() {&nbsp; &nbsp; this.getDataFromProductsApi();&nbsp; }&nbsp; // DELETE&nbsp; deleteProduct = (productId, productName) => {&nbsp; &nbsp; if (window.confirm("Are you sure? you want to delete")) {&nbsp; &nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; &nbsp; statusMsg: `Product name: ${productName} With the ID: ${productId} was removed!`,&nbsp; &nbsp; &nbsp; &nbsp; productList: this.state.productList.filter((item) => item.id !== productId),&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; };&nbsp; showProducts = () => {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <table className="customers">&nbsp; &nbsp; &nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>ID</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Category</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Price</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Delete</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {this.state.productList.map((product) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr key={product.id}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{product.id}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{product.name}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{product.category}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{product.price}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onClick={() => this.deleteProduct(product.id, product.name)}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; X&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })}&nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; );&nbsp; };&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <header className="App-header">&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h1>Products</h1>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div> {this.state.statusMsg}</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {this.showProducts()} {/*running function to show data view */}&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; );&nbsp; }}您已经在更新组件的状态;就是这样setState。您注释掉的行应该类似于this.state.productList.filter((item) => item.id !== productId),filter不是全局函数。如果您希望组件与 api 保持同步,那么您将需要使用 websockets 或长轮询之类的东西。HTTP 是一种无状态协议。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript