gpt4 book ai didi

javascript - 无法在 Ant Modal 中访问 this.setState 或 this.state 确认 ok/cancel 功能

转载 作者:行者123 更新时间:2023-12-04 00:54:25 25 4
gpt4 key购买 nike

我在访问 onCancel/onOk 函数内的 this.statethis.setState 时遇到问题。我想在确认或取消弹出模态后修改状态。如果有人有不同的方法,您的指导会很有帮助。

import React from 'react';
import { Button, Modal } from 'antd';

class ExampleClass extends React.Component {
constructor() {
super();

this.state = {
bankInfo: 100,
};
}

onButtonClicked() {
this.setState({ bankInfo: 200 }); // works fine
Modal.confirm({
title: 'Are you sure delete this item?',
okType: 'danger',
onOk() {
this.setState({ bankInfo: 300 }); // Uncaught TypeError: Cannot read property 'setState' of undefined
},
onCancel() {
this.setState({ bankInfo: 400 }); // Uncaught TypeError: Cannot read property 'setState' of undefined
},
});
}

render() {
return (
<div>
<Button onClick={this.onButtonClicked.bind(this)}>Click Me</Button>
</div>
);
}
}

export default ExampleClass;

最佳答案

我更愿意使用箭头函数

import React from 'react';
import { Button, Modal } from 'antd';

class ExampleClass extends React.Component {
constructor() {
super();

this.state = {
bankInfo: 100,
};
}

onButtonClicked = () => {
this.setState({ bankInfo: 200 });
Modal.confirm({
title: 'Are you sure delete this item?',
okType: 'danger',
onOk: () => {
this.setState({ bankInfo: 300 });
},
onCancel: () => {
this.setState({ bankInfo: 400 });
},
});
}

render() {
return (
<div>
<Button onClick={this.onButtonClicked}>Click Me</Button>
</div>
);
}
}

export default ExampleClass;

关于javascript - 无法在 Ant Modal 中访问 this.setState 或 this.state 确认 ok/cancel 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63747666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com