How to dynamically create state in React?

react dynamically add input fields to form
react update state with dynamic key
react change input value dynamically
react edit form example
react get state dynamic key
create state dynamically
react state
react state with id

Is it possible to create state without using an object inside the constructor?

I need to dynamically create new states inside the component on the fly. Any recommendations?

The state object is literally just a Javascript object, so it is already dynamic.

If you want, you can modify it anywhere, anytime. But this completely breaks the good design pattern that React gives you -- single source of truth for data, and no mutating any state objects gives you a perfect history of every action that has taken place, making debugging a piece of cake.

This is why they encourage you to create an initial state, then use setState, which just creates a new copy while modifying the fields you specified.

Skip down to 'Using State Correctly': https://facebook.github.io/react/docs/state-and-lifecycle.html

If you need to 'dynamically create new states on the fly', is there any reason you can't define these in the constructor with initial values, whether or not you use them? I don't see any reason they would have to not exist at first, instead of just have an initial state. More detail about what you're trying to do would be helpful.

How to declare dynamic state values in react, I create text inputs in a loop. loop { return <TextInput value  I can't find any examples of using a dynamic key name when setting the state. This is what I want to do: inputChangeHandler : function (event) { this.setState( { event.target.id : event.target.value } ); }, where event.target.id is used as the state key to be updated. Is this not possible in React?

in case you was using typescript, i would define your state like this:

interface IComponentState {
    dynamicState: any
}

and then you can assing any object to this dynamicState.... or any number of options to it...

in plain JS you even do not need to assign type, so it will be simplier - just assign empty object at start

react-add-state-fields-dynamically, Starter project for React apps that exports to the create-react-app CLI. Create two state variables countries and colours to store the details of few countries and colours as an array and an object respectively. Inside the constructor (), initialize the countries state variable to an empty array [] and the colours variable to an empty object {}.

Create state in inner method - render, is bad idea. React recommended use only setState(), or 'state = ...' in constructor.

Dynamically Update States in React - Kate Stamas, You probably are familiar with how to update states in React: this.setState({ name​: 'Kate' });. And this is great and all, but what if you have a form  How to dynamically add CSS Classes to React Elements using State If you’re just starting out with React, you might soon get to the point where you want to dynamically change the styling of some React Elements – e.g. at the click of a button.

Understanding this.setState({ [name]: value}), How do you add multiple input fields dynamically in react JS? Using arrays for dynamic inputs. Before we code, we should talk about how we are going to do this. Basically, we’re going to have an array of cat objects in our state. Each object will have a name and age value. Our Form will iterate over this list and create two new inputs for the name and age. When we click the “add new cat” button, we

Building a Dynamic, Controlled Form with React, I knew that changing an input field created an event object and that line 2 was… setState({ [name]: value}) useful in React? (name=) on each input type and dynamically sets the component's state based on the input field's  Use #array.map to create ui (input element) for each array values. while creating the fields, React js dynamic input field value to state. 0.

How to create different states using a for loop in reactstrap? · Issue , Using arrays for dynamic inputs​​ Each object will have a name and age value. Our Form will iterate over this list and create two new inputs for the name and age . When we click the “add new cat” button, we'll add a new object to our array. Since this will change our state, it will trigger a re-render. The good news is: How To Create Components Dynamically in React 16 is available! One of them most common tasks that we need to know how to do with React.js is to render a list of items dynamically - meaning: we don't know how many items we're going to render at any given time.

Comments
  • I didn't get the purpose of creating dynamic state. You can have empty state object in constructor and then manipulate the state object inside the component in any way you want. Can you elaborate your requirement here?
  • Does that mean that if I am in a iterator, at that time can I create a state for the same?
  • Ok I found the solution I was looking for. Thanks, your answer enhanced my search skills :) (stackoverflow.com/a/29281499/842607)