score:2
Accepted answer
The splice
method just mutates the existing array instance and React avoids rerendering the array thinking it's the same array (React uses referential equality). You need to create a new array after removing the item.
Any of the following would work
Using spread operator to create a new array.
if (event.target.checked) {
//adding values to array
setSelectedKeys([...selectedKeys, event.target.value]);
} else {
var index = selectedKeys.indexOf(event.target.value);
if (index >= -1) {
//Removing values from array
selectedKeys.splice(index, 1);
}
setSelectedKeys([...selectedKeys]);
}
Filter the array which also outputs a new array
if (event.target.checked) {
//adding values to array
setSelectedKeys([...selectedKeys, event.target.value]);
} else {
var index = selectedKeys.indexOf(event.target.value);
if (index >= -1) {
//Removing values from array and set the new array
setSelectedKeys(
selectedKeys.filter((item, itemIndex) => itemIndex !== index)
);
}
}
Source: stackoverflow.com
Related Query
- State not updating on removing value from array in React JSX funcntional component
- React event target value from jsx element does not get bind to state and json array (Type error cannot read the properties of undefined )
- State array not updating when removing an array item in React
- Input field not updating from redux state but displays value on console.log in react
- React Hook useCallback not updating State value
- React not updating state with setState with Array of object
- updating one value in State array react native
- React state value not updating
- ReactJS component state is not updating when any value in state array is modified
- Method not getting correct useState value despite updating state in React
- Axios not updating state from API in function called on Click, but works on identical function on Component Mount in React
- How to create array from state value and setstate after removing duplicates in reactjs
- React - ComponentDidMount not getting value from Redux state
- React array state not pushing value
- array of selected values which I want to delete from state is not working as expected - React
- Updating Parent component state from multiple child components not taking updated state value into account
- Add value from input into array is not working in React
- Updating state from an array with react useState hook
- how do i update state in react for updating the value of an object inside an array using array.map function
- Updating Boolean Value in React State Array
- React jsx array component does not re-render when state changes
- React - input date field not updating value in state
- array.filter not removing value from array
- React - changing state not updating elements rendered from variable
- React setState does not update a state array value
- rule validation not triggered on input field when value is changed from React state
- React Redux store not updating on delete of an item from state
- React Dom not updating after updating a state array
- React Native - Text not updating with array value
- React array state is not updating
More Query from same tag
- Material-UI Checkbox is not reset while using react-hook-form reset function
- setState not maintaining set value after value set with Callback
- Pushing to array in React
- How to set the state of parent component in react from inside child component
- Redux Toolkit: have two consecutive dispatch and the latter is dependent upon the former
- How do I fix HandleToggleState?
- How can I change the array key value before passing to the state in react?
- Why does addEventListener and removeEventListener inside useEffect() require an arrow function?
- React state - updating property on object in nested array
- React save temporary file
- Tag 'CreateStudent' expects at least '5' arguments, but the JSX factory 'React.createElement' provides at most '2'
- React JS rendering nothing in .map
- How to setup SonarQube scanner in jenkins for a js/ts/react project
- ReactJS/JSX support in Netbeans IDE
- How to build variable from string in js?
- How to fill an svg image with specific length color
- SAML2.0 Authentication with Node.js and SPA
- How to change ag-grid date filter placeholder format
- Setting different color for material UI range slider's thumbs
- TypeScript error: JSX element type 'MultiSelect' does not have any construct or call signatures
- How to dynamically change API URL's in react app which ran in a Docker container without rebuilding?
- what's the difference between these two functions in react?
- Drag/Click on Audio Seekbar always pointed back to start of the audio element (React Js)
- Show React component based on click event
- Passing Async State to Next.js Component via Prop
- how to deploy successfully React app with nextjs to AWS Amplify?
- Receiving the Progress as a File Uploads
- Is there a need to do any cleanup when using document.createElement within a React ref?
- Why does my bundled file not include all jsx files?
- invalidateQueries is happening too quickly?