Is it possible to match the # part of a route in React Router 4

前端 未结 3 1342
盖世英雄少女心
盖世英雄少女心 2021-02-20 04:04

In my app, I\'d like to match both the path and the hash to different components. For example:

/pageA#modalB

Would show PageA as the main page

3条回答
  •  日久生厌
    2021-02-20 04:24

    @azium answer works fine as long as you don't need to use render or children prop in HashRoute. In which case this solution will work better :

    import React from 'react';
    import { Route } from 'react-router-dom';
    
    const HashRoute = ({ hash, ...routeProps }) => (
       (
          (location.hash === hash) && 
        )}
      />
    );
    
    export default HashRoute;
    

    Use it like that :

    
    

    Or combine it with route matching :

    
    

提交回复
热议问题