React hooks: How do I update state on a nested object with useState()?

前端 未结 6 814
一个人的身影
一个人的身影 2021-02-08 13:43

I have a component that receives a prop that looks like this:

const styles = {
    font: {
        size: {
            value: \'22\',
            unit: \'px\'
           


        
6条回答
  •  太阳男子
    2021-02-08 14:01

    If you have multiple values in a nested object, try this method below:

    setPost({
      ...post,
      postDetails: {
        ...post.postDetails,
        [event.target.name]: event.target.value,
      },
    });
    

提交回复
热议问题