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 鉤子使用調用它的元素的新文本值調用函數,因此我們仍然需要區分狀態中要更新的參數。
添加回答
舉報
