我是 React 和 Typescript 的新手。在 AlertDismissable 类中,我正在设置请求完成时显示的属性。我已经使用了这个示例并对其进行了一些更改。
根据响应,我更改 AlertDismissable 的内容和样式。
当用户单击隐藏按钮时,我尝试将其显示属性设置为 false。我已将状态与属性绑定在一起,这就是我尝试设置 props 的原因。但是,编译器会抛出异常
TS2540:无法分配给“show”,因为它是常量或只读属性。(handleDismiss 方法)
看来通用道具默认是只读的。还有其他方法可以使其工作吗?
这是我的 AlertDismissable tsx
import * as React from 'react'
import { Alert, Button } from 'react-bootstrap';
interface AlertDismissableState {
show: boolean;
style: string;
}
export default class AlertDismissable extends React.Component<AlertDismissableState, AlertDismissableState> {
constructor(props: any, context: any) {
super(props, context);
this.handleDismiss = this.handleDismiss.bind(this);
this.handleShow = this.handleShow.bind(this);
this.state = {
show: this.props.show,
style: this.props.style
};
}
handleDismiss() {
this.props.show=false;
}
handleShow() {
this.props.show=true;
}
render() …Run Code Online (Sandbox Code Playgroud)