0

I have multiple fields on different pages all controlled from a single page and using material ui textfield. I have to set different maxLength for different fields.

I found inputProps={{maxLength: 10}} , but this allows only 1 fixed value, I need a way to set different values depending on requirement.

1
  • Please provide enough code so others can better understand or reproduce the problem. Commented Dec 20, 2021 at 9:11

1 Answer 1

1

Make your maxLength a state, so you can conditionally set it:

const [dynamicLength, setDynamicLength] = useState(10);
<TextField 
    inputProps={{
       maxLength: dynamicLength
    }}
/>

Check code on sandbox: https://codesandbox.io/s/textfield-h95ti

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

2 Comments

not sure it needs a state. A variable might be enough...
Yes, a variable is enough and worked for me. Thanks for the contribution @Diogo M.F.

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.