gpt4 book ai didi

javascript - 如何使数组处于状态并处理handleFormChange

转载 作者:行者123 更新时间:2023-12-02 22:57:05 25 4
gpt4 key购买 nike

我是 React Native 的新手,我只是将 React JS 实现为 Native 来进行 HandleFormChange,但我收到了这样的错误“无法准备好未定义的属性名称”。我不知道该怎么办。

这是用于 React Native,在 Android 和 Node 中运行

constructor(props) {
super(props)
this.state = {
post : [],
formData : {
id : '',
first_name : '',
last_name : '' ,
email : '',
gender : '',
password : '',
}
}
}


getPostAPI () {

axios.get('http://192.168.100.7:3000/datadiri')
.then((res) => {
this.setState ({
post : res.data
})
})
}

sendDataDiri () {
axios.post('http://192.168.100.7:3000/datadiri', this.state)
.then((res) => {

this.setState ({
formData : {
id : '',
first_name : '',
last_name : '',
email : '',
gender : '',
password: ''
}
})
}, (err) => {
console.log('error : ', err);
})
}

handleForm = (e) => {
let formDataNew = {...this.state.formData};
formDataNew[e.target.name] = e.target.value;
this.setState({
formData : formDataNew
})

}
render() {
return (
<RegisterComponent
handleFirstChange = {this.handleFirstChange}
handleLastChange = {this.handleLastChange}
handleEmailChange = {this.handleEmailChange}
handleGenderChange = {this.handleGenderChange}
handlePasswordChange = {this.handlePasswordChange}
onBackPress = {this.onBackPress}
handleForm = {this.handleForm}
onRegisterPress={this.onRegisterPress}
first_name = {this.state.formData.first_name}
last_name = {this.state.formData.last_name}
email = {this.state.formData.email}
gender = {this.state.formData.gender}
password = {this.state.formData.password}
/>
);

注册COMP

<Text style={styles.textStyle}>First Name</Text>
<TextInput style={styles.textboxfield}
underlineColorAndroid='transparent'
onChangeText={this.props.handleForm}
value={this.props.first_name}
placeholder = {'First Name'}
placeholderTextColor={'rgba(221,221,221,1)'}
name="first_name"
/>
<Text style={styles.textStyle}>Last Name</Text>
<TextInput style={styles.textboxfield}
underlineColorAndroid='transparent'
onChangeText={this.props.handleForm}
value={this.props.last_name}
placeholder = {'Last Name'}
placeholderTextColor={'rgba(221,221,221,1)'}
/>
<Text style={styles.textStyle}>Email</Text>
<TextInput style={styles.textboxfield}
underlineColorAndroid='transparent'
onChangeText={this.props.handleForm}
value={this.props.email}
placeholder = {'Email'}
placeholderTextColor={'rgba(221,221,221,1)'}
/>
<Text style={styles.textStyle}>Gender</Text>
<TextInput style={styles.textboxfield}
underlineColorAndroid='transparent'
onChangeText={this.props.handleForm}
value={this.props.gender}
placeholder = {'Gender'}
placeholderTextColor={'rgba(221,221,221,1)'}
/>
<Text style={styles.textStyle}> Password </Text>
<View>
<TextInput
style={styles.textboxfield}
underlineColorAndroid='transparent'
onChangeText={this.props.handleForm}
returnKeyType={'done'}
value={this.props.password}
placeholder = {'Password'}
placeholderTextColor={'rgba(221,221,221,1)'}
/>
</View>

<TouchableOpacity style={styles.loginButtonStyle} onPress={this.props.onRegisterPress}>
<Text style={styles.loginButtonTextStyle}>Register</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.regButtonStyle} onPress={this.props.onBackPress}>
<Text style={styles.loginButtonTextStyle}>Back</Text>
</TouchableOpacity>


</View>
</View>

)
}
}

RegisterComponent.propTypes = {
handleFirstChange : PropTypes.func,
handleLastChange : PropTypes.func,
handleEmailChange : PropTypes.func,
handleGenderChange: PropTypes.func,
handlePasswordChange : PropTypes.func,
onBackPress : PropTypes.func,
showPassword: PropTypes.func,
handleForm : PropTypes.func,
onRegisterPress: PropTypes.func,
first_name: PropTypes.string,
last_name: PropTypes.string,
email: PropTypes.string,
gender: PropTypes.string,
password: PropTypes.string,
};

我需要知道如何使 e.target.name 正确,我只想将我的数据放入数组

最佳答案

onChangeText 采用文本而不是事件,但也有 onChange 属性,check the documentation了解更多详情。

// this method will has text argument but not event
handleForm = (e) => {
let formDataNew = {...this.state.formData};
formDataNew[e.target.name] = e.target.value;

this.setState({
formData : formDataNew
)
}

已更新

您应该修改handleFormTextInput:

// now it takes field name and value
handleFieldChange = (text, name) => {
this.setState({
formData: {
...this.state.formData,
[name]: text
}
});
}

// Register component takes a state of your form
// and method to change fields value
<RegisterComponent
handleFieldChange={this.handleFirstChange}
onBackPress={this.onBackPress}
onRegisterPress={this.onRegisterPress}
formState={this.state.formData}
/>

// you can write your fields like this
<TextInput style={styles.textboxfield}
underlineColorAndroid='transparent'
onChangeText={text => this.props.handleFieldChange(text, 'firstName')}
value={this.props.formState.firstName}
placeholder = {'First Name'}
placeholderTextColor={'rgba(221,221,221,1)'}
/>

// now you can send your data to the BackEnd
sendData = () => {
const { formData } = this.state;
// formData contains values of your fields
}

关于javascript - 如何使数组处于状态并处理handleFormChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57934503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com