React Js Alert on click button

React Js Alert on click button: React is a popular JavaScript library for building user interfaces. To create an alert on click button in React, you can attach an onClick event handler to the button element that triggers a function to display the alert message.




Thanks for your feedback!
Your contributions will help us to improve service.
How to display alert on click of a button in React js?
In this React.js code snippet, an alert is displayed when a button is clicked. The showAlert
function is defined, which uses the window.alert
method to display a message ("Button clicked!").
The onClick
attribute is added to the button element, and it is set to the showAlert
function. This means that when the button is clicked, the showAlert
function is executed, triggering the alert
React Js Alert on click button Example
xxxxxxxxxx
<script type="text/babel" data-presets="env,react">
const { useState, useEffect, useRef } = React;
function App() {
const showAlert = () => {
window.alert("Button clicked!");
};
return (
<div>
<button onClick={showAlert}>Click me</button>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of React Js Alert on click button