score:2
Accepted answer
let x = 3
let y = x++
//output: x=4 y=3
let x = 3
let y = ++x
//output: x=4 y=4
it's why you need to double click because you have next logic:
console.log(counter) //0
//triggers click
console.log(counter) //0, usestate re-assigned to 0
//triggers click
console.log(counter) //1, usestate re-assigned to 1
score:2
difference is that counter + 1
is only an operation and counter++
is an operation with an assignment. in react you shouldn't assign your state directly but use the setter method. so in your case it is better to use counter + 1
Source: stackoverflow.com
Related Query
- why variable++ is not working the same way as variable+1
- Why is setInterval not being cleared in REACT the same way as it is in vanilla JS
- React Beginner Having Problems Adapting Material-UI Example Into React Project. State Not Working the Same Way Hooks was
- Why parent state is not updating when I change a value on a child but it is working the other way around
- Why can I not use a variable as parameter in the require() function of node.js (browserify)?
- Why is this variable from the Redux store not in the `this.props` on React page?
- Anchor link to div on the same page not working in Safari
- Why is it not possible to have the same name of two StaticQuery elements on two different layouts for Gatsby?
- Why is the @forward naming prefix not working with variables using Sass?
- Why is useEffect not working here? Do I need to change the condition?
- Why is my hook variable in React not holding the state I set it to?
- Why this.setState() is not working in the constructor?
- Why is my Redirect not working after the verifications?
- Why doesn't enzymes find() with component props work the same way when using full rendering or shallow rendering?
- What is the proper way of communicating between components in react? Why not using refs everywhere?
- React-select multi not working the way it's defined in the documentation
- React: this.setState() working in some functions, not in others within the same component
- ReactJS. - Why does this work in ReactJS and not the other way
- Why doesn't HTML allow me to add event handlers in the same way as JSX?
- Why is the JavaScript spread notation not working here
- Why would flex-direction not work work the way I have it set up? REACT js
- Why is the preventDefault() method not working for me in my react application?
- Why does typescript complain when I import a component, but not when the component is defined in the same file?
- Why does require not behave the same as import when same structure is given
- Why is the value of my variable not showing in the render for my react component?
- Why are these table rows not re-rendering in React when the stateful variable they are based on is updated?
- Why does useCallback with an empty dependency array not return the same function?
- Why is my navbar from React-Bootstrap rendering vertically and not the way displayed in the documentation?
- React Context is not working as expected: Unable to change the value of shared variable
- Redux toolkit filter() not working when wanting to display new array at the same time
More Query from same tag
- enable scrollbar for the submenu when the windowHeight is smaller than it
- How map with function and text ReactJs
- Data passing from parent component to the child component with reactjs and type script with the React.cloneElement
- React - Redux: Array in the store is not being updated correctly
- React Component Inheritance to use parent method and child method
- React + Flux data errors produce snackbar notification?
- Is there any way to configure baseURL in relation to the current domain?
- React passing two arguments function to onContextMenu event
- useState() causes unexpected behaviour with audio object
- react-simple-typewriter: Loop does not work
- Error: Objects are not valid as a React child (found: object with keys {})
- Trying to retrieve an mp3 file stored in AWS S3 and load it into my React client as a Blob...it's not working
- Can somebody explain what makes this code?
- Where does React's `scryRenderedDOMComponentsWithClass` method name come from?
- this.setState() is changing state to wrong value
- Error in setting headers in axios and react
- Deploying Client and Server to the same VM
- Reactjs Component lifecycle - Fetching data from user input
- Stripe - Problem Creating PaymentMethods directly on the connected account in React. Code in docs needs to be changed for react?
- React and Redux: Redux Custom Middleware Not Executing
- Problem with conditional statements in React Hook
- How do I get the url of my post in react and node application as a string?
- Gatsby: unique "key" prop
- How to catch uncaught errors in React ErrorBoundary
- How to fix async await in MERN using .map() and mongoDB call
- How to make Mongoose query case insensitive?
- How can I test a class which contains imported async methods in it?
- Storybook webpack repeatedly rebuilding (hot reloading over and over?)
- Jest, Enzyme, React, Next.js, Typescript .default is not a constructor
- Fetched Object from API doesn't appear in mapping