1 回答

TA贡献1803条经验 获得超6个赞
您的方式可能固定如下:
import React, {useState} from 'react';
import {View, Text, Button, TextInput} from 'react-native';
import style from './Style';
export default function Login() {
const [authDetails, setAuthDetails] = useState({
email: '',
password: '',
});
const {email, password} = authDetails;
const onChange = update =>
setAuthDetails({
...authDetails,
...update
});
const login = () => {
console.log('EMAIL=', email, '\n', 'password =', password);
};
return (
<View>
<TextInput
name="email"
placeholder="Email"
onChangeText={text => onChange({ email: text }) }
value={email}
/>
<TextInput
name="password"
placeholder="Password"
onChangeText={text => onChange({ password: text }) }
value={password}
/>
<Button title="Login" onPress={login} />
</View>
);
}
从上面的代码中可以看出,onChangeText 钩子使用调用它的元素的新文本值调用函数,因此我们仍然需要区分状态中要更新的参数。
添加回答
举报