【发布时间】:2022-07-23 01:44:29
【问题描述】:
有一个称为切换的功能组件,需要添加一个补丁请求,因此每次切换触发时,更改都会在 UI 和数据库中更新(显示/隐藏)。 唯一的办法是使用hoots useState吗?
这里有一些代码供参考
const ToggleStats = ({ label, value, onChange }) => {
const toggled = !!value
const handleToggle = () => {
onChange(!toggled)
// TODO when the toggle gets clicked a request gets send to update the bd (show/hide)
api.patch(`admin/admin/institutions/id/?with_deleted=true`, { body })
.then(res => res.json())
.then(data => {
this.setState({ })
})
}
return (
<Toggle
label={label}
labelPosition='left'
labelStyle={styles.label}
iconStyle={styles.ripple}
thumbSwitchedStyle={styles.toggle}
trackSwitchedStyle={styles.toggleBackround}
onToggle={handleToggle}
/>
)
}
ToggleStats.propTypes = {
label: PropTypes.string.isRequired,
value: PropTypes.any,
onChange: PropTypes.func.isRequired
}
任何帮助将不胜感激。
【问题讨论】:
-
尝试向
handleToggle(x)函数添加参数。如果您担心使用 useState,请查看您是否处于开启/关闭状态。
标签: reactjs json api react-hooks patch