Redux Form in react native, onChangeText with Value not work correcly

泪湿孤枕 提交于 2019-12-24 05:59:50

问题


I use TextInput with onChangeText and Value, Value for use initialValues, and onChangeText for change this value.

When I only use onChangeText without Value, it works correcly, but when I use onChangeText and Value it doen't works correcly.

I upload image that show the error. I want write "Hi, How are you?"

You can see the error in the following link:

https://i.stack.imgur.com/MiVNn.gif

My code:

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))

回答1:


I guess, you should pass most of the redux-form props.input to the react-native <TextInput />.

Here's a complete wrapper, taken from Easy forms in React Native with Redux Form article:

Input.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>
);

Usage:

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


来源:https://stackoverflow.com/questions/58818669/redux-form-in-react-native-onchangetext-with-value-not-work-correcly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!