How to set state with a dynamic key name in ReactJS ? Last Updated : 13 Nov, 2023 Comments Improve Suggest changes Like Article Like Report When working with ReactJS, managing state is a fundamental aspect of building dynamic and interactive user interfaces. In some cases, you may encounter situations where you need to set a state with a dynamic key name, rather than a fixed one. This can be particularly useful when dealing with dynamic data or when you want to update the state based on user input or other dynamic factors. Prerequisites:NPM & Node.jsReact JSReact JS setStateApproach:To set a state with a dynamic key name in ReactJS we will use the setState method with the dynamic data. We will take the user input as the dynamic data and when the button is clicked it will call the setState method and update the state. Steps to Create a React Project:Step 1: Create a react application by typing the following command in the terminal. npx create-react-app project_name Step 2: Now, go to the project folder i.e. project_name by running the following command. cd project_nameProject Structure: Project StructureExample: This example implements dynamic state changes on click by taking the user input. JavaScript // Filename - App.js import React, { Component } from "react"; class App extends Component { constructor() { super(); this.state = { name: "", value: " ", }; } render() { return ( <div> <p>Enter State Name:</p> <input onChange={(e) => { this.setState({ name: e.target.value, }); }} type="text" ></input> <p>Enter State Value:</p> <input onChange={(e) => { this.setState({ value: e.target.value, }); }} type="text" ></input> <br /> <br /> <button onClick={() => { this.setState({ [this.state.name]: this.state.value, }); }} > Create a dynamic state </button> {this.state[this.state.name] ? ( <p> {this.state.name}: {this.state[this.state.name]} </p> ) : null} </div> ); } } export default App; Step to Run Application: Run the application using the following command from the root directory of the project: npm startOutput: This output will be visible on the http://localhost:3000/ on the browser window. Comment More infoAdvertise with us Next Article How to set state with a dynamic key name in ReactJS ? namankedia Follow Improve Article Tags : Web Technologies ReactJS Web technologies React-Questions Similar Reads How to Update an Object with setState in ReactJS? setState in React is used to update the component state. Updating an object with setState involves modifying only specified properties of object without modifying the complete object. Directly updating the object can lead to the loss of data and unwanted results.Prerequisites:ReactJSsetStateApproach 2 min read How to Work with and Manipulate State in React ? Working with and Manipulating state in React JS makes the components re-render on the UI by Updating the DOM tree. It makes sure to render the latest information and data on the interface. Prerequisites:NPM & Node.jsReact JSState in React JSReact JS Class componentsReact JS Functional Components 6 min read How to locally manage component's state in ReactJS ? Any component in React JS majorly depends on its props and state to manage data. A component's state is private to it and is responsible for governing its behavior throughout its life. A state is nothing but a structure that records any data changes in a react application. It can be used for storing 2 min read How to Update Parent State in ReactJS ? Updating the parent state in React involves passing a callback function as a prop to the child that triggers the state change and updates the data in the state.Prerequisites:ReactJSReact State and PropsReact Components ApproachTo update parent state in React we will pass a function as a prop to the 3 min read State Management with useState Hook in React useState is a built-in hook that empowers functional components to manage state directly, eliminating the need for class-based components or external state management libraries for simple use cases. It provides an easy mechanism to track dynamic data within a component, enabling it to React to user 3 min read Like