关闭始终呈现的滑动面板,关闭,单击 React

shut always rendered sliding panel on off click React

提问人:meWho 提问时间:11/25/2022 最后编辑:meWho 更新时间:11/28/2022 访问量:56

问:

我有一个滑动面板,我希望当它被点击时滑动关闭。我遇到的问题是,当我单击图标将面板滑出时,它总是呈现在屏幕上,它将我的 isPanelOpen 状态设置为 false,因此它永远不会打开。我可以在此代码中更改哪些内容来解决此问题?

  handleClickOutsidePanel = ({ target }) => {
    while (this.isPanelOpen) {
      if (this.panelRef.current && !this.panelRef.current.contains(target)) {
        console.log('outside')
        this.setState({
          isPanelOpen: false
        })
      } else {
        console.log('inside')
      }
    }
  }

  componentDidMount () {
    document.addEventListener('mousedown', this.handleClickOutsidePanel)
  }

  componentWillUnmount () {
    document.removeEventListener('mousedown', this.handleClickOutsidePanel)
  }
javascript reactjs 这个 use-ref

评论


答: 暂无答案