admin管理员组文章数量:1388153
I'm trying to make a menu that when it is clicked it shows up. This works fine. However I want the menu to close whenever anything but the original menu or item list is selected. When I do this however it returns with the error
Uncaught TypeError: _this.dropdownMenu.includes is not a function
at HTMLDocument.eval (FeedItem.jsx:277)
Visual
Code
import React, { Component } from 'react'
import styled from 'styled-ponents';
const OptionMenu = styled.div `
position: absolute;
top: 20px;
left: -130px;
display: ${props => props.showMenu ? 'flex' : 'none'};
flex-direction: column;
width: 150px;
background: #FFFFFF;
border: 1px solid #EEEFEF;
box-shadow: 0 2px 2px 0 rgba(0,0,0,.14), 0 3px 1px -2px rgba(0,0,0,.2), 0 1px 5px 0 rgba(0,0,0,.12);
z-index: 100;
`;
const OptionItem = styled.div `
color: #414141;
font-size: 18px;
cursor: pointer;
padding: 10px 5px;
transition: .2s;
&:hover {
background-color: #F2F2F2;
}
`;
class FeedItem extends Component {
constructor() {
super();
this.state = {
showMenu: false
}
this.dropdownMenu = React.createRef();
}
showMenu = (e) => {
this.setState({ showMenu: true }, () => {
document.addEventListener('click', this.closeMenu);
});
}
closeMenu = (e) => {
if(!this.dropdownMenu.includes(e.target)) {
console.log('yes')
this.setState({ showMenu: false }, () => {
document.removeEventListener('click', this.closeMenu);
});
}
}
render(props) {
const { showMenu } = this.state;
return(
<Container item={this.props.id}>
<UserOptions onClick={() => this.showMenu()} ref={this.dropdownMenu}>
<OptionMenu showMenu={showMenu}>
<OptionItem>Share</OptionItem>
<OptionItem>Something</OptionItem>
</OptionMenu>
</UserOptions>
</Container>
);
}
}
export default FeedItem;
I'm using styled-ponents could this be an issue? Also I've delted most of the parts of the container and have just left the problem areas.
I'm trying to make a menu that when it is clicked it shows up. This works fine. However I want the menu to close whenever anything but the original menu or item list is selected. When I do this however it returns with the error
Uncaught TypeError: _this.dropdownMenu.includes is not a function
at HTMLDocument.eval (FeedItem.jsx:277)
Visual
Code
import React, { Component } from 'react'
import styled from 'styled-ponents';
const OptionMenu = styled.div `
position: absolute;
top: 20px;
left: -130px;
display: ${props => props.showMenu ? 'flex' : 'none'};
flex-direction: column;
width: 150px;
background: #FFFFFF;
border: 1px solid #EEEFEF;
box-shadow: 0 2px 2px 0 rgba(0,0,0,.14), 0 3px 1px -2px rgba(0,0,0,.2), 0 1px 5px 0 rgba(0,0,0,.12);
z-index: 100;
`;
const OptionItem = styled.div `
color: #414141;
font-size: 18px;
cursor: pointer;
padding: 10px 5px;
transition: .2s;
&:hover {
background-color: #F2F2F2;
}
`;
class FeedItem extends Component {
constructor() {
super();
this.state = {
showMenu: false
}
this.dropdownMenu = React.createRef();
}
showMenu = (e) => {
this.setState({ showMenu: true }, () => {
document.addEventListener('click', this.closeMenu);
});
}
closeMenu = (e) => {
if(!this.dropdownMenu.includes(e.target)) {
console.log('yes')
this.setState({ showMenu: false }, () => {
document.removeEventListener('click', this.closeMenu);
});
}
}
render(props) {
const { showMenu } = this.state;
return(
<Container item={this.props.id}>
<UserOptions onClick={() => this.showMenu()} ref={this.dropdownMenu}>
<OptionMenu showMenu={showMenu}>
<OptionItem>Share</OptionItem>
<OptionItem>Something</OptionItem>
</OptionMenu>
</UserOptions>
</Container>
);
}
}
export default FeedItem;
I'm using styled-ponents could this be an issue? Also I've delted most of the parts of the container and have just left the problem areas.
Share Improve this question asked Nov 1, 2018 at 21:50 cosmichero2025cosmichero2025 1,0394 gold badges16 silver badges41 bronze badges 2-
2
includes
is a String and Array method, not a Node method. Maybe you wantthis.dropdownMenu.current.contains(e.target)
? – Tholle Commented Nov 1, 2018 at 21:55 - 1 I crazy thing is I could have sworn I used that! Thank you sir! – cosmichero2025 Commented Nov 1, 2018 at 21:56
1 Answer
Reset to default 2includes
is a String
and Array
method, not a Node
method.
You can use contains
instead:
closeMenu = (e) => {
if (!this.dropdownMenu.current.contains(e.target)) {
console.log('yes')
this.setState({ showMenu: false }, () => {
document.removeEventListener('click', this.closeMenu);
});
}
}
本文标签: javascriptUncaught TypeError includes is not a function with React RefStack Overflow
版权声明:本文标题:javascript - Uncaught TypeError: .includes is not a function with React Ref - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744525870a2610743.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论