I have user state with object:
{
'login': 'somelogin',
'pass': 'somepass'
}
Using this data, I send a request and receive a response in the form of several keys
const res = await post("/api/login", user);
Response comes an object:
{
'xsrf': 'qweqweqwe',
'session': 'asdasdasd'
}
How do i merge the current state with the response so that in the user state it would be like this:
{
'login': 'somelogin',
'pass': 'somepass',
'xsrf': 'qweqweqwe',
'session': 'asdasdasd'
}
I can't think of what to write in the state setUser
something like: setUser(user res.data)
I don't know much about javascript and can't figure it out
CodePudding user response:
You can merge multi object with below expression.
const a = {'a': 1};
const b = {'b': 2};
const c = {...a, ...b}; // {'a': 1, 'b': 2}
In your case:
const user = {
'login': 'somelogin',
'pass': 'somepass'
};
const response = {
'xsrf': 'qweqweqwe',
'session': 'asdasdasd'
};
const result = {...user, ...response};