问题
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