我正在为错误的变量isTaskFieldIsOpened
设置初始状态。
然后在我的组件“ did mount”中,设置点击和调用函数的侦听器。我肯定知道,isTaskFieldIsOpened变为true,这是因为它在组件生命周期内设置在els的某个位置,但是当我单击时,我始终为false。那是发生的,因为我有一个关闭。如何“打破”它并在我的handleClickOutside?
function TaskColumn({ title, children }) {
const [newTaskField, setNewTaskField] = useState(
createRequiredField('textarea', 'text', 'Введите заголовок для этой задачи')
)
const [isTaskFieldOpened, setIsTaskFieldOpened] = useState(false)
const [taskFieldRef, setTaskFieldRef] = useState(null)
useEffect(() => {
document.addEventListener('mousedown', handleClickOutside)
return () => document.removeEventListener('mousedown', handleClickOutside)
}, [])
const handleClickOutside = event => {
if (taskFieldRef && taskFieldRef === event.target) {
console.log('hello')
} else {
console.log(isTaskFieldOpened)
console.log(event.target)
}
}