-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Open
Description
问题
useToggle 的 defaultValue 和 reverseValue 作为 props 传入时,如果这些值发生变化,setLeft 和 setRight 仍然使用初始值。
示例代码
import { useToggle } from 'ahooks';
import { useState } from 'react';
function App() {
const [parentValue, setParentValue] = useState('A');
return (
<div>
<button onClick={() => setParentValue(parentValue === 'A' ? 'B' : 'A')}>
切换: {parentValue}
</button>
<Child leftValue={parentValue} />
</div>
);
}
function Child({ leftValue }) {
const [value, { setLeft }] = useToggle(leftValue, 'OFF');
return (
<div>
<p>当前值: {value}</p>
<p>leftValue prop: {leftValue}</p>
<button onClick={setLeft}>setLeft</button>
</div>
);
}复现步骤
- 点击 "切换" 按钮,
parentValue从 'A' 变为 'B' - 点击子组件的 "setLeft" 按钮
- 期望:
value变为 'B' - 实际:
value仍为 'A'(使用了初始值)
问题
这是预期的行为吗?如果需要响应动态 props 变化,应该如何处理?
Metadata
Metadata
Assignees
Labels
No labels