React-number-format: Doesn't work with redux field

Created on 28 Feb 2018  路  5Comments  路  Source: s-yadav/react-number-format

I tried to wrap the NumberFormat in redux-form/field but it doesn't get the value initialized or set although the inner input contains the correct value.

Here's the code:

<Field name={this.props.fieldName} component={NumberFormat} customInput={TextField} hintText={this.props.title} thousandSeparator={true} prefix={'$'}/>

Most helpful comment

I think redux-form uses the onChange handler to modify form state. You will need to wrap the NumberFormat component to call props.onChange (passed from redux-form) whenNumberFormat.onValueChange occurs.

All 5 comments

I think redux-form uses the onChange handler to modify form state. You will need to wrap the NumberFormat component to call props.onChange (passed from redux-form) whenNumberFormat.onValueChange occurs.

You pass the events object to onChange. Just in case anyone needed more clarification.

export default ({ inputRef, onChange, ...other }) => (
  <NumberFormat
    {...other}
    getInputRef={inputRef}
    onValueChange={(_, e) => onChange(e)}
    suffix="%"
  />
);

You pass the events object to onChange. Just in case anyone needed more clarification.

export default ({ inputRef, onChange, ...other }) => (
  <NumberFormat
    {...other}
    getInputRef={inputRef}
    onValueChange={(_, e) => onChange(e)}
    suffix="%"
  />
);

Hi man, I tried using this Field component:

function MyNumberFormat(props) {
  const { inputRef, onChange, ...other } = props;
  return (
    <NumberFormat
    {...other}
    getInputRef={inputRef}
    onValueChange={(_, e) => onChange(e)}
    suffix="%"
    />
  );
}

...

<Field
   name="liquidation_fee"
   component={MyNumberFormat} 
   customInput={TextField}
   />

When I write in the input it returns the next error: Uncaught TypeError: onChange is not a function

PD: <Field/> is in a form decorated with redux-form
Sorry my english

You pass the events object to onChange. Just in case anyone needed more clarification.

export default ({ inputRef, onChange, ...other }) => (
  <NumberFormat
    {...other}
    getInputRef={inputRef}
    onValueChange={(_, e) => onChange(e)}
    suffix="%"
  />
);

Hi man, I tried using this Field component:

function MyNumberFormat(props) {
  const { inputRef, onChange, ...other } = props;
  return (
    <NumberFormat
    {...other}
    getInputRef={inputRef}
    onValueChange={(_, e) => onChange(e)}
    suffix="%"
    />
  );
}

...

<Field
   name="liquidation_fee"
   component={MyNumberFormat} 
   customInput={TextField}
   />

When I write in the input it returns the next error: Uncaught TypeError: onChange is not a function

PD: <Field/> is in a form decorated with redux-form
Sorry my english

I think I solved with this: const { inputRef, input: {onChange}, ...other } = props;

You pass the events object to onChange. Just in case anyone needed more clarification.

export default ({ inputRef, onChange, ...other }) => (
  <NumberFormat
    {...other}
    getInputRef={inputRef}
    onValueChange={(_, e) => onChange(e)}
    suffix="%"
  />
);

Hi man, I tried using this Field component:

function MyNumberFormat(props) {
  const { inputRef, onChange, ...other } = props;
  return (
    <NumberFormat
    {...other}
    getInputRef={inputRef}
    onValueChange={(_, e) => onChange(e)}
    suffix="%"
    />
  );
}

...

<Field
   name="liquidation_fee"
   component={MyNumberFormat} 
   customInput={TextField}
   />

When I write in the input it returns the next error: Uncaught TypeError: onChange is not a function
PD: <Field/> is in a form decorated with redux-form
Sorry my english

I think I solved with this: const { inputRef, input: {onChange}, ...other } = props;

You saved me a ton of time. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arpontes picture arpontes  路  7Comments

osvaldokalvaitir picture osvaldokalvaitir  路  3Comments

havantuan picture havantuan  路  7Comments

everlasting-neverness picture everlasting-neverness  路  5Comments

TkDodo picture TkDodo  路  5Comments