【发布时间】:2020-06-14 05:00:22
【问题描述】:
如下所示,我从 <Todo/> 父组件调用 isChecked 属性。我需要根据用户的点击来更改它的值,所以我正在使用“useState”并设置一个事件。问题是我无法将它发送到名为 currentCheck 的 API,它需要再次命名为 isChecked 以便我的 API 可以识别并保存新值。
Obs:我是 React 新手,我只是在训练,所以这可能是一个愚蠢的问题,抱歉。
function Todo({ _id, text, isChecked }) {
const [currentCheck, setCurrentCheck] = useState(isChecked),
[icon, setIcon] = useState(ellipseOutline)
async function handleClick() {
if (currentCheck) {
setCurrentCheck(false)
setIcon(ellipseOutline)
return await api.put('/', { _id, currentCheck })
}
setCurrentCheck(true)
setIcon(checkmarkCircle)
return await api.put('/', { _id, currentCheck })
}
【问题讨论】:
-
可以在匿名对象中命名key。
return await api.put('/', { _id, isChecked: currentCheck })
标签: reactjs react-hooks