简体   繁体   English

反应表不显示数据

[英]react-table not showing data

I am new in React and JS.我是 React 和 JS 的新手。 Trying to use react-table and sending data from class to function.尝试使用 react-table 并将数据从 class 发送到 function。 Data coming from props is showing in console but not appearing in react-table.来自 props 的数据显示在控制台中,但未出现在 react-table 中。 Below is the code and result of console.log.下面是 console.log 的代码和结果。 Please tell my mistake and what topic do I cover to improve.请告诉我的错误以及我要改进的主题。

Note: If replace this line:注意:如果替换此行:

const result = props.state;

with:和:

const result = props.state.data;

show error data is undefined .显示错误data is undefined

function Showdata(props){
    // Result of below console.log is in attached image.
    console.log(props.state.data);
    const columns = useMemo(
        () => [
                {
                    Header: 'Column 1',
                    accessor: 'props.state.data.id', // accessor is the "key" in the data
                },
                {
                    Header: 'Column 2',
                    accessor: 'props.state.data.TeamName',
                },
            ],
        []
    );

    const [data, setData] = useState([]);

    useEffect(() => {
        (async () => {
            /*If replace below line with const result = props.state.data; show error "data is undefined".*/
            const result = props.state;
            setData(result.data);
            console.log(result.data);
        })();
    }, []);

    return (
        <div className="App">
            <Table columns={columns} data={data} />
        </div>
    );


const {
  getTableProps,
  getTableBodyProps,
  headerGroups,
  rows,
  prepareRow,
} = useTable({ columns, data });

    return (
        <div>
            <p>Data of {props.state.category}</p>
  <table {...getTableProps()}>
    <thead>
      {headerGroups.map(headerGroup => (
        <tr {...headerGroup.getHeaderGroupProps()}>
          {headerGroup.headers.map(column => (
            <th {...column.getHeaderProps()}>{column.render('Header')}</th>
          ))}
        </tr>
      ))}
    </thead>
    <tbody {...getTableBodyProps()}>
      {rows.map(row => {
        prepareRow(row)
        return (
          <tr {...row.getRowProps()}>
            {row.cells.map(cell => {
              return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
            })}
          </tr>
        )
      })}
    </tbody>
        </table>
    </div>
  )
}

控制台日志结果

I see mistake in your columns :我在您的columns中看到了错误:

const columns = useMemo(
  () => [
    ...
      accessor: 'props.state.data.id',
    ...
      accessor: 'props.state.data.TeamName',
    ...
);

There can't be accessor like props.state.data.id .不能有像accessor这样props.state.data.id In your console I see array of data objects like:在您的控制台中,我看到了一系列数据对象,例如:

[{ id: ..., TeamNameL ... }]

And you should give this fields like:你应该给这个字段,比如:

const columns = useMemo(
  () => [
    ...
      accessor: 'id',
    ...
      accessor: 'TeamName',
    ...
);

Also you can see official example .您也可以查看官方示例

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

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