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

标签 javascript react-native

我是 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/

相关文章:

javascript - 此函数如何处理缺少的参数

javascript - 延迟加载有时在 Angular 上不起作用

android - 单词上的点击事件 react native

reactjs - react native 转换 - 错误找不到预设“babel-preset-react-native-stage-0

react-native - 需要点击两次才能在 TextInput 之间切换(应该点击一下即可切换)

javascript - 3djs,力图。如何保留缩放级别

javascript - 如何指示动态表列表中 <tr> 表行的位置

javascript - Jquery on expand,输入框不在标签旁边

android - 此过程中未初始化默认 FirebaseApp - React Native - 本地通知

iOS 启动屏幕不显示