I'm new working with react hooks so I wanted a hand in a problem that is appearing me when I try to change the value of a variable. When I'm trying to set a value returned from an external API, it prints that in the data returns information that can be captured, however, using useState (setVariable) to use that value in other part, it appears in blank. Here is an example of my code:
const InicioSesion: React.FC<{}> = () => {
const [token, setToken] = useState('');
const [success, setSucess] = useState(false);
const [userLogin, setUserLogin] = useState({
'nombre' : '',
'password' : ''
});
const actualizarUser = (e: any) => {
setUserLogin({
...userLogin,
[e.target.name]: e.target.value
});
}
const sendInfo = () => {
axios.post('https://service.herokuapp.com/login', JSON.parse('{"nombre":"' + userLogin.nombre + '", "password":"' + userLogin.password + '"}') )
.then(res => {
console.log(res);
console.log(res.data.Authorization); //Here appears like I got the token as response
setToken(res.data.Authorization); // Here I'm trying to set my variable with the value obtained.
console.log(token); //Here prints as if I didn't asing any value.
handleOpen();
}).catch(error => {
console.log(error.response)
});
}
const handleOpen = () => {
setSucess(true);
}
const handleClose = () => {
setSucess(false);
}
return(
<div>
<h1>Iniciar sesión</h1>
<Container placeholder>
<Segment>
<Form>
<Form.Field>
<label>Correo</label>
<input placeholder='Ingresa tu correo' name='nombre' onChange={actualizarUser}/>
</Form.Field>
<Form.Field>
<label>Contraseña</label>
<input placeholder='Ingresa tu contraseña' name='password' onChange={actualizarUser}/>
</Form.Field>
<Link to={{ pathname:'/init/home', state:{ value: token } }}>
<Button type='submit'>SubmitNuevo</Button>
</Link>
<Button type='submit' onClick={sendInfo}>Prueba</Button>
</Form>
</Segment>
</Container>
<Modal show={success} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Registro exitoso</Modal.Title>
</Modal.Header>
<Modal.Body>
Su local se ha creado exitosamente. Ahora puede iniciar sesión.
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={handleClose}>
Cerrar
</Button>
</Modal.Footer>
</Modal>
</div>
);
Thank you for you help.