0

I have been trying to redirect to ProductId Component when there is a click on div with a className= "card" <div className="card" onClick={()=>CallComponent(_id)}>

The function CallComponent is:

CallComponent = (_id) => {
   console.log("Clicked");
   <Link to="/productnew">
   </Link>;
   <Route path="/productnew" component={ProductId}></Route>
};

When there is a click on card "Clicked" is seen in the console but the component is not routed

Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30
DanDemon
  • 53
  • 5

1 Answers1

0

To programmatically navigate to /productnew route you could use Redirect in combination with a state variable. Something like:

import { Redirect } from "react-router-dom";

...

state = { redirect: null };

CallComponent = (_id) => {
   console.log("Clicked");
   this.setState({ redirect: "/productnew" });
};


render() {
  if (this.state.redirect) {
    return <Redirect to={this.state.redirect} />
  }
  return(
  // Your Code goes here
  )
}

This is an intersting article if you want read more about this topic.

Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30