score:0
Change your delItem function to be higher order... You can have delItem() be a function that returns a function, and that function can use a parameter passed to the delItem() function... Pass the fruit!
delItem = (fruit) => () => {
console.log('Click happened');
console.log(item)
// delete your fruit
}
Then bind onClick as follows:
<p className="del-btn" onClick={this.delItem(item)}> x </p>
score:1
Well, You can choose to send item ref directly in function onClick - this.delItem.bind(this, item)
delItem(item) {
let fruits= this.state.fruits;
function findFruit(element, index, array){
return element==item;
}
let index = fruits.findIndex(findFruit);
fruits.splice(index,1);
this.setState({fruits : fruits});
}
Source: stackoverflow.com
Related Query
- how to pass the current item to the function so that i can remove it from array in react component?
- How can i remove employees from the array that have endDate property higher than current selected month?
- How can i pass props from a functional button component to another function component that displays the prop?
- React-Redux: Copying an array from the state in Reducer function so that you can modify it
- React : how to pass and array from inside a Function to the return (JSX)
- How can I pass a value from the backend to a React frontend and use that value in the CSS for the frontend?
- how to capture the file of an input element so that I can pass it as an argument to another function in react
- How to remove one item from an array using filter() in ReactJS - function not working properly
- how do i pass array from select html element component to two text components that have different values in the array
- How can I pass the index from javascript map function to the onclick function?
- How to pass the array index value from array mapping to onClick function in React?
- How can I toggle the display of an array that is located within each object of another array that comes from an API?
- How can I pass the index from the filter function into the map function?
- How can I remove the multiple display of ``nomineename`` ( which is highlighted) from the below iterating function
- How to remove item from array stored in localstorage if the item already exists and changing state of button
- How can I fetch data from an array of objects, pass it through an API end point to .fetch() the needed object value using React?
- How can I pass the value from my function to another component in another file
- How to pass a variable from a post request to a function that takes params as string or array of string in Node.js?
- How to remove an item instantly from the UI which is in a useState array
- How can I pass in the arguments to a function in react from within the html?
- React/Mobx: How to pass single item from a mapped array into another component and retrieve only the item associated with the value clicked
- How can I display one item from this array in the render?
- How can I remove the underline of TextField from Material-UI?
- How can I remove unused imports/declarations from the entire project of React Typescript?
- React JS: how to properly remove an item from this.state.data where data is an array of objects
- What's the advantage of using $splice (from immutability-helper) over filter to remove an item from an array in React?
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
- How do I manage state on a React component that can have state changed from the parent or from events upon it?
- How do we return a Promise from a store.dispatch in Redux - saga so that we can wait for the resolve and then render in SSR?
- How can I remove the elevation (box shadow) from a material-ui AppBar over one small section?
More Query from same tag
- React - Used Route(path ,exact, component) to render a component on a specified path but it will only render if I hit refresh once the path matches
- POST http://localhost:3001/[object%20Promise] 404 (Not Found) React Axios
- React redux inputs
- Using jQuery in React to modify CSS of an element
- React Dropzone does not work correctly with .pkl files
- Material-UI Responsive Cards
- Simple React components won't update after Redux state change
- react-select: Is it possible to add animated components and custom components simultaneously?
- NextJS with SWR is showing initialData during validation when using with getServerSideProps
- Adding React to an existing page and getting URL parameter inside .js
- CSS Position 2 buttons inline-block in center of screen, while keeping hover behavior
- Firebase returning two different outputs
- Material Ui Drawer Broken on Build
- How to disable the Text field name is disappearing when we moved out the input filed box in react js
- Is there a way to programatically switch to hashHistory from browserHistory when on a webpack dev server?
- Bind URL to components rendered by the state
- Warning: Each child in a list should have a unique "key" prop
- useCallback to fetch data on button click
- How to access Sass variables using javascript inside a react component?
- Cannot get rid of this comma on the bottom of html. I am using react and in the index.html I don't see it there or in any of my components
- How can I use "home" state within "global" reducer?
- current number of slide using bootstrap carousel in reactJS
- Next.js dynamic route - 404 on page reload after added SLUG
- How to get a child component to control the parent in react?
- How do I set up a bootstrap in reactjs to be responsive?
- ReactJS element doesn't reload after state change
- Type 'boolean' is not assignable to type 'number'.ts
- i could not edit data by using react-redux
- Accessing cookies set at django backend server from the React client server
- RxJS inside React Functional Component