score:2
Accepted answer
onclick
accepts to be a function and would be written as
<button onclick={() => {console.log("hi")}}>hello</button>
or
handleclick() {
console.log('hi');
}
<button onclick={this.handleclick}>hello</button>
instead of
<button onclick={console.log("hi")}>hello</button>
if you write onclick={console.log("hi")}
, on each render
the value for onclick
is evaluated and hence hi
is seen on console
score:1
you should provide a function in the event handler, not call the function. see:
import react from "react";
import { render } from "react-dom";
const app = () => (
<div>
<div>
<button onclick={() => {console.log("hi")}}>hello</button>
</div>
</div>
);
render(<app />, document.getelementbyid("root"));
Source: stackoverflow.com
Related Query
- How to fire a click event in a ul, li or div in reactjs
- How to select text to copy without triggering click event in reactjs
- How to get key attribute of parent div when button is click in ReactJS
- How to set state and props on a single click event in reactjs
- How to handle onKeyDown event on div in ReactJS
- How do you get a button in DIV not call the <div> click event
- how to handle array of buttons handle click event in ReactJs
- How to remove object from Array in ReactJS after click event
- How to disable click event while fetching the data in reactjs witout jquery?
- How to add the click event on the icons in reactjs
- How to fire click event for choosing each plot in google map drawing manager function?
- How to render DIV element with a button click on ReactJs
- How do I get the id of div wrapper in React when childnodes are the click event target?
- How do I trigger a click event based on a specific className in ReactJS
- how to add pagination and fire row click event in table in react js
- ReactJS - Make Click Event fire before onBlur
- How can I disable the click event after the div is clicked in react?
- How to fire an event handler for a child component in reactjs
- How to properly display loading event on button click using reactjs
- How to use array method to change different color on div in. In single button reactjs click
- how to fire mouse event click on tab
- ReactJs how to scroll to bottom of div on click or submit
- how to handle the click event to display text in the <span> tag on reactjs
- How to pending button show approved on click event by reactjs
- How to manually trigger click event in ReactJS?
- How to get input text value on click in ReactJS
- How to get pasted value from Reactjs onPaste event
- How to detect Click + [Shift, Ctrl, Alt] in reactjs click event?
- How to trigger INPUT FILE event REACTJS by another DOM
- How to simulate mouse over event on a div using enzyme for testing a react application?
More Query from same tag
- Using useState to store state for an array of objects
- RTL on MuiTextField goes wrong using Special Charcters
- Having and Issue checking to see if state value is true or false
- How to get a React Redux state value from the reducer?
- how do I make a post and get request with ReactJS, Axios and Mailchimp?
- App should go to a different route when fetched dropdown values are clicked
- How to get a Keycloak mock to work with Jest
- Function works but won't return HTML?
- React PropTypes File type
- How to fix this Syntax Error in my React App?
- Understanding nesting vs parent in ReactJS
- How to reset input field from useRef in React?
- Fetch data from server in reactjs
- styled-component template literal returns undefined to react component
- Returning a new set of array using map method
- How to fetch data from api using map in reactjs?
- Why i can't use map on redux useSelector?
- React scroll to bottom of Element without scrolling iFrame parent
- React-Select: How to clear a selection value when another selection value change
- Internal server error 500 react post to firebase
- How can I solve the following error "Cannot read property 'name' of undefined"?
- Jest how to loop through similar test functions
- React Router calls component constructor when switching routes
- onChange in select not working in react js
- Typescript - pass new key in object as generic
- Access the `access_token` property in localstorage
- Switching input field form validation in react
- react - componentWillReceiveProps not running
- ReactJS Load Images on Click
- Creating rows in a table when data is missing for some columns