简体   繁体   English

React:DRY 功能组件集 state 带变量

[英]React: DRY Functional component set state with variable

I have a series of Input components and want to use only one onChange function.我有一系列输入组件,只想使用一个 onChange function。

Within a Class component I know I am able to do something like the following to adapt to the different states:在 Class 组件中,我知道我可以执行以下操作以适应不同的状态:

inputChange(input) {
    this.setState({
      [input.name]: input,
    });
  }

How, within a functional component, can I update state with only one onChange function?在功能组件中,如何仅使用一个onChange function 更新 state?

Example of a component:组件示例:

function Form(){
  const [selectedClient, updateSelectedClient] = useState(null);
  const [selectedLocation, updateSelectedLocation] = useState(null);
 

  return(

   <input
     value={selectedClient}
     id="selectedClient"
     name="selectedClient"
     onChange={???}
   />
   <input
    value={selectedLocation}
    id="selectedLocation"
    name="selectedLocation"
    onChange={???}
  />
  );
}

Thank you for the time!

Since this is one form I would combine the states into one, initialising it with an object, and then when the onChange handler is called update the property identified by the input id with the value.由于这是一种形式,我会将状态合并为一个,用 object 初始化它,然后当调用onChange处理程序时,用该值更新由输入id标识的属性。

useEffect is used here to log the result of updating the state.这里使用useEffect来记录更新state的结果。

 const { useEffect, useState } = React; function Example() { const [form, setForm] = useState({}); useEffect(() => { console.log(form); }, [form]) function handleChange(e) { // Grab the id and value from the input const { id, value } = e.target; // Create a new object by spreading out the current form // state, and setting the state property to the new value setForm({...form, [id]: value }); } return ( <div> Client: <input value={form.client} id="client" name="client" onChange={handleChange} /> Location: <input value={form.location} id="location" name="location" onChange={handleChange} /> </div> ); } // Render it ReactDOM.render( <Example />, document.getElementById("react") );
 input { display: block; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script> <div id="react"></div>

use the same method you used in the class, the difference is you combine the client and location into one object which will represent the state, and you take advantage of the id s:使用您在 class 中使用的相同方法,不同之处在于您将客户端和位置合并为一个 object,它将代表 state,并且您利用了id

 const {useState} = React; function Form(){ const [selectedClientOrLocation,setSelectedClientOrLocation] = useState({ selectedClient:"", selectedLocation:"" }) const handleChange = (e) =>{ setSelectedClientOrLocation({...selectedClientOrLocation,[e.target.id]:e.target.value}) } return( <form> <label htmlFor="selectedClient"> Client <input value={selectedClientOrLocation.selectedClient} id="selectedClient" name="selectedClient" onChange={handleChange} /> </label> <br/> <label htmlFor="selectedLocation"> Location <input value={selectedClientOrLocation.selectedLocation} id="selectedLocation" name="selectedLocation" onChange={handleChange} /> </label> </form> ) } ReactDOM.render(<Form/>,document.getElementById('root'))
 <script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> <div id="root"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM