我在编写 React 组件时是如何思考的。🤔
这将简要演示我在编写 React 组件时通常如何思考。
假设我想创建一个表单组件。
我并不关心表单此刻会有哪些字段。
import React from 'react';
function Form() {
return (
<form>
{/* */}
</form>
)
}
export default Form;
我想添加一个firstName
字段。
import React, { useState } from 'react';
function Form() {
const [firstName, setFirstName] = useState('');
const handleFirstNameChange = ({ target }) => {
setFirstName(target.value);
}
return (
<form>
<div>
<label htmlFor="firstName">First name</label>
<div>
<input
id="firstName"
onChange={handleFirstNameChange}
type="text"
value={firstName}
/>
</div>
</div>
</form>
)
}
export default Form;
看起来不错。😎
我想添加一个lastName
字段。
import React, { useState } from 'react';
function Form() {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const handleFirstNameChange = ({ target }) => {
setFirstName(target.value);
}
const handleLastNameChange = ({ target }) => {
setLastName(target.value);
}
return (
<form>
<div>
<label htmlFor="firstName">First name</label>
<div>
<input
id="firstName"
onChange={handleFirstNameChange}
type="text"
value={firstName}
/>
</div>
</div>
<div>
<label htmlFor="lastName">Last name</label>
<div>
<input
id="lastName"
onChange={handleLastNameChange}
type="text"
value={lastName}
/>
</div>
</div>
</form>
)
}
export default Form;
添加第二个字段就容易多了。
我使用了我的copy paste
力量。
我想添加一个email
字段。
我将再次使用我的力量。🐱🏍
import React, { useState } from 'react';
function Form() {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [email, setEmail] = useState('');
const handleFirstNameChange = ({ target }) => {
setFirstName(target.value);
}
const handleLastNameChange = ({ target }) => {
setLastName(target.value);
}
const handleEmailChange = ({ target }) => {
setEmail(target.value);
}
return (
<form>
<div>
<label htmlFor="firstName">First name</label>
<div>
<input
id="firstName"
onChange={handleFirstNameChange}
type="text"
value={firstName}
/>
</div>
</div>
<div>
<label htmlFor="lastName">Last name</label>
<div>
<input
id="lastName"
onChange={handleLastNameChange}
type="text"
value={lastName}
/>
</div>
</div>
<div>
<label htmlFor="email">Email</label>
<div>
<input
id="email"
onChange={handleEmailChange}
type="email"
value={email}
/>
</div>
</div>
</form>
)
}
export default Form;
...
然后我想添加一个password
字段。
...
然后我想添加另一个字段。
...
...
停!🤚
每个新领域都会转化为以下三个变化:
- 为字段添加状态并设置状态操作
- 为输入添加新的事件处理程序
- 添加字段的 HTML
现在是我发挥真正力量的时候了。
我会尝试减少发生的变化的数量。
我不想为每个输入添加一个新的事件处理程序。
每个事件处理程序中唯一改变的是被调用的操作。
我将把它作为一个论据。
import React, { useState } from 'react';
function Form() {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [email, setEmail] = useState('');
const handleChange = setStateAction => ({ target }) => {
setStateAction(target.value);
}
return (
<form>
<div>
<label htmlFor="firstName">First name</label>
<div>
<input
id="firstName"
onChange={handleChange(setFirstName)}
type="text"
value={firstName}
/>
</div>
</div>
<div>
<label htmlFor="lastName">Last name</label>
<div>
<input
id="lastName"
onChange={handleChange(setLastName)}
type="text"
value={lastName}
/>
</div>
</div>
<div>
<label htmlFor="email">Email</label>
<div>
<input
id="email"
onChange={handleChange(setEmail)}
type="email"
value={email}
/>
</div>
</div>
</form>
)
}
export default Form;
我现在将尝试添加该password
字段。
import React, { useState } from 'react';
function Form() {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleChange = setStateAction => ({ target }) => {
setStateAction(target.value);
}
return (
<form>
<div>
<label htmlFor="firstName">First name</label>
<div>
<input
id="firstName"
onChange={handleChange(setFirstName)}
type="text"
value={firstName}
/>
</div>
</div>
<div>
<label htmlFor="lastName">Last name</label>
<div>
<input
id="lastName"
onChange={handleChange(setLastName)}
type="text"
value={lastName}
/>
</div>
</div>
<div>
<label htmlFor="email">Email</label>
<div>
<input
id="email"
onChange={handleChange(setEmail)}
type="email"
value={email}
/>
</div>
</div>
<div>
<label htmlFor="password">Password</label>
<div>
<input
id="password"
onChange={handleChange(setPassword)}
type="password"
value={password}
/>
</div>
</div>
</form>
)
}
export default Form;
好的,看起来好一点了。
我想我可以从清单中把它划掉。
- 为字段添加状态并设置状态操作
-
为输入添加新的事件处理程序 - 添加字段的 HTML
我不想为每个字段添加新状态并设置状态操作。
我将更新事件处理程序,因为我将使用一个设置状态操作。
我还将为这些输入添加一个名称属性。
import React, { useState } from 'react';
function Form() {
const [values, setValues] = useState({});
const handleChange = ({ target }) => {
setValues(prev => ({ ...prev, [target.name]: target.value }));
}
return (
<form>
<div>
<label htmlFor="firstName">First name</label>
<div>
<input
id="firstName"
name="firstName"
onChange={handleChange}
type="text"
value={values.firstName || ''}
/>
</div>
</div>
<div>
<label htmlFor="lastName">Last name</label>
<div>
<input
id="lastName"
name="lastName"
onChange={handleChange}
type="text"
value={values.lastName || ''}
/>
</div>
</div>
<div>
<label htmlFor="email">Email</label>
<div>
<input
id="email"
name="email"
onChange={handleChange}
type="email"
value={values.email || ''}
/>
</div>
</div>
<div>
<label htmlFor="password">Password</label>
<div>
<input
id="password"
name="password"
onChange={handleChange}
type="password"
value={values.password || ''}
/>
</div>
</div>
</form>
)
}
export default Form;
好的,我也将把那一项划掉。
-
为字段添加状态并设置状态操作 -
为输入添加新的事件处理程序 - 添加字段的 HTML
我现在快要发狂了。
import React, { useState } from 'react';
const fields = [
{
id: 'firstName',
label: 'First name',
name: 'firstName',
type: 'text'
},
{
id: 'lastName',
label: 'Last name',
name: 'lastName',
type: 'text'
},
{
id: 'email',
label: 'Email',
name: 'email',
type: 'email'
},
{
id: 'password',
label: 'Password',
name: 'password',
type: 'password'
}
];
function Form() {
const [values, setValues] = useState({});
const handleChange = ({ target }) => {
setValues(prev => ({ ...prev, [target.name]: target.value }));
}
return (
<form>
{fields.map(({ id, label, name, type }, index) => (
<div key={index}>
<label htmlFor={id}>{label}</label>
<div>
<input
id={id}
name={name}
onChange={handleChange}
type={type}
value={values[name] || ''}
/>
</div>
</div>
))}
</form>
)
}
export default Form;
好吧,现在当我想添加一个字段时,我只需在我的字段数组中添加一个即可。😁
-
为字段添加状态并设置状态操作 -
为输入添加新的事件处理程序 -
添加字段的 HTML
你怎么认为?
鏂囩珷鏉ユ簮锛�https://dev.to/potouridisio/how-i-think-when-i-write-a-react-component-1g69