2021年3月30日星期二

Parsing error: Unexpected token, expected "," with nested state in react

How to update nested state properties in React ?

Here is my code :

  const [currentUser, setUser] = useState(props.serverUserData);        const updateUserAvatar =  (picture) => {      console.log('currentUser', currentUser);      console.log('picture', picture);      console.log('currentUser1', currentUser.picture);      setUser({  ...currentUser, currentUser.picture: picture });      console.log('currentUser2', currentUser);    };  

but i am having an error at currentUser.picture:

(property) currentUser: any Parsing error: Unexpected token, expected ","

14 | console.log('picture', picture); 15 |
console.log('currentUser1', currentUser.picture); 16 | setUser({ ...currentUser, currentUser.picture: picture }); 17 |
console.log('currentUser2', currentUser); 18 | };

console.log(currentUser) gave me this:

currentUser   {id: "a19cac5c-ea25-4c9c-b1d9-5d6e464869ed", name: "Nhan Nguyen", email: "nhan13574@gmail.com", publicId: "Nh1615314435848", picture: "http://res.cloudinary.com/teammateme/image/upload/v1617154009/vvaqsguilreeenhwt8we.jpg", …}  currentUser: "http://res.cloudinary.com/teammateme/image/upload/v1617154126/zzqqtreg1g5xvyyhu3wj.png"  email: "nhan13574@gmail.com"  id: "a19cac5c-ea25-4c9c-b1d9-5d6e464869ed"  isSessionValid: true  name: "Nhan Nguyen"  picture: "http://res.cloudinary.com/teammateme/image/upload/v1617154009/vvaqsguilreeenhwt8we.jpg"  publicId: "Nh1615314435848"  __proto__: Object  
https://stackoverflow.com/questions/66880213/parsing-error-unexpected-token-expected-with-nested-state-in-react March 31, 2021 at 08:52AM

没有评论:

发表评论