-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom component value #277
Comments
I have done it before wrapping a const normalizeValue = val => multi && val ? val.split(delimiter) : val || null;
...
<Select
...other props
onChange={changeValue => onChange(normalizeValue(changeValue))}
/> Where All you need to do to make an input component work with |
Yes, that makes sense. Thanks. Only thing I'm not sure about is how the value property gets populated in my component? |
I think I'm missing something obvious here. I have a complex component that has a state value which I want to connect to a redux-form onSubmit. Is there a function I can implement or a way of binding the state value to the redux form value? |
I'm not following you exactly, but let me write some code and see if something clicks in your head. class ProductPage extends Component {
componentWillMount() {
const product = API.loadProduct();
this.setState({product: product});
}
render() {
return <ProductForm
initialValues={this.state.product}
onSubmit={values => API.saveProduct(values)}/>;
}
}
@reduxForm({
form: 'product',
fields: ['name', 'tags']
})
class ProductForm extends Component {
render() {
const {fields: {name, tags}, handleSubmit} = this.props;
return <form onSubmit={handleSubmit}>
<input type="text" {...name}/>
<TagEditor {...tags}/> // passes name, value, onChange, among others
</form>;
}
}
class TagEditor extends Component {
state = {
value: this.props.value // initialize state from value prop given by redux-form
}
componentWillReceiveProps(nextProps) {
this.setState({value: nextProps.value}); // update state when props change
}
updateValue(value) {
this.setState({value: value}); // change local state
this.props.onChange(value); // notify redux-form
// calling setState() might not be necessary, because onChange() will result in the
// prop changing, which will trigger componentWillReceiveProps() and update the
// local state, but that will be on the next render cycle
}
render() {
const {value} = this.state;
// render input and call this.updateValue() to update the value.
}
} |
@erikras I have the similar issue, and I tried your code which works good. But I'm not sure about --UPDATE-- It seems the --UPDATE-- I think I fixed my issue, |
@martinwells looks like if you need preprocess your value, you should define
http://redux-form.com/5.0.1/#/examples/complex I did this myself and it's works as expected. If docs already contains example, then issue can be closed. |
Is this technique still relevant in redux-form 6.2? If so, could the example perhaps be updated and added to the documentation? |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hi, I built a custom tags editor component. When I submit a form I'd like to generate a custom value returned from the component (an array of tags for example). Do you have any examples of how to do this?
The text was updated successfully, but these errors were encountered: