2

I have one form component, in which we have one input field.once user enter a digit to input field and press submit, it should become two time of that input value.

import {Field} 'redux-form'; 
class Multiply extends React.Component {

  onInputChange = (stateKey) => (e) => {
    this.setState({ [stateKey]: e.currentTarget.value.trim() });
  };

  onSubmit = () => {
    this.setState({ volume: this.state.volume *2  });
  };

  render(): JSX.Element {
    return (
      <div>
        <div>
          <form onSubmit={this.props.handleSubmit(this.onSubmit)}>
            <Field
              name="volume"
              placeholder="Volume"
              component={renderInput}
              type="number"
              value={this.state.volume}
              onChange={this.onInputChange('volume')}
              validate={required}
            />

            <div>
              <button type="submit">
                Multiply by 2
              </button>
            </div>
          </form>
        </div>
      </div>
    );
  }
}

once i click on button, value doesn't get change. but instead of <field> if i use <input> then it works fine. why ?

1 Answer 1

2

Redux Form inputs are dictated by their Redux state. This means you have to use its API to deal with them, as Field is giving your renderInput function an input value that equals whatever is in the Redux store for that field name.

What you'll need to do is

  1. Inject the field value. A good way of doing so is by using formValues()
  2. Inject a function that dispatches a change action for that form and for that field
  3. Call such function in your button.

This is the gist of it:

const MyForm = ({ double }) => {
    /* Your form implementation, then */
    <button type="submit" onClick={double}>Double and submit</button>
};

const mapDispatchToProps = (dispatch, props) => ({
    double: () => dispatch(change("foo", "number", props.number * 2))
});

const WithDoublingFunction = formValues("number")(connect(null, mapDispatchToProps)(MyForm));

export default reduxForm({
    form: "foo",
    // the rest of your config
})(WithDoublingFunction);

Here's a working sandbox of such functionality

Sign up to request clarification or add additional context in comments.

3 Comments

I have one container component that wraps this component. That wrapper component connected to redux. In your Answer you use same component. how to achieve this ?
I don't understand you @MukundKumar
@mukund You pass action methods to update state in the container component as props to your child component (the form / UI I presume). Call action methods in the child and it'll update the state in the parent container component (that is connected to redux). If you've already got a smart / dumb component model in place, I'm surprised you ask this question as it's a really common pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.