What type of event should I set the setEndpointState
method of this component to. I tried setting it to React.FormEvent<HTMLFormElement>
but then I get a type error in the setState function saying that I am missing props name
, hostname
and description
. Is the type of the event wrong or is there a way to write the setState function differently?
import * as React from "react";
import EditBasicInfoForm from "./EditBasicInfoForm";
import { Endpoint } from "../../Endpoints/model";
interface EditBasicInfoProps {
editBasicInfo: (endpoint: Endpoint) => void;
ocid: string;
}
interface EditBasicInfoState {
name: string;
hostname: string;
description: string;
}
export class EditBasicInfo extends React.Component<EditBasicInfoProps & EditBasicInfoState, EditBasicInfoState> {
constructor(props: any) {
super(props);
this.state = {
name: "",
hostname: "",
description: ""
};
this.setEndpointState = this.setEndpointState.bind(this);
this.editBasicInfo = this.editBasicInfo.bind(this);
}
public setEndpointState(e: React.FormEvent<HTMLFormElement>): void {
const target = e.currentTarget;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
}
public editBasicInfo(): void {
const endpoint: any = {
name: this.state.name,
hostname: this.state.hostname,
description: this.state.description,
ocid: this.props.ocid,
};
this.props.editBasicInfo(endpoint);
}
public render(): JSX.Element {
return (
<>
<EditBasicInfoForm
name={this.state.name}
hostname={this.state.hostname}
description={this.state.description}
handleChange={this.setEndpointState}
handleSubmit={this.editBasicInfo}
/>
</>
);
}
}
export default EditBasicInfo;