-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to change a Item value of a state. #14156
Comments
You could use the immutability-helper, that would look something like this: You have the following state, and want to change the first item in the list array. this.state = {
list: [1, 2, 3, 4, 5],
}; Using the update function, you can do the following: import update from 'immutability-helper';
this.setState({
list: update(this.state.list, {
indexYouEdit: {
$splice: [[indexYouEdit, amountOfRemoval, newValue, ...optionalNewValues]]
},
}),
}); Hope that helps you out! |
@HofmannZ ,Thanks for your help.But when I import update from 'immutability-helper' to my page, there is a error said that - Unable to resolve module 'immutability-helper' rom [MY_DIRECTORY];Modules does not exist in the module map or in theres directories .... do you have any idea about this ? |
Like any other package, you have to install it, depending on whether you use npm or yarn you would do one of the following: npm yarn This will add the package to your project. |
@HofmannZ ,I simply consider that was intergered into react-native already. :-) . And can you tell me is there any other way without importing a third-party lib to achieve this ? Will be grateful to solve my confusion.Thanks again! |
for example, I have a list state like this:
How can I change the value of the first item of list from 1 to 0?
I usually use like this but I don't believe that is the best way.
Please who can tell me is there any better way to do this ?
The text was updated successfully, but these errors were encountered: