OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

I define a subcomponent with an input box in another component. Why does the subcomponent lose focus when inputting?

  • Thread starter Thread starter Merry Ye
  • Start date Start date
M

Merry Ye

Guest
I define a child component within a parent component and use JSX to render it. However, the input box in the child component loses focus when typing. Why does this happen?

Code:
import { useState } from 'react';

function App () {
  const [text, setText] = useState('');
  const handleChange = (event) => {
    setText(event.target.value);
  };
  const Input = () => (<input value={text} onChange={handleChange} />)
  return (
    <div className="App">
      <Input />
    </div>
  );
}

export default App;

I want to know what causes this phenomenon.

<p>I define a child component within a parent component and use JSX to render it. However, the input box in the child component loses focus when typing. Why does this happen?</p>
<pre><code>import { useState } from 'react';

function App () {
const [text, setText] = useState('');
const handleChange = (event) => {
setText(event.target.value);
};
const Input = () => (<input value={text} onChange={handleChange} />)
return (
<div className="App">
<Input />
</div>
);
}

export default App;

</code></pre>
<p>I want to know what causes this phenomenon.</p>
 
Top