When using history.push() my component does not render, you need to re-load the page to render

Asked

Viewed 1,034 times

1

I’m using React-router-dom to work with routes in an React application.

I have a file called Approuter.js that has my routes:

const AppRouter = props => {

    const loading = useSelector(state => state.loadingStates)
    return (
        <Router>
            <Switch>
                <div>
                    <Route exact path="/">
                        <Redirect
                            to={{
                                pathname: "/login",
                            }}
                        />
                    </Route>
                    <Route path="/home" component={Home} />
                    <Route path="/registrar" component={LoginWrapper} />
                    <Route path="/login" component={LoginWrapper} />
                </div>
            </Switch>
        </Router>
    );
}

export default AppRouter

My index.js file renders this route file:

ReactDOM.render(
    <Provider store={store}>
            <AppRouter/>
    </Provider>, document.getElementById('root'));

So when I go to the localhost:3333 url I get redirected to the login screen. When I’m on the login screen and log in, I call a history.push('/home'):

function* makeLogin(action) {
    yield put(allActions.loadingActions.startLoading());
    try {    
        const { data } = yield call(api.post, '/login', action.payload);
        yield put(allActions.loginActions.setUser(data))
        yield put(allActions.loadingActions.endLoading());
        localStorage.setItem('currentUser', JSON.stringify(data))
        history.push('/home')
    } catch (e) {
        console.log(e)
    }
}

The url in my browser is updated to localhost:3333/home but the login screen is still shown, it is necessary to perform a page re-load for the component to render.

This is my history.js file that is imported by the saga.js file that performs the login request:

import { createBrowserHistory } from 'history';

const history = createBrowserHistory();

export default history;
  • 1

    I’m not sure how the react-router-dom, but it shouldn’t be window.location.href = '/home' ?

  • 1

    is a hard problem to reproduce. @Marceloboni no ...

  • 1

    I managed to solve by adding history to my appRouter, soon put as I could solve

  • Same problem with me, I downgrade as said in the answer below, it worked with me.

  • Same problem with me, I downgrade as said in the answer below, it worked with me. -

  • It really is the version. Something similar happened to me, I was using version 5.0 of history and while doing a downgrade was just reloading the page that worked. After much research and attempts I managed to solve. Thank you for posting the solution there!! Thanks

Show 1 more comment

1 answer

3


I was suffering with the same error and in my case the problem is occurring because of the version of history, so I downgrade the version of history and solved the problem.

yarn remove history
yarn add history@^4.10.1
  • Same thing happened to me here, I went to search to see if it was just me, now I need to understand why version 5 has this problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.