Is it possible to store an object in the state of the React component? If so, how can we change the key value in this object using setState ? I think it is syntactically not allowed to write something like:
this.setState({ abc.xyz: 'new value' });
Similarly, I have one more question: is it permissible to have a set of variables in the React component so that they can be used in any method of the component instead of being stored in state?
You can create a simple object that contains all of these variables and place it at the component level, just as you would declare any methods of the component.
It is very likely that you will encounter situations where you include a lot of business logic in your code, and this requires the use of many variables, the values ββof which are changed by several methods, and then you change the state of the component based on these values.
Thus, instead of storing all these variables in a state, you save only those variables whose values ββshould be directly reflected in the user interface.
If this approach is better than the first question I wrote here, then I don't need to keep the object in state.
javascript reactjs
Rahul Dole Nov 24 '14 at 12:48 2014-11-24 12:48
source share