【发布时间】:2019-08-09 10:29:55
【问题描述】:
如果您只单击打开/关闭下拉菜单,我当前的实现工作正常。但是,一旦您通过滚动关闭菜单,一旦您再次打开下拉菜单,该菜单就不会显示,或者在您每次打开它时都会显示。
我的移动导航栏中有同样的 CSSTransition 组件,它运行良好,所以这里唯一的外部因素是我拥有的 <Dropdown> 组件。
有谁知道我如何在不做任何多余工作的情况下解决这个问题?否则我想我将不得不放弃下拉组件并实现我自己的显示/隐藏系统。
import React from 'react';
import { Link } from 'react-router-dom';
import { CSSTransition } from 'react-transition-group';
import Dropdown, {DropdownTrigger, DropdownContent} from 'react-simple-dropdown';
class Nav extends React.Component {
constructor() {
super();
this.state = { show: false, }
this.showDropdown = this.showDropdown.bind(this);
this.closeDropdown = this.closeDropdown.bind(this);
}
showDropdown() {
this.setState({ active: true }, () => {
document.addEventListener('click', this.closeDropdown);
window.addEventListener('scroll', this.closeDropdown);
});
}
closeDropdown() {
this.setState({ active: false }, () => {
document.removeEventListener('click', this.closeDropdown);
window.removeEventListener('scroll', this.closeDropdown);
});
}
render() {
return(
...
<Dropdown className="...">
<DropdownTrigger onClick={this.showDropdown} >
Dropdown {this.state.active
? <img /> //up arrow
: <img /> //down arrow
</DropdownTrigger>
<DropdownContent className="...">
<CSSTransition
in={this.state.active}
timeout={150}
unmountOnExit
>
<ul>
<li><Link ... ></Link></li>
<li><Link ... ></Link></li>
</ul>
</CSSTransition>
</DropdownContent>
</Dropdown>
);
}
}
为了清楚起见,我的下拉图标与该州完美配合。唯一的问题是下拉内容。滚动关闭下拉菜单并再次单击下拉菜单后,图标会发生变化,但不会出现菜单。
【问题讨论】:
标签: javascript reactjs css-transitions listener dropdown