score:1
Accepted answer
Please find the updated code that helps to solve your issue:
import ReactDOM from "react-dom";
import "./styles.css";
import React, { Component } from "react";
class OptionsMenu extends Component {
constructor() {
super();
this.dropdownBoxRef = React.createRef();
}
handleClickOutside = event => {
if (this.dropdownBoxRef && !this.dropdownBoxRef.current.contains(event.target)) {
this.props.close();
}
};
componentDidMount() {
document.addEventListener("mousedown", this.handleClickOutside);
}
componentWillUnmount() {
document.removeEventListener("mousedown", this.handleClickOutside);
}
render() {
const options = ["a", "b", "c"].map(option => (
<li className="OptionsList-Element">
<div className="OptionsList-ElementIcon">Icon</div>
<span>{option.label}</span>
</li>
));
return (
<div ref={this.dropdownBoxRef} styles={this.props.styles}>
<ul className="OptionsList">{options}</ul>
</div>
);
}
}
export default OptionsMenu;
const rootElement = document.getElementById("root");
ReactDOM.render(<OptionsMenu />, rootElement);
Note: this.dropdownBoxRef.current.contains(event.target) line is the game changer.
score:0
Refs have things stored inside a property called current
. So in order to actually reach your element you'll have to do this.dropdownBoxRef.current
and call .contains
on that.
Source: stackoverflow.com
Related Query
- How to make ref to component and listen for click outside?
- How to listen for click events that are outside of a component
- react Portal - how to close modal when clicking outside of modal and how to assign ref to modal inside parent component rendering modal?
- Handle click outside of React component and how to add animation to it?
- How do I make a reusable component for the Cards and how can I use the new makeStyles of material-ui?
- How do I make a class component method accessible outside of the component in React and Typescript?
- How to center a component in MUI and make it responsive?
- React & Jest, how to test changing state and checking for another component
- how to access vairables outside of map function in js and jsx in a React component
- how to make webpack typescript react webpack-dev-server configuration for auto building and reloading page
- How to conditionally render a DIV and REACT component for OPENING and CLOSING tags?
- How to prevent outside CSS from adding and overriding ReactJS component styles
- How to click an image and make a rotation
- In React, using TypeScript, how do I pass a ref to a custom component using RefForwardingComponent and forwardRef?
- How to make an AppBar Component fill all div's width and 10% of its height?
- How to Make React.js component listen to a store in Reflux
- How to make an electron react app, which has 2 windows - a general one and one for the Tray
- Listen for changes in Formik field using ref and useEffect
- How to make a React component look more readable and clean from my example?
- How to make React component wait for redux store variables to get set?
- How can we write a click handler for a Stateless Function Component in React JS
- How to make React.js fetch data from api as state and pass this state data from its parent to child component
- React How to return div and variable value for another component
- How to make my component re render after updating props from selector in react and redux-saga?
- How do I make a client-side only component for GatsbyJS?
- How to make an http call in parent component to setState and then send the state to all child components?
- How to make react data grid table cell editable and focused on click of a button without performing a double click action
- How Do I Pass a Function Through A React Component For Data Sharing Between Parent and Child?
- How do I call a function on button click to listen for another mouse click in React Hooks?
- Formik & Yup - How to make a schema for an array and string
More Query from same tag
- How to redirect to correct client route after social auth with Passport (react, react-router, express, passport)
- React - ComponentDidMount not getting value from Redux state
- State update in event listener react hooks
- Component not re-rendering when context is updated
- How to specify text color in React using Material Design
- How to extract props and states from one sibling component to another with react hooks?
- How apply styles in React Shadow Root?
- Form data not being received properly in Node.js backend
- Multiple Modals with React Hooks
- Loading component not rendering in React.js form component?
- Err: Getting an error on implementing react animation.timing method
- Summing up prices of user's cart from 2 arrays
- Code is ginving suprising output in react. fucntion is not getting called after submit button
- Cannot create property 'key' on string
- Why is setState printing out the final state value only in the functional setState?
- FlatList calling twice
- Material UI: How to access a child component function?
- CSS Modules vs. normal CSS to change background of page in React
- Lodash Filter Array and Get Parent Index
- Reactjs: How to pass name, value to Material ui autocomplete?
- Jest, testing object contains correct function type
- Module not found: Can't resolve '../images/bg-header-desktop.svg' when importing image into React project
- react-vis sankey captions and legend
- Uncaught TypeError: Cannot read property 'history' of undefined
- How to re-render react functional component when a promise is resolved
- react typescript 'current' is not present in type 'never'
- How to render components side by side
- Problems setting up custom FB Signup with Meteor and React
- How to replace one child react/html component with another based up on event using react?
- React hooks useState not updating with onChange