在React v6中,我应该使用什么作为替代?

huangapple go评论55阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月7日 04:24:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定