Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Dynamic Var with `Set` State in React Hooks?

This is a pretty common pattern in React components:

handleTextFieldChange(event) {     const name = event.currentTarget.name;     this.setState({[name]: event.currentTarget.value}) } 

What Javascript syntax could be used to do the same with React hooks?

i.e. something possibly along the lines of:

handleTextFieldChange(event) {     const name = event.currentTarget.name;     this.set[name](event.currentTarget.value); } 
like image 309
VikR Avatar asked Feb 13 '19 21:02

VikR


People also ask

What is the usestate hook in react?

Line 1: We import the useState Hook from React. It lets us keep local state in a function component. Line 4: Inside the Example component, we declare a new state variable by calling the useState Hook. It returns a pair of values, to which we give names.

How to manage state on functional components in react?

There are many methods of managing state in React, including class-based state management and third-party libraries like Redux. In this tutorial, you’ll manage state on functional components using a method encouraged by the official React documentation: Hooks. Hooks are a broad set of tools that run custom functions when a component’s props change.

Can I name my own state variables in react?

The names on the left aren’t a part of the React API. You can name your own state variables: This JavaScript syntax is called “array destructuring”. It means that we’re making two new variables fruit and setFruit, where fruit is set to the first value returned by useState, and setFruit is the second. It is equivalent to this code:

Why do hooks always start with “usestate”?

“Create” wouldn’t be quite accurate because the state is only created the first time our component renders. During the next renders, useState gives us the current state. Otherwise it wouldn’t be “state” at all! There’s also a reason why Hook names always start with use. We’ll learn why later in the Rules of Hooks.


Video Answer


2 Answers

How about something like this?

function handleTextFieldChange(mySetFunction, event) {     const value = event.currentTarget.value;     mySetFunction(value); }  <TextField     placeholder="Email"     name="passwordResetEmailAddress"     onChange={(e) => handleTextFieldChange(setPasswordResetEmailAddress, e)} >     {passwordResetEmailAddress} </TextField> 

I've tested it and it works.

like image 21
VikR Avatar answered Sep 20 '22 15:09

VikR


You could use a single useState with a default value of an object that contains all your input values and update that like you are used to with class components.

Example

const { useState } = React;    function App() {    const [state, setState] = useState({ email: "", password: "" });    function onChange(event) {      const { name, value } = event.target;      setState(prevState => ({ ...prevState, [name]: value }));    }      return (      <div>        <input value={state.email} name="email" onChange={onChange} />        <input value={state.password} name="password" onChange={onChange} />      </div>    );  }    ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>    <div id="root"></div>
like image 108
Tholle Avatar answered Sep 19 '22 15:09

Tholle