useState object set

前端 未结 3 1872
灰色年华
灰色年华 2021-01-28 17:32

I am new to ReactJS and working with Hooks. I want to set state for object. Here is my code:

const StartPage = (props)=> {
    const [user,setUser] = useState         


        
3条回答
  •  [愿得一人]
    2021-01-28 18:17

    I'm not exactly sure what you mean by "not working" but it looks like you are deconstructing info but not data. My suspicion is that it is setting state but not the way you want. If data is an object with email as a property you most likely will want to deconstruct that too like this:

    setInfo({...info, ...data})
    

    You can use useEffect to console.log state changes if you want with something like this:

    import React, { useState, useEffect } from 'react'
    
    const StartPage = props => {
      const [info,setInfo] = useState({ email:'' })
      useEffect(() => {
        console.log(info)
      }, [info])
    

提交回复
热议问题