我将TextInput与onChangeText和Value一起使用,将Value用作initialValues,将onChangeText更改此值。
当我只使用不带值的onChangeText时,它可以正确地工作,但是当我使用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))
最佳答案
我想,您应该将大多数redux-form
props.input
传递给react-native
<TextInput />
。
这是完整的包装,摘录自Easy forms in React Native with Redux Form文章:
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>
);
用法:
<Field name="firstName" component={Input} />