英文:
What should i use for alternative In React v6
问题
let history = useHistory();
let location = useLocation();
let { from } = location.state || { from: { pathname: '/' } };
history.replace(from);
英文:
What need to use for log in page auth in react-router V6. I also try useNavigate()
to replace of useHistory
but it was not work.
let history = usehistory();
let location = useLocation();
let { from } = location.state || { from: { pathname: '/' } };
history.replace(from);
答案1
得分: 1
useNavigate
钩子是 RRDv6 对 RRDv5 useHistory
钩子的替代品。navigate(to, { replace: true })
替代了 history.replace(to)
用于重定向。
查看 useNavigate。
const navigate = useNavigate();
const { state } = useLocation();
...
const { from } = state || { from: { pathname: ''/' } };
navigate(from, { replace: true });
有关从 RRD v5 到 v6 的其他重大更改的详细信息,请参阅 Upgrading from v5 迁移指南。
英文:
The useNavigate
hook is the RRDv6 replacement to the RRDv5 useHistory
hook. navigate(to, { replace: true })
replaces history.replace(to)
for redirects.
See useNavigate.
const navigate = useNavigate();
const { state } = useLocation();
...
const { from } = state || { from: { pathname: '/' } };
navigate(from, { replace: true });
For more details on other breaking changes from RRD v5 to v6 see the Upgrading from v5 migration guide.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论