23
loading...
This website collects cookies to deliver better user experience
history
objects and you have access to several functions to navigate your page. It's all about navigation. This is how you can use useHistory
.import { useHistory } from 'react-router-dom';
const Portfolio = (props) => {
const history = useHistory();
console.log(history);
return (
<div>
Portfolio
</div>
);
};
export default Portfolio;
history.goBack()
, history.goForward()
, history.go()
)history.push()
history.replace()
//using pathname
history.push("/blog");
//https://localhost:3000/blogs
//using pathname and state
history.push("/blog", { fromPopup: true });
//https://localhost:3000/blogs
//using location
history.push({
pathname: "/blogs",
search: "?id=5",
hash: "#react",
state: { fromPopup: true }
});
// https://localhost:3000/blogs?id=5#react
.replace
, it will not go back to the previous one..push
from history in the example localhost:3000/blogs?id=5#react
.useLocation()
is getting information from the current route, and it will return these attributes.{
key: 'ac3df4', // not with HashHistory!
pathname: '/somewhere',
search: '?some=search-string',
hash: '#howdy',
state: {
[userDefined]: true
}
}
const params = useParams();