我正在尝试构建一个下拉菜单。基本结构Test是我需要使用 Reactref和styled-components.
这是代码:
import React from "react";
import PropTypes from "prop-types";
import styled, { ThemeProvider } from "styled-components";
import { theme } from "../theme";
import Icon from "../Icon";
const Container = styled.div`
display: flex;
`};
const SelectorDiv = styled.div`
background-color: black;
color: white;
height: 100px;
`;
class Test extends React.Component {
componentDidMount () {
document.addEventListener("mousedown", this.handleClickOutside);
}
componentWillUnmount () {
document.removeEventListener("mousedown", this.handleClickOutside);
}
handleClickOutside = event => {
console.log(this.refs); // undefined
}
handleClickInside = () => {
alert("Clicked inside");
}
render = () => {
return (
<ThemeProvider theme={theme}>
<Container disabled={disabled}>
<SelectorDiv
onClick={this.handleClickInside}
ref={"wrapper"}
>
<h1>This is the content to click</h1>
</SelectorDiv>
</Container>
</ThemeProvider>
);
};
}
export default Test;
ref 不是工作属性。当点击区域之外时,我正在undefined继续this.refs。发现styled-components有问题,但在 V4 上已解决(我使用的是 V5.1.0)。
如何将包装组件放入我的handleOutsideClick?
森林海
相关分类