React onClick function fires on page refresh [Fixed] 2022

    By: Manu
    2 years ago

    If in react js you face issue where a function passed in anchor tag, is firing on page refresh or page render. then you can fix it like this. Make your code changes as given here


    Change your code from this

     <a href="#/" onClick={handleDelete(listItem.id)}>
    

    To this

    <a href="#/" onClick={() => {handleDelete(listItem.id)}>
    

    Now you are passing function instead of calling it. this will fix the issue.


    See live execution in the video guide. Video will start from the point we show the usage of onclick in react project.