Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
Pass another parameter to NumberFormatCustom(props) React

I have the following function called NumberFormatCustom.

NumberFormatCustom :

function NumberFormatCustom(props) {
const { inputRef, onChange, ...other } = props;

return (
    <NumberFormat
        {...other}
        getInputRef={inputRef}
        onValueChange={values => {
            onChange({
                target: {
                    name: props.name,
                    value: values.value
                }
            });
        }}
        thousandSeparator=","
        allowNegative={false}
        decimalSeparator="."
        decimalScale={2}
        maxLength={12}
        fixedDecimalScale={true}
        allowEmptyFormatting={i}
    //prefix="S/"
    //mask="_" //nunca los entendí
    //format="##.###" //nunca los entendí
    // isNumericString
    />
 );
}

That I have implemented like,

 <TextField
    InputProps={{
       inputComponent: NumberFormatCustom,
       className: classes.finput
    }} />

but i want to send another parameter to use inside function, for example in property allowEmptyFormatting, but i don't know how to do it. can someone please help me with this.

this does not work with the current NumberFormatCustom function:

 <TextField InputProps={{ inputComponent: NumberFormatCustom(null,'another parameter'),
                      className: classes.finput }}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can define a function, which receives a bunch of parameters and returns a component, then you can pass any arguments to the function, like this:

const createNumberFormatCustom = (...params)=> (props) => {
 // params will be available in the body of your component
 const { inputRef, onChange, ...other } = props;
 
 return (
   <NumberFormat
      ...
   />
 );}

and use it like this:

<TextField InputProps={{ inputComponent: createNumberFormatCustom('another parameter'),
                      className: classes.finput }}

to solve the losing focus problem which you mentioned in the comment, there are some solutions:

1- if you want to pass some additional params just one time you can call createNumberFormatCustom outside of your component and pass the returned component as inputComponent, like this:

const MyComp = createNumberFormatCustom('custom params'); // don't call it inside the body of component, because whenever component rerenders, it makes a new component
<TextField
  ...
InputProps={{
    inputComponent: MyComp,
 }}
/>

You can learn more about this trick Here: Higher-Order Components

2- if you want to read some values inside the component and pass it to createNumberFormatCustom but again for the just first render you can use React.useMemo or React.useRef, like this:

const MyComp = React.useMemo(()=> createNumberFormatCustom('customparams'), []);
<TextField
    ...
    InputProps={{
     inputComponent: MyComp,
    }}
 />

or:

const MyComp = React.useRef(createNumberFormatCustom('custom params'))
<TextField
  InputProps={{
    inputComponent: MyComp.current,
  }}
/>

3- if you want to dynamically send some parameters to NumberFormatCustom,the better solution is to use materialUI approach to send attributes to inputComponent, like this:

<TextField
  InputProps={{
   inputComponent: NumberFormatCustom,
   inputProps: { param1: 'value1', param2:'value2' }
  }}
/>    

and inside NumberFormatCustom read custom params like this:

function NumberFormatCustom(props) {
  const { param1, param2, ...other } = props;
  console.log({param1, param2})

  return (
   <NumberFormat .../>
 );
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!