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]})
}

猜你喜欢

转载自www.cnblogs.com/Nyan-Workflow-FC/p/11230813.html