如果我在父组件中调用它:
private addEmployeeForm() {
return <AddEmployeeComp employees={this.state.employees} showForm={true} />;
}
然后AddEmployeeComp:
export class AddEmployeeComp extends React.Component<
{ employees: Employee[]; showForm: boolean },
{
showEmployeeForm: boolean;
}
> {
constructor(props: { employees: Employee[]; showForm: boolean }) {
super(props);
this.state = {
showEmployeeForm: this.props.showForm,
};
}
private handleClose() {
this.setState({
showEmployeeForm: false,
});
}
public render() {
return (
<>
{this.state.showEmployeeForm ? (
<div className="popup-box">
<div className="box">
<span className="close-icon" onClick={() => this.handleClose()}>
x
</span>
<div className="userNameDiv">
<label className="labelUsername" htmlFor="username">
Username
</label>
<br />
...
为什么 showForm={true} 不适用,一旦我关闭了公式 (AddEmployeeCmp) 并且我想再次打开它。 showForm 在第一次打开后总是设置为 false。我怎样才能再次将其更改为 true(从父组件)?