score:3

Accepted answer

don't use async/await and .then() together. use either of those.

const login = async () => {
    const response = await axios.get('/api');
    const parseddata = json.parse(response.data.resp);
    const userdata = parseddata.data;

    setloginresponse({
        token: userdata.token,
        userid: userdata.user.id,
        username: userdata.user.username,
        firstname: userdata.user.firstname,
        rccode: userdata.user.attributes.rccode
    });
}

score:0

in the .then

setloginresponse({...loginresponse, token: response.data.resp.data.token, userid: response.data.resp.data.user.id, ...}

you can destructure your response object first and store values in variables to make the setloginresponse more easy to read.

https://reactjs.org/docs/hooks-state.html


Related Query

More Query from same tag