score:2
Accepted answer
deleteHandler: function(e,index){
//copy array
var newAray = this.state.listdata.slice();
//delete element by index
newAray.splice(index, 1);
this.setState({listdata: newAray});
},
<button onClick={e=>this.deleteHandler(e,index)}>delete</button>
score:1
<button onClick={()=>this.deleteHandler(index)}>delete</button>
score:2
deleteHandler: function(index){
this.setState(state => {
// state.listdata - array of initial values,
state.listdata.splice(index, 1);
return {listdata: state.listdata}; // returns a new state
});
}
Source: stackoverflow.com
Related Query
- React JS - How does this function work to delete JSON data?
- How does this React code work on CodePen even if no preprocessor is selected?
- How does React Context work by using a function inside of a component
- how does the .render() function in react work internally?
- How does this arrow function work in my special case passing it through Redux
- angular scope vs react state how does it work for data binding
- How does an anonymous function work in onClick in React component?
- React JS: How to make this delete button work
- How does setState in React work in this scenario?
- React callback function does not work how to fix it?
- how to map this given json data in react js
- How does shallow compare work in react
- How Snapshot testing works and what does toMatchSnapshot( ) function do in Jest Snapshot testing for React components?
- How to parse json data in react native
- How does React know if a function component is a React component?
- How do you pass data when using the navigate function in react router v6
- React JS - How do I bind json data to a dropdown list
- How does React useState hook work with mutable objects
- how to display data in table using json in react js?
- In React why does calling a function from another function in our code not work and display?
- How to fetch data without useEffect hooks in React function component?
- react native how to filter data with some key object on json
- React - How to pass returned data from an exported function to a component?
- How to render Pretty json data inside text area React js?
- How does React useState and useCallback hook work when useCallback lacks dependencies
- How does React hook useRef() work under the hood? What is that reference exactly?
- How does Express and React Routes work on initial GET request from browser?
- How does placing an event handler in an anonymous function in React fix bind issues?
- How does this React example work?
- How to insert json data in React js?
More Query from same tag
- Not able to get values from Redux to props
- how to align a grid item center in material-ui in react?
- Cannot get the error.message (status 400+)
- Import styles bootstrap in Next Js project React SSR
- Next.js pages using Link module not loading files with CSS import
- Updating an object in an array if the object exists
- Paperclip Unpermitted parameter: image
- How to set flex-direction for <td> elements
- Right align the content in a column inside a row
- Unknown word error in react-datepicker component
- Maximum call stack size exceeded - reactjs
- Convert JSX string to JSX expression with .map (React)
- Is there an easy way of setting all other states other than e.target.name to false?
- Extend Background of Drop-down Menu to Full Width of Browser
- React router history is string?
- Object Destructuring eslint throws react/prop-types
- How to get state of _app.js in Next.js?
- Annotate a function type to accept either a React.MouseEvent parameter or to remain anonymous
- React : display multiple hard coded options in Select component
- Change color of selected element - React
- React JS react-router-dom results in 404
- Display data in a single table in React Sharepoint from two REST API or from 2 different getbytitle name
- React - input checkbox onchange won't fire first time
- return this returns undefined
- How does this React code translate to class representation?
- Using SCSS variables in CSS Modules
- How to use React.hydrate when using vue?
- PersistGate opens before state loads
- password check without using regex in reactjs
- Each child in an array or iterator should have a unique "key" prop. Check the render method of `Abstractfactory`