score:2
Accepted answer
Why is it re-rendering like crazy?
I think that is because you are changing the state in the render function so that trigger a side-effect.
I tried using useEffect to fire the event once but that didn't make sense as it fired on componentDidMount which wasn't triggered by the first click.
useEffect its also componentDidUpdate event
And you can handle the event in an auxiliary function "toggle"
function App() {
const [isOpen, setIsOpen] = useState(false);
const [counter, setCounter] = useState(0);
function toggle() {
if (!isOpen) {
setCounter(counter + 1);
}
setIsOpen(!isOpen);
}
useEffect(() => {
if (counter === 1) {
doSomething();
}
}, [counter]);
return (
<div className="App">
<button onClick={() => toggle()}>Click here</button>
</div>
);
}
The "[counter]" says that whenever the counter variable change, the callback function is going to be executed.
useEffect(() => {
if (counter === 1) {
doSomething();
}
}, [counter]);
Source: stackoverflow.com
Related Query
- React - fire callback based on state and first onClick event only
- React onClick event to only fire for the clicked div and not the other once
- React - how to capture only parent's onClick event and not children
- React component only changes state on second onClick event
- React keypress event taking only initial state values and not updated values
- How can I use useEffect in React to run some code only when the component first mounts, and some other code whenever an event occurs (repeatedly)?
- Update parent state based on props and onClick from child in React
- React App only re-renders on first state change and changes are not visible for second change even though the change is successful
- React hooks, initialize and change state with onClick event using tables
- React Material Table action OnClick event for edit and delete does not fire action
- React state, Onclick will not fire first time.. only second
- Why does React rerender when the state is set to the same value the first time via an onClick event on DOM, but not react-native?
- Get object data and target element from onClick event in react js
- Change state when properties change and first mount on React - Missing function?
- Correct way to throttle HTTP calls based on state in redux and react
- onMouseDown event in react does not trigger state changes but onClick does
- Any reason for a react state hook set callback to fire twice?
- React: How to add onChange functionality inside of Function component using React Hooks? Need onClick event from a checkbox to influence input state
- Creating a Custom Button in React Typescript and add onClick Event
- Access props and state of current component in React JS within an event handler
- React Native: Redux - How to properly update TextInput via event and have redux state update?
- Advantages and Understanding of using Currying Function pattern in onClick Event handler in React
- OnClick event not firing on first click in React
- How do I fire React useEffect hook only once after state change?
- How to store react component in state and pass a callback function
- What is the difference between passing a function name in onclick react and calling it through callback
- change state for onClick event in other component react
- Set state and run function on a single onClick event
- How to run React useEffect only once and property update state with setState
- React Hooks - having trouble with splice updating state during onClick event
More Query from same tag
- SharePoint Framework with React Component loading images
- Google map react
- Webpack hot reload using webpack dev server and rails server
- React Router - History fires first rather waiting
- Have 2 issues with React, Changing the color of single button with click and play different sounds on each click
- How to change react-select option automatically after 5 seconds
- Filter a collection for everything except the values in an array (exclude the values in the array)
- Webpack fails to transpile CRA app, trying to import firebase from URL
- how to display a button when video played 1 minute when using react-player?
- Remove event listener
- React - TypeError: _this.setState is not a function
- react update array of object with checked field in state
- How to pass multiple key and their HTML tag attributes in React
- How to use the new IdleTimer from 'react-idle-timer' in a functional component
- React Router Displaying only Blank Page - No data Showing
- React router <Redirect> is not redirecting on state
- React js What am I supposed to do with an object that used before componentDidMount() is invoked?
- 'visualize' is not defined no-undef in react
- React: How do you lazyload image from API response?
- Optimal way of transforming flat map to nested data structure using React.js?
- Testing results for async calls made in useEffect hook
- React Fabric UI remove the Pivot Item
- How to replay a CSS3 animation in Reactjs
- Not able to setValue in useState in React Material-UI
- How to add style to a component with styled component react?
- React - TypeError: Cannot read property 'image' of undefined
- force update jQuery fullcalendar, after prop change in React.js
- Android keyboard disappears on resize
- bulma flex justify content not working in reactjs
- too many re-renders. react limits the number of renders to prevent an infinite loop. UseEffect