How to make react router work with static assets, html5 mode, history API and nested routes?

前端 未结 4 579
北荒
北荒 2020-12-28 14:44

I thought I was starting to understand React Router, but I hit a new wall when adding a library that loads css for its components. Everything works fine when a navigate from

4条回答
  •  臣服心动
    2020-12-28 15:19

    I hava a same problem.

    1. Add a tag to the head of a page containing react-router's HTML using the default browserHistory

    2. Load the page - history.js will output 2 errors, Warning: Automatically setting basename using is deprecated and will be removed in the next major release. The semantics of are subtly different from basename. Please pass the basename explicitly in the options to createHistory

    3. Change the react-router history to use history where history is const history = useRouterHistory(createHistory)({ basename: 'http://whatever' }), which should fix the problem (as basename is now explicitly passed)

    4. Reload the page

    https://github.com/reactjs/react-router/issues/3387

    5/9 update

    In my case.

    index.html

    
      
      
    
    
      

    app.js

    import { Router , useRouterHistory } from 'react-router'
    import { createHistory } from 'history'
    
    const browserHistory = useRouterHistory(createHistory)({ basename: '/' })
    
    render(
      ,
      document.getElementById("app")
    );
    

    And then reload the warning to disappears. hope this is useful.

提交回复
热议问题