I have this example for a React form with Typescript and Material-UI.
Please take a look in your code I am not able to answer it, that's why I edited your question.
so you can use history. push to redirect after submission.
import React, { useState } from "react";
import TextField from "@material-ui/core/TextField";
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles";
import { Box, Button, Container, Grid, Typography } from "@material-ui/core";
import SaveIcon from "@material-ui/icons/Save";
import { TicketDTO } from "../../service/support/types";
import { postTicket } from "../../service/support";
import { useForm } from "react-hook-form";
import { Redirect } from "react-router-dom";
import { useHistory } from "react-router-dom";
export default function OpenTicket(props: any) {
let history = useHistory();
const {
register,
handleSubmit,
formState: { errors },
} = useForm<TicketDTO>();
const onSubmit = async (data: TicketDTO) => {
postTicket(data)
.then(({ data }) => {
console.log(data.title);
history.replace("/path you want add it here");
})
.catch((err) => {
console.log(err);
});
};
const formHandler = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
console.log(e.target);
};
return (
<Container>
<form onSubmit={handleSubmit(onSubmit)}>
<TextField
id="title"
{...register("title", {
required: true,
maxLength: 40,
})}
/>
<Button
variant="contained"
color="primary"
size="small"
type="submit"
startIcon={<SaveIcon />}
>
Submit Ticket
</Button>
</form>
</Container>
);
}
After I successfully submit the form how I can redirect the user to a new page?
CodePudding user response:
You can do this
const router = useHistory();
const onSubmit = async (data: TicketDTO) => {
postTicket(data)
.then(({ data }) => {
console.log(data.title);
router.push('/path_name_of_the_page')
})
.catch((err) => {
console.log(err);
});
};
note that useHistory() are imported form 'react-router'
import {useHistory} from "react-router";