Home > Blockchain >  React input value is not changing
React input value is not changing

Time:10-17

My input Tag is here:

<Input
   value={isNaN(currentCount) ? '' : currentCount}
   onKeyUp={(e) => {
      onKeyup(e);
      if (e.key === 'Enter') {}
   }}
   onChange={(e) => {
      handleChange(e);
   }}
/>

onKeyUp is working, but onChange is not working now.

I found that the value of the input tag does not change when I enter a value in the textbox.

Please let me know if you have had the same problem as me or know how to fix it.

CodePudding user response:

You should create a state and set your input value in it like below:

const [value, setValue] = useState('')

const onChange = (event) => {
   setValue(event.target.value)
}

return (
<Input
   value={value}
   onKeyUp={(e) => {
      onKeyup(e);
      if (e.key === 'Enter') {}
   }}
   onChange={handleChange}
/>
)

CodePudding user response:

const [ currentCount, setCurrentCount] = useState('');
    
const handleChange = (e) => {
   setCurrentCount( e.target.value || '');
}

return (
  <input type="text" value={ isNaN(currentCount) ? '' : currentCount } onChange={ (e) =>  handleChange(e) }/>
)
  • Related