Home > Enterprise >  How do I store the id of a button a user clicked in local storage in a react component?
How do I store the id of a button a user clicked in local storage in a react component?

Time:12-19

I'm new to Javascript and I'm trying to get the id of the button a user clicked and store the value in localStorage to use it elsewhere in my React component but it's not working as expected. Please what am I doing wrong? Below is my function

const handleButtonClilck = (button, event) => {
    setButton(button)
    localStorage.setItem("btncourseId", event.target.id)
}
const coursebtn = localStorage.getItem("btncourseId")

Here is the button:

<button id={course.id} onClick={()=> handleButtonClick('chapter')} className="btn btn-primary btn-sm">Add Chapter</button>

The variable coursebtn is returning null but {course.id} is returning expected value

CodePudding user response:

<button id={course.id} onClick={(e)=> handleButtonClick('chapter',e)} 
className="btn btn-primary btn-sm">Add Chapter</button>
  • Related