亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

react native 中的全局 onChange 處理程序

react native 中的全局 onChange 處理程序

慕后森 2022-11-03 14:46:20
我正在嘗試在 react-native 中創建一個全局 onchange 處理程序,但它沒有將我的電子郵件和密碼的值設置為我嘗試在谷歌上搜索的任何內容,但大多數示例都是基于 react.js 而不是 react-native 我將不勝感激立即幫助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 = text =>    setAuthDetails({      ...authDetails,      email: text.email,      password: text.name,    });  const login = () => {    console.log('EMAIL=', email, '\n', 'password =', password);  };  return (    <View>      <TextInput        name="email"        placeholder="Email"        onChangeText={onChange}        value={email}      />      <TextInput        name="password"        placeholder="Password"        onChangeText={onChange}        value={password}      />      <Button title="Login" onPress={login} />    </View>  );}
查看完整描述

1 回答

?
慕碼人8056858

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


查看完整回答
反對 回復 2022-11-03
  • 1 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號