I already know how to setState with a dynamic key name but how do I set a value as the state with a dynamic key?
Example
function thing(key) {
let stuff = this.state.key;
//Do stuff
}
Output: Open your browser. It will by default open a tab with localhost running and you can see the output shown in the image. Fill in the required details and click on the button. As you can see in the output new state with a dynamic name is created with the value you entered.
React will call that function with state and props, and use the result for next state. It is safe to call setState with a function multiple times. Updates will be queued and later executed in the order they were called.
StrictMode is a tool for highlighting potential problems in an application. Like Fragment , StrictMode does not render any visible UI. It activates additional checks and warnings for its descendants.
The state and props in React are always in an object format. This means that the value could be accessed from the state and props via key-value pair. To access the normal state object, you can use the key name from the object. The state object will look as shown below.
Didn't even think of think of this as first but all I did was
function thing(key) {
let stuff = this.state[key];
//Do stuff
}
and it worked!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With