'Redux Form in react native, onChangeText with Value not work correcly
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))
Solution 1:[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} />
Solution 2:[2]
The same thing happened to me. For me it was just changing the value property by defaultvalue
Here the complete explanation
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jordan Enev |
| Solution 2 | juagicre |
