score:3
Accepted answer
The issue here is that IDs are treated as strings in the DOM, so here: index + 1 !== selectedButton
(Where selectedButton
is set from e.currentTarget.id
)
You are comparing a number to a string, and they will never be equal, so !==
will always return true
E.g.
function example(e) {
console.log(e.currentTarget.id)
console.log(1 === e.currentTarget.id)
}
<input id="1" onClick="example(event)" placeholder="Click me" />
You will need to either:
- Convert the
index + 1
to a string before comparing the values - Parse the ID as a number before the comparison
- Parse the ID as a number before storing it to
selectedButton
I would recommend replacing:
const clcikedDivId = e.currentTarget.id
with
const clcikedDivId = parseInt(e.currentTarget.id)
Source: stackoverflow.com
Related Query
- React state update produces wrong result
- React useEffect warning: Can't perform a React state update on an unmounted component causes switch statement to render wrong component
- Solution for getting result update state in hooks ( inside async / useState ) in react js
- How to update nested state properties in React
- Can't perform a React state update on an unmounted component
- React useEffect causing: Can't perform a React state update on an unmounted component
- React input defaultValue doesn't update with state
- Does React batch state update functions when using hooks?
- React-hooks. Can't perform a React state update on an unmounted component
- Update component state from outside React (on server response)
- onChange event for React child component to update state
- How to update array state in react native?
- How to update react state without re-rendering component?
- React hooks: How do I update state on a nested object with useState()?
- "Cannot update during an existing state transition" error in React
- How to update object in React state
- UI not re-rendering on state update using React Hooks and form submission
- how to update multiple state at once using react hook react.js
- Can not update state inside setInterval in react hook
- How to update the state of a sibling component from another sibling or imported component in REACT
- Warning: Can't perform a React state update on an unmounted component. In a functional component
- How to update the router state on history.replace in react router?
- What's the best alternative to update nested React state property with setState()?
- React - State Update value only of Particular key
- React Apollo: Dynamically update GraphQL query from Component state
- Update React Hooks State During Render Using useMemo
- React hooks function component prevent re-render on state update
- React does not refresh after an update on the state (an array of objects)
- React not re-rendering after array state update
- Form created with React doesn't update the backing state object
More Query from same tag
- convert reducer from react redux to hooks
- How to focus viewport to the bottom instead of the center
- How to work around jsx-a11y/no-static-element-interactions restriction?
- Styling(tailwind/SCSS stylesheet) not getting applied to dynamically inserted HTML in React
- Can't push the data to array in Firestore + React Hooks
- ReactJS- remove HTTP header before redirect
- publicRuntimeConfig in next.config.js is always undefined in prod/staging
- How to transform: translatex(0%) when click event using react
- Removing an specific item from an array in React
- How to store independent variables into an array as an object
- Push the value returned from a promise into an array
- how can i close modal after form submitting gives an error?
- Property 'propTypes' does not exist on type 'typeof FactoryMethod
- Why does useReducer first parameter get set to Value of dispatch?
- Rendering search results in react
- ReactJS one time initialisation
- How to catch custom raised exceptions from oracle to java
- How do I grab this API data using ReactJS
- create-react-app installs, but fails to run with "start"
- React Material UI Grid Item doesn`t render after data update
- SwiperSlide onTouchStart/onClick => trigger slideNext()
- Do embedded react hooks have a performance degradation compared to single use hooks?
- React handling click outside for multiple elements
- React/Redux changing values in module (not in component)
- How to implement a tile source to mapbox-gl
- Pass value from parent to child component with React
- got an ''Unexpected strict mode reserved word" when try to use await
- Action cable : Call custom method in channel
- Property is undefined inside a component - React Redux
- Console.log not working on Reactjs app