Ad

When I Use Value In Input , I Can't Type A Text In Input

- 1 answer

When i want use value={tempLogin.login} i can't type in my input . What did i do wrong ? I don't see mistakes in event,object and state :(

var loginInfo = {
    login: "",
    password: ""
};
const [tempLogin, addTempLogin] = React.useState(loginInfo);
const [logged, setLogged] = React.useState(0);
  const newLogin = event => {
    loginInfo.login = event.target.value;
    addTempLogin(loginInfo);
    console.log(tempLogin.login);
  };
return ....
          <TextField
            placeholder="Wpisz swoją nazwę użytkownika"
            type="text"
            id="name"
            onChange={newLogin}
          />
.....
Ad

Answer

Don't use var in React.

tempLogin is always re-initialized for every re-render to

{
    login: "",
    password: ""
}

Thus, tempLogin.login is always "".

Instead, have your useState() as:

React.useState({
    login: "",
    password: ""
});

or pass loginInfo as prop.

Ad
source: stackoverflow.com
Ad