React history.push() not rendering new component

前端 未结 5 1169
梦谈多话
梦谈多话 2021-01-02 09:31

I have a React.js project with a simple sign in function. After the user is authorized, I call history.push method which changes the link in the address bar but does not ren

5条回答
  •  青春惊慌失措
    2021-01-02 09:35

    Try to use custom history and Router instead of BrowserRouter. After installing history:

    yarn add history
    

    Create a custom browser history:

    import { createBrowserHistory } from "history";
    
    export default createBrowserHistory();
    

    Use Router instead of BrowserRouter in your setup:

    import history from "your_history_file";
    
    ReactDOM.render(
      
        
          
    , document.getElementById('root') );

    or if you don't want to use a custom history file and import from there you can crate it directly in your index.js:

    import { createBrowserHistory } from "history";
    
    const history = createBrowserHistory();
    
    ReactDOM.render(
      
        
          
    , document.getElementById('root') );

提交回复
热议问题