React Router 4 and props.history.push

后端 未结 2 721

There\'s something driving me crazy in React, and I need your help. Basically, when the user clicks \"My Profile\", I want to redirect to the user\'s profile. To do that, I use

相关标签:
2条回答
  • 2021-01-23 05:12

    You need to integrate the react-router-redux.

    Its look like the updated state is not reflecting in the container as redux do shallow comparison to check whether the component need to be update or not.

    import {Router} from 'react-router-dom';
    import { ConnectedRouter, routerReducer, routerMiddleware } from 'react-router-redux';
    import createHistory from 'history/createBrowserHistory';
    
    const history = createHistory()
    const middleware = routerMiddleware(history)
    
    
    const rootReducer = combineReducers({
        router: routerReducer,
      //other reducers
    });
    
    const store = createStore(rootReducer,applyMiddleware(middleware));
    
    <Provider store={store}>
    <ConnectedRouter>
     //routes
    </ConnectedRouter>
    </Provider>
    

    Also,at reducers,add this snippet.

    let updatedStore = JSON.parse(JSON.stringify(state));
    
    0 讨论(0)
  • 2021-01-23 05:17

    I think has to do with you using <BrowserRouter>. It creates its own history instance, and listens for changes on that. So a different instance will change the url but not update the <BrowserRouter>. Instead you can use ConnectedRouter and pass a history instance as

    import createHistory from 'history/createBrowserHistory';
    ...
    const history = createHistory();
    ...
    <ConnectedRouter history={history}>
    ...
    </ConnectedRouter>
    
    0 讨论(0)
提交回复
热议问题