0

I'm just checking if I have language in redux store or not. If I have, I'll save that in my localStorage, if not I want to navigate to a component call MultiLanguage.
I used below approach using history but it's showing mw an error. What is it showing push of undefined and what could be solution for this?

const history = useHistory();
  const language = store.getState().languages.selectedLangauge.language;
  console.log("language", language);
  if (language) {
    localStorage.setItem("language", language);
  } else {
    history.push("/multilanguage");
  }
  return (
    <div className="font-metropolis_regular">
      <Router>
        <Route path="/" exact component={Selection} />
        <Route path="/login" component={Login} />
        <Route path="/home" component={Homepage} />
        <Route path="/account" component={Account} />
        <Route path="/multilanguage" component={MultiLanguage} />
      </Router>
    </div>
  );

Chandler Bing
  • 410
  • 5
  • 25

1 Answers1

-1

The reason is simple, as the error says, your useHistory hook returns undefined. This happens because this hook can only be called inside a component wrapped by Router so the decision might be moving it one level up. Take a look at this for code examples https://flaviocopes.com/react-router-uselocation-usehistory-undefined/

Kiril
  • 131
  • 5