如何在反应中“打破”功能部件的闭合?

时间:2019-09-30 10:25:11

标签: reactjs react-hooks

我正在为错误的变量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)
    }
  }

0 个答案:

没有答案