I’m trying to partially update userData
object
// Types named randomly...
export type authDetails = {
info: {
token: String,
somethingElse: String
},
moreInfo: {
something: String
}
}
...
export type userData = {
auth: authDetails,
config: configDetails,
params: paramsDetails
}
const [userData, setUserData] = useState<userData>();
setUserrData((prev: any) => ({
...prev,
auth : {
info: {
token: newToken
}
},
params: {
someParam: newParam
}
}));
Is it even possible? I can’t find any way to do that. I wanna change some data and keep other values unchanged. ...prev
didn’t work.
(new to typescript and react)
2
Answers
This is what you want to do:
You need to still spread the values of the updated properties.
Couple things I’d like to mention since you’re new to TS and React.
Usually, types should be defined in
PascalCase
and the string type is a native keyword and is defined in lowercase, so your types would look like:And about the issue that you’re facing, when you’re dealing with updating nested fields, one way to solve the issue is using spread and you must use it to fill all the nested object like the following:
Another way is cloning that value and replace directly the fields and return the name value: