Search code examples
reactjsionic-frameworkionic-react

How to use href with onClick() in IonButton?


The below onClick() function works as intended without href. But when I add in the href, only the href seems to run, not the onClick() function. How do I both run the onClick() function and also send the user to a new page?

                <IonButton
                    color="primary"
                    onClick={() => {saveData()}}
                    href="/create/"
                >Save</IonButton>

Solution

  • you do not really want to use href use history

    const history = useHistory();
    
    <IonButton color="primary"
      onClick={(event) => { 
          event.preventDefault();
          saveData(); 
          history.push("/create")}}
    > Save
    </IonButton>