react native中的Redux表单,带值的onChangeText无法正常工作

cbjzeqam  于 2022-11-12  发布在  React
关注(0)|答案(3)|浏览(152)

我将TextInput与onChangeText和Value一起使用,Value用于使用initialValues,onChangeText用于更改此值。
当我只使用onChangeText而不使用Value时,它可以正常工作,但当我同时使用onChangeText和Value时,它就不能正常工作了。
我上传显示错误的图片。我想写“嗨,你好吗?”
您可以在以下链接中看到该错误:
https://i.stack.imgur.com/MiVNn.gif
我的代码:

import { Field, reduxForm } from 'redux-form';
import { connect } from 'react-redux';

const fieldNombre = (props) => {

    return (
        <TextInput
            placeholder="Field nom"
            onChangeText={props.input.onChange}
            value={props.input.value}
            style={{borderWidth: 1, borderColor: 'white', color: 'white'}}
        />
    );
};

class EditForm2 extends Component {

    render() {
        console.log('this.props.initialValues');
        console.log(this.props.initialValues);
        return (
            <View>
                <Field name="firstName" component={fieldNombre} />
                <WhiteText>Redux Form</WhiteText>
                <Button
                    title="Registrar"
                    onPress={this.props.handleSubmit((values) => {
                        console.log(values);
                    })}
                />
            </View>
        );
    }
};

const mapStateToProps = (state) => {
  return {
    initialValues: {
      firstName: 'aaaaa',
      lastName: 'bbbbb',
      email: 'cccccc'
    }
  }
}

export default connect(mapStateToProps)(reduxForm({ form: 'EditForm2', enableReinitialize: true})(EditForm2))
kmynzznz

kmynzznz1#

同样的事情也发生在我身上。对我来说,它只是将value属性更改了defaultvalue
这里完整的解释

tag5nh1u

tag5nh1u2#

我猜,你应该把大部分的redux-form传给react-native
下面是一个完整的 Package 器,摘自Easy forms in React Native with Redux Form文章:

输入.js

import React from 'react';
import { TextInput, View, Text } from 'react-native';

// Your custom Input wrapper (adapter betwen `redux-form` and `react-native`
export default ({ input, ...inputProps }) => (
  <View>
    <TextInput
      {...inputProps}
      onChangeText={input.onChange}
      onBlur={input.onBlur}
      onFocus={input.onFocus}
      value={input.value}
      />
  </View>
);

用法:

<Field name="firstName" component={Input} />
tgabmvqs

tgabmvqs3#

如果要显示过去在TextInput中输入值,则必须使用defaultValue属性

相关问题