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

前端 未结 12 1105
滥情空心
滥情空心 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:37

    Hooks sample

    const {history} = useRouter();
      useEffect(() => {
        return () => {
          // && history.location.pathname === "any specific path")
          if (history.action === "POP") {
            history.replace(history.location.pathname, /* the new state */);
          }
        };
      }, [history])
    

    I don't use history.listen because it doesn't affect the state

    const disposeListener = history.listen(navData => {
            if (navData.pathname === "/props") {
                navData.state = /* the new state */;
            }
        });
    

提交回复
热议问题