【发布时间】: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