Last active
September 7, 2018 11:55
-
-
Save frkosk/fabe7d4744eeec7f02934c6392122738 to your computer and use it in GitHub Desktop.
string as an object reference a.k.a Computed property names in react setState
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // define setState object based on string reference | |
| const stateNameInString = 'MyState'; | |
| const Obj = {}; | |
| Obj[stateNameInString] = 'bla bla'; | |
| this.setState(Obj); | |
| // OR | |
| this.setState({ [stateNameInString]: 'bla bla' }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment