简体   繁体   English

我们如何在 ANT D 列中获取/重新呈现异步数据

[英]How can we get/rerender async data in ANT D Column

I need to fetch the data from ant d table column using the text in the column and rerender the result but my code isn't working it this possible?我需要使用列中的文本从 ant d 表列中获取数据并重新呈现结果,但是我的代码无法正常工作吗?

const columns = [
    {
        title: "Stocks On Hand",
        dataIndex: "key",
        width: "33.333333333%",
        render: async function render(text) {
            //console.log(warehouse)
            const data =  await dispatch(getStocksDetails(text,warehouse))
            // console.log(data)
            return (
                <>
                   {
                      <Text>{data.qty}</Text>
                   }
                </>
            )
        },
    }, 

no data is being displayed没有数据显示

You can consider storing the entire column data into the component state.您可以考虑将整个列数据存储到组件 state 中。 Then let the async function modify that state, so when the promise is resolved, it will update the state and trigger a rerender for your entire column. Then let the async function modify that state, so when the promise is resolved, it will update the state and trigger a rerender for your entire column.

The catch is you will need to manually handle the loading state of your table component since the stable version of the Suspense API does not support data-fetching at the moment.问题是您需要手动处理表组件的加载 state,因为 Suspense API 的稳定版本目前不支持数据获取。

It goes something like它就像

const MyComponent = () => {
  const [data, setData] = useState([]);
  const asyncFn = async () => {
    // do your data fetching here
    await fetchDataStuff().then((response)=>setData(response));
  }
  
  useEffect(()=> {
      asyncFn();
  }, [asyncFn]);

  return (<><column>{data}</column></>);

}

Edit: I added the useEffect dependency to prevent the component from fetching data at every render.编辑:我添加了useEffect依赖项以防止组件在每次渲染时获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM