const history = useHistory();
useHistory()
返回一个对象,可以使用两种方法:push
和 replace
实现编程式导航,push
增加新页,可以使用后退按钮返回到旧的页面,使用 replace
则不能返回。
sample code:
import { useHistory } from "react-router-dom";
import QuoteForm from "./../components/quotes/QuoteForm";
const NewQuote = () => {
const history = useHistory();
const addQuoteHandler = (quoteData) => {
console.log(quoteData);
history.push("/quotes");
};
return <QuoteForm onAddQuote={addQuoteHandler} />;
};
export default NewQuote;