1

I have two inputs and want to convert the values, like area convertion.

let [input, setInput] = useState('');
let [inputs, setInputs] = useState('');



<div className="area_container">
     <input name="area_p" type="number" onChange={(e)=>{ setInput(e.target.value * 3.31) }} onFocus={() => {setInput('')}} value={inputs}/>
     <p>area</p>
     <input name="area_m" type="number" onChange={(e)=>{ setInputs(e.target.value / 3.31)}}  onFocus={() => {setInputs('')}} value={ input }/>
     <p>m<sup>2</sup></p>
</div>


// 1 area = 3.31 m2

So I want to use both inputs, but It's not working...

If I put the value in first input, the second input have a value which is converted automatically. And the opposite case is same, I hope.

How can I modify it? Please let me know...😥

Actually onFocus function is for "make input state value initially" but it's not working too.

1 Answer 1

0

you need to assignment tow values

<input
  onChange={(e) => {
  setInput(e.target.value * 3.31)
  setInputs(e.target.value);
  }}
  value={inputs}
/>

<input
  onChange={(e) => {
  setInput(e.target.value);
  setInputs(e.target.value / 3.31);
  }}
  value={input}
/>

Codesandbox

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.