我想知道,是否可以像下面的示例一样编写通用的onChange
处理程序,而不仅是将可用性状态属性作为对象。
interface Aviability
{
Arrival: Date;
Departure: Date;
Lenght: number;
Beam: number;
Draft: number;
ShorePower?: number;
PromoCode?: string;
}
然后在类构造函数中:
this.state =
{
aviability : (null!)
}
处理程序:
handleChange = (input: string) => (e: React.ChangeEvent<HTMLInputElement>) =>
{
this.setState
(
oldState =>
({
aviability: {[input]: e.target.value,
...oldState.aviability}
})
);
}