![React JS](https://res.cloudinary.com/practicaldev/image/fetch/s--3zWuwYa3--/c_imagga_scale,f_auto,fl_progressive,h_900,q_auto,w_1600/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdib9r9rk5j1m7oala1p.png)
The basic way to do this is by defining some state, and then handling the state update directly in the change event of the input control, like this:
const [loginState, setLoginState] = useState({username: ''});
...
<input type='text' id='username' onChange={ (e) => setState({...loginState, username: e.target.value }) } value={ loginState.username } />
The better way would be to define a generic handler which is more readable and simple to maintain:
const [loginState, setLoginState] = useState({username: ''});
function changeLogin(e) {
setLoginState({...loginState, [e.target.id]: e.target.value});
}
...
<input type='text' id='username' onChange={changeLogin} value={ loginState.username } />
Tags
React