reactjs - React(Reakit):切换复选框时如何要求确认?
问题描述
我想知道,是否有办法通过Reakit 的 checkbox进行确认。我正在使用Reakit
,因为我找到了一种让它读取数据库布尔信息的快速方法,但我也欢迎其他方法!
我习惯于使用带有async
和的按钮进行确认window.confirm
:
<button onClick={async hiStackOverflow => {
if (window.confirm("Want to do this?")) {
// saving to database here
}
}}>
但我不知道如何使用复选框来做到这一点。简而言之,当用户打开/关闭复选框时,我希望页面确认(然后保存到数据库)。
// personData = database table, with boolean "recurring"
// row = which entity in a table we are talking about
function CheckboxThing ({ row, personData }) {
const checkbox = useCheckboxState({state: personData[row].recurring});
return (
<div className="checkbox-admin-other">
<Checkbox
{...checkbox}
// what here?? onClick or something?
/>
</div>
);
}
解决方案
Reakit 的复选框可以这样使用:
const toggle = () => setChecked(!checked);
return <Checkbox checked={checked} onChange={toggle} />;
这意味着如果需要将 React 组件状态的变量 ' checked ' 为真,则复选框将被选中,并且当用户切换复选框时,将调用名为 ' toggle ' 的方法。在该方法中,您可以输入将显示确认提示的代码,然后如果用户单击“是”则更改检查,或者如果他们检查“否”则保持原样。
推荐阅读
- symfony - 为 API Platform Bundle 中的布尔过滤器设置标准过滤器值
- python - 高斯混合模型的代码是否正确?
- node.js - 在 mongodb 文档中更新不起作用
- typescript - 使用 XLSX 时,Buffer.isBuffer 不是 Angular 6.1.10 中的函数
- android - AndroidStudio 找不到布局
- linux - 如何使用 ssh 将远程服务器变量存储到本地服务器变量?
- node.js - 使用nodejs为支付宝API生成数字签名
- python - google.api_core.exceptions.PermissionDenied 但服务帐户具有所需的权限
- amazon-web-services - 无法将请求正文解析为 json:数值中有意外字符
- javascript - 从 ajax javascript 创建的表单但操作不起作用