Good morning,
I just posted my question here because I can’t find any answer anywhere… I’m simply trying to create a redirection with the "redirect" function of "react-router-dom" but it is completely ignored and I don’t understand why …
Imported function:
{ redirect } from "react-router-dom";
My function:
switchView = () => {
this.setState((state, props) => {
const url = state.signup === true ? '/signin' : '/signup';
redirect(url);
return {
signup: !state.signup
};
});
}
My implementation:
<Button variant="contained" color="secondary" size="small" fullWidth={true} onClick={this.switchView}>
{this.state.signup ? 'Connexion' : 'Inscription'}
</Button>
I redid the entire road management part…
Unfortunately I can’t go through the useHistory hook because I’m going through a Component class extension. I don’t know if you have a solution for this?
Do you have an idea ?
Thanking you.
2
Answers
use
withRoute
hoc for class components and access history object through propsIf you are working with a class component and need to perform redirection using React Router, you can access the history object by wrapping your component with the withRouter higher-order component (HOC) from "react-router-dom."
This HOC injects the history prop into your component, allowing you to access the history object and perform the redirection.