React Hooks : UseRef not recognizing Event from Material UI Select

Dy4

I'm using React Hooks.I am trying to trigger a onclick event using useRef.


    const component1: React.FC<Props> = props {
      const node =useRef<HTMLDivElement>(null);

      const ClickListener = UseCallback((e:any)=>{
        if(node.current.contains(e.target))
         console.log("Event Contained");
        else console.log("Event not Contained");
      },[]);

      return (
        <div ref={node} onClick={ClickListener}>
         <FormControl>
           <InputLabel>{text}</InputLabel>
           <Select> {contents} </Select>
         </FormControl>
        </div>
      );
    };

I have this Component1 called by componentX and componentY.

The above code is failing to recognise onclick event on a componentX's node when componentY's select menu is opened.

Only after closing ComponentY's select menu and clicking again on ComponentX currently recognises the event. Any Suggestions on why itsn't recognized.I am using Material UI's select.

ckedar

Use callback ref.

Quoting from API doc:

Keep in mind that useRef doesn’t notify you when its content changes. Mutating the .current property doesn’t cause a re-render. If you want to run some code when React attaches or detaches a ref to a DOM node, you may want to use a callback ref instead.

Edit

Sorry to lead you in wrong direction. My suggestion to use callback ref is not correct for your code.

Your callbacks are working correctly and are able to use the div ref properly.
The reason you feel your callback is not working when Select in other component is opened is that Select uses mouse capture to detect click outside the pop-up.
So when your drop-down is open, click on other component div does not reach that div at all, it is consumed by Select. Subsequent click works well.
This is the case not only with your component but with any other element in the dom.

See: https://codesandbox.io/s/silly-star-k430x

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Set JSON object to a Const on material UI Select component React Hooks

React material ui autocomplete hooks

React Material-UI Select not detecting scroll event

React Hooks UseRef issue

Unable to Select options from select component using material ui react

setState from a React material UI MenuItem click event

Material UI select throws event.target.getAttribute is not a function when used from within react-datatable-component

How to focus on Material UI TextField using useRef hooks when a button is clicked?

Remove blue outline from Select box React Material UI

React, Leaflet - Hooks, setView, and useRef

How to use useRef with Material UI

setValue from material UI select

enzyme not simulating change event on React Material-UI v1 - Select component

How select part of text in a Textfield on onFocus event with material-UI in React?

Unsubscribe from event listener react hooks

Passing data across Material-UI Stepper using React Hooks

Material UI Nested lists using recursive render is not working with react hooks

Implementing react hooks useState in material-ui Tab container not working

Cant get event.target.value using select item from material-ui autocomplete with onchange

React Hooks: Why is .current null for useRef Hook?

Material UI react select value is not clearing on backspace

react material-ui select not working

React render siblings on Multiple Select Material UI

React Material UI Select not working properly

Material UI Select component crashing React application

react select not recognizing default value

React Material UI Select doesn't change value when selecting an item from the dropdown

React Material-Table editing from props using hooks

Enhancing from Component in React with Material-UI