react: typescript interface useState issue


 define interface:

interface ILoginState {
  imageId: string;
  imageSrc: string;
  username: string;
  password: string;
  verifyCode: string;    
}

useState:

const [loginData, setLoginData] = useState(loginState)

update imageId && imageSrc:

setLoginData({
  ...loginData,
  imageId: res.data.id.
  imageSrc: res.data.content
})

antd inputchange:

onInputChange(e: FormEvent<HTMLInputElement>) {
  const inputName = e.currentTarget.id;
  const inputValue = e.currentTarget.value;
  setInputData({
    [inputName]: inputValue
  } as {[key in keyof ILoginState]:ILoginState[key]})
}

 antd form onsubmit

<Form onSubmit={handleSubmit.bind(this)}>

add window resizeLitener

const [contentHeight, setHeight]=useState(INTIAL_HEIGHT)
useEffect(()=>{
  screenChange();
  return () => removeListener()//回調銷毀
},[contentHeight])

const screenChange = () => {
  window.addEventListener("resize", handleResize)  
}

const removeListener = () => {
  window.removeEventListener("resize", handleResize)  
}

handleResize =(e)=> {
  setHeight(e.target.innerHeight-headerHeight-footerHeight)
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM