问题
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