There are two ways:
- Set the type as a Record
type ResponseType = Record<string, boolean>;
const Component: React.FC = (): JSX.Element => {
const [responseStatus, setResponseStatus] = React.useState<ResponseType>({
emptyResponse: true,
unsuccessfulResponse: false,
successfulResponse: false,
badResponse: false
});
return (
<div>{responseStatus.emptyResponse}</div>
)
}
- Make an interface for the state
interface ResponseStatus {
[key: string]: boolean;
}
const Component: React.FC = (): JSX.Element => {
const [responseStatus, setResponseStatus] = React.useState<ResponseStatus>({
emptyResponse: true,
unsuccessfulResponse: false,
successfulResponse: false,
badResponse: false
});
return (
<div>{responseStatus.emptyResponse}</div>
)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…