最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i re-render ant Table - Stack Overflow

programmeradmin0浏览0评论

I using Table from ant desing. I have question about how can i re-render table datasource when i update the state ? I add state to dataSource like :

<Table
            rowKey="Id"
            columns={columns}
            dataSource={documents.Data}
            className="w-full"
          />

And after i update my state like :

const UpdateDocument = (row) => {
    let fakeList = { ...documents };
    let item = fakeList.Data.find((x) => x.Id === row.Id);
    let index = fakeList.Data.indexOf(item);
    fakeList.Data[index] = row;
    setDocuments(fakeList); //I update my state here. 
  };

But its not re-render table datasource. After i update my state shouldn't it re-render too ? Where do i make wrong ? Thanks for replies!

I using Table from ant desing. I have question about how can i re-render table datasource when i update the state ? I add state to dataSource like :

<Table
            rowKey="Id"
            columns={columns}
            dataSource={documents.Data}
            className="w-full"
          />

And after i update my state like :

const UpdateDocument = (row) => {
    let fakeList = { ...documents };
    let item = fakeList.Data.find((x) => x.Id === row.Id);
    let index = fakeList.Data.indexOf(item);
    fakeList.Data[index] = row;
    setDocuments(fakeList); //I update my state here. 
  };

But its not re-render table datasource. After i update my state shouldn't it re-render too ? Where do i make wrong ? Thanks for replies!

Share Improve this question asked Sep 11, 2021 at 7:30 juniorFrontEndDDjuniorFrontEndDD 511 gold badge1 silver badge4 bronze badges 3
  • @Yushan sure : codesandbox.io/s/vibrant-thunder-i4hn8?file=/src/App.js – juniorFrontEndDD Commented Sep 11, 2021 at 10:44
  • @Yushan as you can see state is changing but table view is still same. I want when i change state table view change too as i made. – juniorFrontEndDD Commented Sep 11, 2021 at 10:45
  • I have a same issue too. My data es from firestore snapshot and the table is not re-rednering – juniortan Commented Mar 22, 2023 at 14:19
Add a ment  | 

1 Answer 1

Reset to default 3

You are updating the same reference, you need to render a copy or your ponent will not render.

A working example based on your codesandbox:

import "./styles.css";
import React, { useState } from "react";
import { Button, Table } from "antd";

const fakeData = {
  Data: [
    {
      Name: "Test 1",
      isOpen: true
    },
    {
      Name: "Test 2",
      isOpen: true
    },
    {
      Name: "Test 3",
      isOpen: true
    },
    {
      Name: "Test 4",
      isOpen: true
    }
  ]
};
export default function App() {
  const [newData, setnewData] = useState(fakeData);

  const changeState = () => {
    const fakeItem = { Name: "Fake test", isOpen: true };
    const newList = { Data: []};
    const datas = fakeData.Data.map((data) =>
      data.Name === "Test 3" ? fakeItem : data
    );
    newList.Data = datas;
    setnewData(newList);
  };
  const columns = [
    {
      title: "Name",
      dataIndex: "Name",
      key: "name"
    }
  ];

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <Table rowKey="Name" columns={columns} dataSource={newData.Data} />
      <Button onClick={changeState}>Change State</Button>
    </div>
  );
}
发布评论

评论列表(0)

  1. 暂无评论