I am new to React.js and I am not sure how to handle this.
I did a selection with react where the selected option is passed by the link. I am also using the react router.
If no option is selected I want to redirect to the first option when the page loads.
function initTrainer() {
for(let i = 0; i < trainer.length; i++) {
trainer[i].link = "/trainer/" + trainer[i].firstName.toLowerCase() + "-" + trainer[i].lastName.toLowerCase();
trainer[i].key = i;
}
if(window.location.pathname === "/") {
// navigate("/trainer/")
}
}
How can I redirect without a click in a normal function? Is there an easier solution to this problem?
Y I am using React Router V6 but I want to call navigate() conditionally.