我正在尝试通过材料反应表中的 api 获取数据,但它向我显示了一个错误,如下所示 Invalid hook call。钩子只能在函数组件的主体内部调用。这可能是由于以下原因之一: 1. 你可能有不匹配的 React 版本和渲染器(例如 React DOM) 2. 你可能违反了 Hooks 规则 3. 你可能有多个 React 副本同一个应用
代码如下:
import React, { Component } from 'react';
import MaterialTable from "material-table";
import withStyles from "@material-ui/core/styles/withStyles";
import { makeStyles } from '@material-ui/core/styles';
import ReactTable from 'react-table'
import api from '../../api'
import { Button } from 'react-floating-action-button'
import "@fortawesome/fontawesome-free/css/all.min.css";
import "bootstrap-css-only/css/bootstrap.min.css";
import "mdbreact/dist/css/mdb.css";
import {
MDBBtn,
} from "mdbreact";
import "react-table/react-table.css";
import styled from 'styled-components'
import 'react-table/react-table.css'
import { whiteColor } from 'assets/jss/material-dashboard-react';
import { createSourceConfiguration } from '../../components/UserForm/SourceDatasetSelection';
import { grayColor } from 'assets/jss/material-dashboard-react';
const styles = {
cardCategoryWhite: {
color: "rgba(255,255,255,.62)",
margin: "0",
fontSize: "14px",
marginTop: "0",
marginBottom: "0"
},
cardTitleWhite: {
color: "#FFFFFF",
marginTop: "0px",
minHeight: "auto",
fontWeight: "300",
fontFamily: "'Roboto', 'Helvetica', 'Arial', sans-serif",
marginBottom: "3px",
textDecoration: "none"
}
};
class MoviesList extends Component {
constructor(props) {
super(props)
this.state = {
movies: [],
columns: [],
isLoading: true,
}
}
componentDidMount = async () => {
this.setState({ isLoading: true })
await api.getAllMovies().then(movies => {
this.setState({
movies: movies.data.data,
isLoading: false,
})
})
}
render() {
const { movies, isLoading } = this.state
console.log('TCL: Configuration List -> render -> movies', movies)
// const { movies, isLoading } = this.state
const [state, setState] = React.useState({
columns: [
{
title: "Dataset Name",
// field: "dataset",
accessor: 'configname',
// type: "String",
headerStyle: { fontSize: 20,backgroundColor:grayColor},
cellStyle: {fontSize: 18},
}
]
});
return (
<div style={{ maxWidth: "100%" }}>
<MaterialTable
title=''
columns={state.columns}
data={state.data}
editable={{
onRowAdd: newData =>
new Promise(resolve => {
setTimeout(() => {
resolve();
const data = [...state.data];
data.push(newData);
setState({ ...state, data });
}, 600);
}),
onRowUpdate: (newData, oldData) =>
new Promise(resolve => {
setTimeout(() => {
resolve();
const data = [...state.data];
data[data.indexOf(oldData)] = newData;
setState({ ...state, data });
}, 600);
}),
onRowDelete: oldData =>
new Promise(resolve => {
setTimeout(() => {
resolve();
const data = [...state.data];
data.splice(data.indexOf(oldData), 1);
setState({ ...state, data });
}, 600);
})
}}
/>
</div>
);
}
}
export default MoviesList
//export default withStyles(styles)(Dataset);