lig*_*sky 17 reactjs react-hooks
我发现有几种方法可以处理带有钩子的用户文本输入。用钩子处理输入的更优选或更合适的方法是什么?您会使用哪个?
1)最简单的钩子来处理输入,但是您拥有更多的字段,必须编写更多的重复代码。
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
Run Code Online (Sandbox Code Playgroud)
事件:
onChange={event => setPassword(event.target.value)}
onChange={event => setUsername(event.target.value)}
Run Code Online (Sandbox Code Playgroud)
2)与上面的示例类似,但具有动态键名
const [inputValues, setInputValues] = useState({
username: '', password: ''
});
const handleOnChange = event => {
const { name, value } = event.target;
setInputValues({ ...inputValues, [name]: value });
};
Run Code Online (Sandbox Code Playgroud)
事件:
onChange={handleOnChange}
Run Code Online (Sandbox Code Playgroud)
3)通常,最好使用替代方法useState,如ReactJS文档所述。useReduceruseState
const [inputValues, setInputValues] = useReducer(
(state, newState) => ({ ...state, ...newState }),
{username: '', password: ''}
);
const handleOnChange = event => {
const { name, value } = event.target;
setInputValues({ [name]: value });
};
Run Code Online (Sandbox Code Playgroud)
事件:
onChange={handleOnChange}
Run Code Online (Sandbox Code Playgroud)
4)useCallback将返回一个已记忆的回调版本,仅当其中一个依赖项已更改时才更改。
const [inputValues, setInputValues] = useState({
username: '', password: ''
});
const handleOnChange = useCallback(event => {
const { name, value } = event.target;
setInputValues({ ...inputValues, [name]: value });
});
Run Code Online (Sandbox Code Playgroud)
事件:
onChange={handleOnChange}
Run Code Online (Sandbox Code Playgroud)
She*_*heo 28
是的,您可以使用 useState() 处理反应钩子
import React, {useState} from 'react'
export default () => {
const [fName, setfName] = useState('');
const [lName, setlName] = useState('');
const [phone, setPhone] = useState('');
const [email, setEmail] = useState('');
const submitValue = () => {
const frmdetails = {
'First Name' : fName,
'Last Name' : lName,
'Phone' : phone,
'Email' : email
}
console.log(frmdetails);
}
return(
<>
<hr/>
<input type="text" placeholder="First Name" onChange={e => setfName(e.target.value)} />
<input type="text" placeholder="Last Name" onChange={e => setlName(e.target.value)} />
<input type="text" placeholder="Phone" onChange={e => setPhone(e.target.value)} />
<input type="text" placeholder="Email" onChange={e => setEmail(e.target.value)} />
<button onClick={submitValue}>Submit</button>
</>
)
}
Run Code Online (Sandbox Code Playgroud)
nce*_*sar 26
这就是我现在使用的方式:
const [inputValue, setInputValue] = React.useState("");
const onChangeHandler = event => {
setInputValue(event.target.value);
};
<input
type="text"
name="name"
onChange={onChangeHandler}
value={inputValue}
/>
Run Code Online (Sandbox Code Playgroud)
Jon*_*lms 11
如何编写返回输入值...及其<input>本身的可重用函数:
function useInput({ type /*...*/ }) {
const [value, setValue] = useState("");
const input = <input value={value} onChange={e => setValue(e.target.value)} type={type} />;
return [value, input];
}
Run Code Online (Sandbox Code Playgroud)
然后可以用作:
const [username, userInput] = useInput({ type: "text" });
const [password, passwordInput] = useInput({ type: "text" });
return <>
{userInput} -> {username} <br />
{passwordInput} -> {password}
</>;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7301 次 |
| 最近记录: |