How do you handle events in React?

React state and events · Freshers

Model answer

Pass a function to an event prop in camelCase: <button onClick={handleClick}>. React calls it with a synthetic event object that behaves like the browser event. Write onClick={handleClick}, not onClick={handleClick()}, which would run the function during render.

For arguments, use an arrow function: onClick={() => remove(item.id)}. Use event.preventDefault() on a form submit to stop the page reload.

Learn this topic

React useState Explained: Make Your Page React to Change in the course React.js Full Course: Beginner to Advanced.

Sign in to add your own answer.

More questions