react-router go back a page how do you configure history?

前端 未结 21 1253
说谎
说谎 2020-11-30 18:30

Can anyone please tell me how I can go back to the previous page rather than a specific route?

When using this code:

var BackButton = React.createCla         


        
21条回答
  •  隐瞒了意图╮
    2020-11-30 18:39

    React Router v6

    useNavigate Hook is the recommended way to go back now:

    import { useNavigate } from 'react-router-dom';
    
    function App() {
      const navigate = useNavigate();
    
      return (
        <>
          
          
        
      );
    }
    

    Codesandbox sample

    Go back/forward multiple history stack entries:
    
    
    
    Go to specific route:
    navigate("users") // go to users route, like history.push
    navigate("users", { replace: true }) // go to users route, like history.replace
    navigate("users", { state }) // go to users route, pass some state in
    

    useNavigate replaces useHistory to support upcoming React Suspense/Concurrent mode better.

提交回复
热议问题