Intercept/handle browser's back button in React-router?

前端 未结 12 1116
滥情空心
滥情空心 2020-11-27 03:47

I\'m using Material-ui\'s Tabs, which are controlled and I\'m using them for (React-router) Links like this:

    

        
12条回答
  •  广开言路
    2020-11-27 04:36

    Upcoming version 6.0 introduces useBlocker hook - which could be used to intercept all navigation attempts.

    import { Action } from 'history';
    import { useBlocker } from 'react-router';
    
    // when blocker should be active
    const unsavedChanges = true;
    
    useBlocker((transition) => {
        const {
            location, // The new location
            action, // The action that triggered the change
        } = transition;
    
        // intercept back and forward actions:
        if (action === Action.Pop) {
            alert('intercepted!')
        }
    
    }, unsavedChanges);
    

提交回复
热议问题