我正在使用状态在图标的单击事件中打开和关闭图层。当我 _onOpenLayer()
从点击处理程序调用时,没有任何反应。
以前我直接从图标 click 调用该方法onClick={this._onOpenLayer()}
,这确实打开了图层,但如果由于不允许在 rednder() 中调用方法而冻结了 UI。
所以我找到了一个在调用 open 之前添加 lambda的解决方案,如下所示。但是单击该图标不会打开具有此更改的图层:
onClick={() => this._onOpenLayer()}
为了进一步调试,我在_onOpenLayer()
方法中放置了一个 console.log ,我可以看到它没有在图标点击时被击中。
问题:
如何从 render 方法中的 click 事件中调用方法?
这是我正在渲染的课程的要点。_onOpenLayer() 将 openLayer 状态设置为 true,从而打开图层元素:
class Page1 extends Component {
constructor(props) {
super(props);
this.state = {
openLayer: false,
};
}
_onOpenLayer() {
console.log("fooo");
this.setState({
openLayer: true
});
}
_onCloseLayer() {
this.setState({
openLayer: false
});
}
render() {
let layerNode;
if (this.state.openLayer) {
layerNode = (
<Layer flush={true} closer={true} onClose={() => this._onCloseLayer()} align='right'>
</Layer>
);
}
return (
<Section pad="small" full="vertical" flex={true}>
<div>
<Box direction="row" align="center" justify="end" tag="aside" pad={{horizontal: "large" , between: "small"}} >
<Filter size="medium" colorIndex="brand" onClick={() => this._onOpenLayer()} />
</Box>
{layerNode}
</Section>
);
}
};