【发布时间】:2021-11-09 01:16:05
【问题描述】:
在 Material UI 表中,我使用 Action 来查看数据的详细信息, 因此,每当单击此操作按钮时,我都想移动页面或呈现新组件。 请帮助我有效地做到这一点。提前致谢。
[我尝试使用 history.push() 执行此操作,但它给出错误:意外使用历史记录]
import AddIcon from "@material-ui/icons/Add"; import { get, getDatabase, orderByKey, query, ref } from "firebase/database"; import MaterialTable from "material-table"; import { useEffect, useReducer, useState } from "react"; import { Redirect } from "react-router-dom"; import GridLoader from "react-spinners/GridLoader"; import useVideos from "../hooks/useVideos"; import { useHistory } from "react-router"; const initialState = [{}]; const reducer = (state, action) => { switch (action.type) { case "videos": const data = []; let cnt = 1; action.value.forEach((video, index) => { data.push({ sl: cnt, noq: video.noq, id: index, youtubeID: video.youtubeID, title: video.title, }); cnt = cnt + 1; }); return data; default: return state; } }; export default function ManageVideos() { const { addVideo, updateVideo, deleteVideo } = useVideos(""); const [loading, setLoading] = useState(true); const [error, setError] = useState(false); const [cnt, setCnt] = useState(0); const [videoList, dispatch] = useReducer(reducer, initialState); const history = useHistory(); useEffect(() => { async function fetchVideos() { const db = getDatabase(); const videosRef = ref(db, "videos"); const videoQuery = query(videosRef, orderByKey()); try { setError(false); setLoading(true); const snapShot = await get(videoQuery); setLoading(false); if (snapShot.exists()) { dispatch({ type: "videos", value: snapShot.val(), }); } } catch (err) { console.log(err); setLoading(false); setError(true); } } fetchVideos(); }, [cnt]); const columns = [ { title: "Serial", field: "sl", editable: "never" }, { title: "Title", field: "title", validate: (rowData) => rowData.title !== "", }, { title: "Number of Qusetion", field: "noq", editable: "never", }, { title: "Youtube Video Id", field: "youtubeID", validate: (rowData) => rowData.youtubeID !== "", }, ]; return ( <div> <MaterialTable title="Videos Table" data={videoList} columns={columns} options={{ export: true, grouping: true, filtering: true, }} editable={{ onRowAdd: (newData) => new Promise((resolve, reject) => { setTimeout(() => { addVideo(newData); setCnt((prevCnt) => prevCnt + 1); resolve(); }, 1000); }), }} actions={[ { icon: "add_box", color: "info", tooltip: "View Video Details", onClick: () => { history.push("/addQuiz"); // want to write something to move a new page or render a component. }, }, ]} /> </div> ); }
【问题讨论】:
-
解决了!不幸的是,我导入了 useHistory 表单“react-router”,这就是我收到此错误的原因。
标签: reactjs material-ui action render material-table