React Ant table customized column calculation. How to get the length of other items in each row

此生再无相见时 提交于 2020-04-18 03:47:47

问题


I made an application where I output the length of the questions for each post, but I get the result for each post equal with: 310.

Probably it happens because of too many renders.
How to solve this and get the real length of the questions for each post?

link to my app: https://codesandbox.io/s/basic-usage-ant-design-demo-4tl5h


回答1:


Fix the incorrect number: do not push, use callback event of render instead

Ant Table API: table Column

const App = () => {
  const res = [
    ...Object.keys(posts[0]).map(i => {
      return {
        title: i,
        dataIndex: i,
        key: i
      };
    }),
    {
      title: "Questions",
      key: "Questions",
      render: e => {
        console.log(e); // Object {title: 1, id: "123"}
        return (
          <Router>
            <Link to={`demo/${e.id}/url`}>
              {questions.filter(q => q.id_post.toString() === e.id).length}
            </Link>
          </Router>
        );
      }
    }
  ];
  return <Table columns={res} dataSource={posts} />;
};



来源:https://stackoverflow.com/questions/60829209/react-ant-table-customized-column-calculation-how-to-get-the-length-of-other-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!