Material-UI表格中如何显示动态数据?

我有返回一些表格数据的 API。我需要在表格中显示这些数据。我不清楚如何实现这个目标。


假设我想显示字段id并name存储在groups. 如何在 Material-UI 表中显示它们?


请在下面查看我当前的代码。它不会抛出任何错误。但两者都没有显示包含数据的表格。


import '../../App.css';

import React, { useEffect } from 'react'

import { makeStyles } from '@material-ui/core/styles';

import Grid from '@material-ui/core/Grid';

import Table from '@material-ui/core/Table';

import TableBody from '@material-ui/core/TableBody';

import TableCell from '@material-ui/core/TableCell';

import TableContainer from '@material-ui/core/TableContainer';

import TableHead from '@material-ui/core/TableHead';

import TableRow from '@material-ui/core/TableRow';

import Paper from '@material-ui/core/Paper';

import axios from 'axios'

import config from '../../config/config.json';


const useStyles = makeStyles((theme) => ({

    root: {

        width: '100%',

    },

    heading: {

        fontSize: theme.typography.pxToRem(18),

        fontWeight: theme.typography.fontWeightBold,

    },

    content: {

        fontSize: theme.typography.pxToRem(14),

        fontWeight: theme.typography.fontWeightRegular,

        textAlign: "left",

        marginTop: theme.spacing.unit*3,

        marginLeft: theme.spacing.unit*3,

        marginRight: theme.spacing.unit*3

    },

    table: {

        minWidth: 650,

    },

    tableheader: {

        fontWeight: theme.typography.fontWeightBold,

        color: "#ffffff",

        background: "#3f51b5"

    },

    tableCell: {

        background: "#f50057"

    },

    button: {

        fontSize: "12px",

        minWidth: 100

    },

}));



export function Main() {


    const [groups, setGroup] = React.useState('');


    const classes = useStyles();


    const options = {

        'headers': {

            'Authorization': `Bearer ${localStorage.getItem('accessToken')}`

        }

    }


慕运维8079593
浏览 147回答 3
3回答

慕斯王

我认为问题是你使用this.setState而不是setGroupuseEffect(() => {&nbsp; &nbsp; axios.get(config.api.url + '/api/test', options)&nbsp; &nbsp; &nbsp; &nbsp; .then( (groups) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setGroup(groups)&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .catch( (error) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(error);&nbsp; &nbsp; &nbsp; &nbsp; })}, [])改变你的地图功能{Object.keys(groups).map( (row, index) => (&nbsp; <TableRow key={index} selected="false">&nbsp; &nbsp; <TableCell>{row._id}</TableCell>&nbsp; &nbsp; <TableCell>{row.user_id}</TableCell>&nbsp; </TableRow>))}import '../../App.css';import React, { useEffect } from 'react'import { makeStyles } from '@material-ui/core/styles';import Grid from '@material-ui/core/Grid';import Table from '@material-ui/core/Table';import TableBody from '@material-ui/core/TableBody';import TableCell from '@material-ui/core/TableCell';import TableContainer from '@material-ui/core/TableContainer';import TableHead from '@material-ui/core/TableHead';import TableRow from '@material-ui/core/TableRow';import Paper from '@material-ui/core/Paper';import axios from 'axios'import config from '../../config/config.json';const useStyles = makeStyles((theme) => ({&nbsp; &nbsp; root: {&nbsp; &nbsp; &nbsp; &nbsp; width: '100%',&nbsp; &nbsp; },&nbsp; &nbsp; heading: {&nbsp; &nbsp; &nbsp; &nbsp; fontSize: theme.typography.pxToRem(18),&nbsp; &nbsp; &nbsp; &nbsp; fontWeight: theme.typography.fontWeightBold,&nbsp; &nbsp; },&nbsp; &nbsp; content: {&nbsp; &nbsp; &nbsp; &nbsp; fontSize: theme.typography.pxToRem(14),&nbsp; &nbsp; &nbsp; &nbsp; fontWeight: theme.typography.fontWeightRegular,&nbsp; &nbsp; &nbsp; &nbsp; textAlign: "left",&nbsp; &nbsp; &nbsp; &nbsp; marginTop: theme.spacing.unit*3,&nbsp; &nbsp; &nbsp; &nbsp; marginLeft: theme.spacing.unit*3,&nbsp; &nbsp; &nbsp; &nbsp; marginRight: theme.spacing.unit*3&nbsp; &nbsp; },&nbsp; &nbsp; table: {&nbsp; &nbsp; &nbsp; &nbsp; minWidth: 650,&nbsp; &nbsp; },&nbsp; &nbsp; tableheader: {&nbsp; &nbsp; &nbsp; &nbsp; fontWeight: theme.typography.fontWeightBold,&nbsp; &nbsp; &nbsp; &nbsp; color: "#ffffff",&nbsp; &nbsp; &nbsp; &nbsp; background: "#3f51b5"&nbsp; &nbsp; },&nbsp; &nbsp; tableCell: {&nbsp; &nbsp; &nbsp; &nbsp; background: "#f50057"&nbsp; &nbsp; },&nbsp; &nbsp; button: {&nbsp; &nbsp; &nbsp; &nbsp; fontSize: "12px",&nbsp; &nbsp; &nbsp; &nbsp; minWidth: 100&nbsp; &nbsp; },}));export function Main() {&nbsp; &nbsp; const [groups, setGroup] = React.useState([]);&nbsp; &nbsp; const classes = useStyles();&nbsp; &nbsp; const options = {&nbsp; &nbsp; &nbsp; &nbsp; 'headers': {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Authorization': `Bearer ${localStorage.getItem('accessToken')}`&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }useEffect(() => {&nbsp; &nbsp; &nbsp; &nbsp; axios.get(config.api.url + '/api/test', options)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .then( (groups) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setGroup(groups.data.subtask)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(groups.data.subtask);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .catch( (error) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(error);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }, [])return (&nbsp; &nbsp; <div className={classes.root}>&nbsp; &nbsp; &nbsp; &nbsp; <Grid container spacing={3}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Grid item xs={12} className={classes.content}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TableContainer component={Paper}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Table id="split_table" size="small">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TableHead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TableHead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TableBody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {Object.keys(groups).map( (item, index) => (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TableRow key={index} selected="false">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TableCell>{item.user_id}</TableCell>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TableCell>{item.task_name}</TableCell>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TableRow>))}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TableBody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TableContainer>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Grid>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </Grid>&nbsp; &nbsp; </div>)

慕码人2483693

我认为这是Object.keys(groups).它不是 React 状态,所以它不会重新渲染?您能否尝试创建一个 groupKey 状态,然后在组更新时使用 Effect 更新状态。const [groupKey,setGroupKey] = useState([]);useEffect(() => {&nbsp; setGroupKey(Object.keys(groups));},[groups]);在组件中,使用{groupKey.map((item, index) => (&nbsp; <TableRow key={index} selected="false">&nbsp; &nbsp; <TableCell>{item.user_id}</TableCell>&nbsp; &nbsp; <TableCell>{item.task_name}</TableCell>&nbsp; </TableRow>))}你明白了。

一只甜甜圈

像下面这样的东西应该有所帮助:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript