score:0
Accepted answer
you can change the value at the index and set the state variable again:
const handlechange = (index, value) => {
const data = [...polldata]
data.splice(index, 1, value) /* or data[index] = value */
setpolldata(data)
}
polldata && polldata.map((entry, index) => {
return(
<div classname="input-group mb-2" key={index}>
<input placeholder={"option " + index} classname="form-control" value={entry} onchange={(e) => handlechange(index, e.target.value)}}></input>
<button tabindex="-1" classname="btn btn-outline-danger" onclick={() => handledelete(index)}> x </button>
</div>
)
})
you shouldn't mutate the state variable directly. if it is an object or an array, make a copy of it, alter the copy as you wish and set the state variable to the altered copy.
Source: stackoverflow.com
Related Query
- Changing specific element in array in useState
- Changing classname of a specific element on click from a mapped array an keeping the same classname until clicked again in react
- How to push into an array of object using the spread operator to a specific element
- Changing vertical scroll to horizontal at a point for a specific element
- Remove first element of an array in React useState hooks?
- Redux, React, MapStateToProps: Retrieve a specific element in an array from the redux state tree
- Opening a single element in an array with useState
- Can't remove specific elements from array by index number from dynamic div element in react?
- Changing React State Array at Specific Index
- useState delete array element in the state containing object
- React UseState Boolean Array Modify One Element
- Button element in array with onClick property to change state the specific button that is clicked
- push an element to an array with useState hooks
- React - how to update a specific field in Object Array using useState in UseEffect
- Update a specific variable of a useState array (by its place) in React.js?
- React change specific element in an array of objects
- React setAttribute to specific element from .map array
- How to remove specific element from a array or object using ES6 spread operators and rest operators
- React/NextJS - useEffect - update specific array element
- changing some specific value react usestate
- Changing an array of objects with React useState
- Get specific element on array of array JS without index equivalent with id (react)
- When I try to push an element to a array but it takes only one rest of the elements are got removed automatically. It happen when use useState hook
- Find last element in array from a specific index
- mapping and changing name of element in array object
- changing array element property in react
- Return new Array with specific element
- traverse (iterate) each element of array in specific time?
- ReactJs UseState : insert element into array not updating
- remove specific element from array with duplicate react-redux
More Query from same tag
- How to enable the React Semantic UI Modal to stretch across the full screen?
- React-hook-form: unable to successfully implement MUI Select component
- Printing generated QR codes on a PDF file
- How to use the useHook in the component rather than passing the value returned from it as a prop to the components using react and typescript?
- Getting Error TypeError: Cannot read property 'rows' of null
- Reactjs Add spinner when click button
- Replace an element in an array state (and benefit from ReactJS fast rendering)
- Fetch data with redux saga
- React, Conditionally rendering a component based upon communication between a parent and two children components
- How to pass function as props in React?
- React map changes index number and breaks conditional rendering
- Can text within an iframe be copied to clipboard?
- How to render fontawesome as button in ReactJS
- Pass a wrapper component as props in ReactJS
- How to set a state of an array of object inside an array of objects in react
- What is the best way to trigger an redux action if the props changed after the react 16.3?
- select method to restore to default value
- deployment of react.js application to apache server via react-scripts
- Module not found: Can't resolve 'google-maps-react' in 'C:\Users\USUARIO
- React.js: Modeling selection of objects
- React.semantic ui's Icones are not loading with webpack middle ware
- Placing an icon inside a text input in React component
- Is there a way to skip the yup validation to the last object in an array of objects?
- radio inputs repeating on selection (React)
- issue loading css module classes in storybook v6.4
- How to change the collapse/expand icon to right side of TreeView of material?
- React-Redux - Show and Hide Component
- Effective onBlur for react-data-grid
- Toggle dropdown menu in reactjs
- React component not rendering on with latest version