Skip to content
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

Closed
Symous opened this issue May 24, 2017 · 4 comments
Closed

How to change a Item value of a state. #14156

Symous opened this issue May 24, 2017 · 4 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@Symous
Copy link

Symous commented May 24, 2017

for example, I have a list state like this:

this.state = {
    list:[1,2,3,4,5]
}

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.

var temp = this.state.list;
temp[0] = 0;
this.setState({ list : temp});

Please who can tell me is there any better way to do this ?

@Symous Symous changed the title How to change one Item of a state. How to change a Item value of a state. May 24, 2017
@HofmannZ
Copy link

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!

@hramos hramos closed this as completed May 24, 2017
@Symous
Copy link
Author

Symous commented May 25, 2017

@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 ?
PS: React Native 0.43 + Android ;

@HofmannZ
Copy link

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
Navigate to the root of your react-native project, then run:
npm install --save immutability-helper

yarn
Navigate to the root of your react-native project, then run:
yarn add immutability-helper

This will add the package to your project.

@Symous
Copy link
Author

Symous commented May 25, 2017

@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!

@facebook facebook locked as resolved and limited conversation to collaborators May 24, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants