我在反應功能組件中使用 AgGrid 顯示一些數據。對于作為對象的值,我試圖顯示它們的 json 值,我在 colDefs 中使用了 valueFormatter 選項,如下面的代碼所示。但是當我使用這個時,我得到一個錯誤:Error: ag-Grid: cannot get grid to draw rows when it is in the middle of drawing rows. Your code probably called a grid API method while the grid was in the render stage. To overcome this, put the API call into a timeout, eg instead of api.refreshView(), call setTimeout(function(){api.refreshView(),0}). To see what part of your code that caused the refresh check this stacktrace.如果我不使用 valueFormatter 選項,則不會出現上述錯誤。我知道我需要根據錯誤在準備好網格時加載網格,但我不確定如何在反應功能組件中執行此操作。代碼示例:import React, { useState } from "react";import { AgGridReact, ICellRendererReactComp } from "ag-grid-react";function Test(props) { const [rowData, setRowData] = useState(null); const defaultColDef = { sortable: true, filter: true, resizable: true, }; const columnDefs = [ { headerName: 'Sample Key', field: 'sampleKey', valueFormatter: (val) => JSON.stringify(val), }, //OTHER COLDEFS ] return ( <div className="container-fluid"> <div className="card mt-2 p-3 bg-red shadow p-3 mb-5 bg-white rounded"> <div className="ag-theme-balham" style={{ height: "500px", width: "100%" }} > <AgGridReact columnDefs={columnDefs} rowData={rowData} ></AgGridReact> </div> </div> </div> );}export { Test };
在反應功能組件中添加 valueFormatter 時,Ag Grid 無法加載
胡子哥哥
2022-10-21 10:29:08