【发布时间】:2020-10-15 18:39:51
【问题描述】:
尝试在一个对象中组织应用程序挂钩:
最小、完整、可验证的示例
而不是这个:
export default function App() {
const [count1, setCount1] = useState(0)
const [count2, setCount2] = useState(0)
// Usage:
setCount1(17)
console.log(count1)
//-> 17
...这个:
export default function App() {
let hook = {
"count1": {
"set": "",
"n": 0
},
"count2": {
"set": "",
"n": 0
}
}
}
const [hook.count1.n, hook.count1.set] = useState(0)
const [hook.count2.n, hook.count2.set] = useState(0)
也尝试过这种方式,但没有运气:
const [hook.count1['n'], hook.count1['set'] = useState(0)
const [hook.count2['n'], hook.count2['set'] = useState(0)
打算这样使用:
// Usage:
hook.count2.set(17)
console.log(hook.count2.n)
// Expected 17, but...
运气不好 :( 抛出此错误:
意外的令牌,应为“,”(16,13) (
hook和count1之间的“.”)
为什么我不能将钩子状态和设置器聚合为对象属性和方法?泰基思:^)
【问题讨论】:
标签: reactjs object react-hooks