score:1
Accepted answer
Have modified your code and the working example can be found here.
The changes can be found here:
toggle = (index, newname) => {
this.setState((prevState, props) => ({
// Return new array, do not mutate previous state.
names: [
...prevState.names.slice(0, index),
{ name: newname },
...prevState.names.slice(index + 1),
],
}));
}
render() {
const names = this.state.names.map((name, index) => (
// Need to bind the index so callback knows which item needs to be changed.
<Card key={index} change={this.toggle.bind(this, index)} {...name} />
))
return (
<div>
{names}
</div>
);
}
The idea is that you need to pass the index into the callback function via .bind
, and return a new state
array with the modified name. You need to pass the index so that the component knows which object to change the name
to newname
.
score:1
I would use this for the toggle method:
toggle = (nameYouWantChanged, nameYouWantItChangedTo) => {
this.setState({
names: this.state.names.map(obj =>
obj.name === nameYouWantChanged
? { name: nameYouWantItChangedTo }
: obj
)
})
}
Source: stackoverflow.com
Related Query
- How can I use `setState` with objects nested in an array in React JS?
- How can I render an Array nested in another Array with React
- How to use setState with nested JSON array object
- React Hooks: how to properly map an array with nested arrays and objects inside a select element?(new coder)
- How can I use react router in nested page with "exact paramater" for my this scenario?
- How to fix Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
- How can modify an object of a deep nested array of objects with lodash?
- How can I use multiple refs for an array of elements with hooks?
- how can I use fs in react with electron?
- How can I use React with Google Places API, to display place markers on a Google map?
- In React Native, How Can I Use KeyboardAvoidingView with a Modal in iOS?
- How can I use bootstrap in react with rails
- How can I use grid layout with Fluent UI React
- React State update a nested array with objects based on the id when iterated
- How to count multiple objects inside nested array sequentially in react js
- How can I use Promise.all on an array of objects and then reassign them to the relevant key in a new object?
- Objects are not valid as a React child (found: object with keys {username}). If you meant to render a collection of children, use an array instead
- How can I access a local JSON file with Redux to use throughout my React app?
- How can I setState of in a nested array
- How can I use a React 16.3 Context provider with redux store?
- How to use react setState with chrome API
- How can I use multiple refs for nested arrays of elements with hooks, or any other way
- How can I cache nested objects with Apollo Client?
- Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
- How can I use an effect once, but still use state, with React Hooks?
- How can I use react masonry with react-infinite-scroller?
- How to delete objects from react state hook array with a button click
- Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead
- How to map through nested array of objects in React
- React how to conditionally edit or add object in array of objects using setState
More Query from same tag
- Not able to understand the issue in react dependency
- How can I change icon in the KeyboardTimePicker (material-ui-pickers)?
- Transitioning between two components tied to a stateful value
- Reactjs - parent, child action and functions
- building a higher order component for `Field` redux component
- React won`t render array map function
- Showing loading indicator only on initial load in graphql Query
- Building an atomic design development ecosystem in javascript
- How to change ANTD table text to different language
- bootstrap scss with reactjs not adding bootstrap classes
- Fetch API in React for JSON file: Uncaught (in promise) SyntaxError: Unexpected token � in JSON at position 0
- How to pass props without value to component
- TypeError: dispatch is not a function when using react-context api
- Delay suggestion time in MUI Autocomplete
- Parsing JSON response in React to State
- React js - Protected Routes
- Calling function from parent to child component in React
- React, update array's total count state when new item is added to it
- Writing a constructor in React.js
- React route with multiple layouts, re-render?
- Passing data from Child functional component to parent - React/Typescript
- How to improve react redux performance?
- react - .include return unexpected result
- Reactjs to change the state of a component based on child component's class
- How to set props value as selected checked in dropdown
- How to use Variables in setState in JSX
- QuerySelector is not recognising #id with space
- I want to display different contents when there is no item in map or filter
- change opacity and animated that with react js
- Map children and flatten multidimensional array of objects