【问题标题】:getting Property 'value' does not exist on type 'number'.ts(2339) on onChange function of react native text input在反应本机文本输入的onChange函数上,类型'number'.ts(2339)上不存在获取属性'值'
【发布时间】:2020-09-22 21:48:11
【问题描述】:

嘿,我对打字稿比较陌生。它告诉我 onChange 函数的 e.target 属性上没有 value 属性。我只是想让它更新 userObj 的状态,允许我保存该数据,然后让我在另一个组件中查看该数据。

import React, { useState } from 'react';
import {
    View,
    Image,
    Text,
    TextInput,
    StyleSheet,
    SafeAreaView,
    KeyboardAvoidingView,
  } from 'react-native';
import { TouchableOpacity, ScrollView } from 'react-native-gesture-handler';
import profile from '../assets/IMG_7767.psd'
import UserStore from '../store/UserStore'

interface Props {

}


  const Profile: React.FC<Props> = () =>  {
    const { user } = UserStore 
    const [firstName, setFirstName] = useState(user[0].firstName)
    const [lastName, setLastName] = useState(user[0].lastName)
    const [location, setLocation] = useState(user[0].location)
    const [description, setDescription] = useState(user[0].description)
    const userObj = {
      firstName: firstName,
      lastName: lastName,
      location: location,
      description: description
    }

    return (
      <SafeAreaView style={styles.blue}>
        <KeyboardAvoidingView>
          <ScrollView>
            <View style={styles.center}>
                <View style={styles.head}>
                  <Text style={styles.headText}>Profile Details</Text>
                </View>
                <View style={styles.shadow}>
                  <Image source={profile} style={styles.profileImage}/>
                </View>
                <View style={styles.formField}>
                  <Text style={styles.formText}>First Name</Text>
                  <TextInput 
                  style={styles.textInput} 
                  value={firstName} 
                  onChange={(e) => setFirstName(e.currentTarget)}
                  />
                </View>
                <View style={styles.formField}>
                  <Text style={styles.formText}>Last Name</Text>
                  <TextInput 
                  style={styles.textInput}
                  value={lastName} 
                  onChange={(e) => setLastName(e.target.value)}
                  />
                </View>
                <View style={styles.formField}>
                  <Text style={styles.formText}>Location</Text>
                  <TextInput 
                  style={styles.textInput}
                  value={location} 
                  onChange={(e) => setLocation(e.target.value)}
                  />
                </View>
                <View style={styles.formField}>
                  <Text style={styles.formText}>Description</Text>
                  <TextInput 
                  style={styles.textArea}
                  value={description} 
                  onChange={(e) => setDescription(e.target.value)}
                  />
                </View>
                <TouchableOpacity style={styles.mainButton} onPress={()=> user[0].update(userObj)}>
                  <Text style={styles.mainButtonText}>Save</Text>
                </TouchableOpacity>
            </View>
          </ScrollView>
        </KeyboardAvoidingView>
      </SafeAreaView>
    )
}

我查看了有关堆栈溢出的其他帖子,但找不到与此特定问题相关的任何帖子。我在网上找到的其他解决方案说使用 getElementById().value 但我认为会有一种更简单的方法来使用 react-native。我还看到了一个解决方案,它说 e.currentTarget 而不是 e.target 我假设这将具有与我当前具有相同的结果,因为它与 onChange 属性相关并且一次只有一个文本输入正在更改。第二个解决方案对我也不起作用

【问题讨论】:

    标签: reactjs typescript react-native mobx-state-tree


    【解决方案1】:

    你为什么不这样使用onChangeText

    onChangeText={(text)=> setFirstName(text)}
    

    在这里查看文档https://reactnative.dev/docs/textinput

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-10
      • 2019-08-11
      • 2021-08-02
      • 2020-06-02
      • 2022-07-09
      • 2022-10-04
      • 2023-02-07
      • 2021-11-16
      相关资源
      最近更新 更多