Detect Route Change with react-router

前端 未结 8 1056
南笙
南笙 2020-11-27 11:18

I have to implement some business logic depending on browsing history.

What I want to do is something like this:

reactRouter.onUrlChange(url => {
         


        
8条回答
  •  日久生厌
    2020-11-27 12:12

    Update for React Router 5.1.

    import React from 'react';
    import { useLocation, Switch } from 'react-router-dom'; 
    
    const App = () => {
      const location = useLocation();
    
      React.useEffect(() => {
        console.log('Location changed');
      }, [location]);
    
      return (
        
          {/* Routes go here */}
        
      );
    };
    

提交回复
热议问题