Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusable Dropdown with React Hooks

As I got from React documentation using hooks do not require big code refactoring and can be easily included in the existing code. I want to make reusable dropdowns, being called from react component render method. Here is my code:

//navigation.js
import {DropdownToggler} from './dropdown.js';
export class ErrorTriangle extends Component {
   render(){
     return(
        {DropdownToggler({uiElement: {
            toggler: <img src={MyImg}/>,
            field: <div className={'errorMessage'}>
                       <p>Dropdown content</p>
                   </div >
        }})
       }
     )
   }
}

//dropdown.js
import React, {useEffect, useRef, useState} from "react";
import {store} from "../api/redux/store";

const DropdownToggler = (props) => {
    const myRef = useRef(null);
    const [showField, setShow] = useState(false);
    const isMobile = store.getState().isMobile;

   const remove = (e) => {
    e.stopPropagation();

    if (myRef && myRef.current.contains(e.target)) {
        return
    }
    setShow(false);
    if (isMobile===true) {
        document.removeEventListener('touchend', remove, false)
    } else {
        document.removeEventListener('mouseup', remove, false)
    }
};

const show = (e) => {
    e.stopPropagation();
    if (showField) {
        return
    }
    setShow(true);
    if (isMobile===true) {
        document.addEventListener('touchend', remove, false)
    } else {
        document.addEventListener('mouseup', remove, false)
    }
};

return (
    <>
        {React.cloneElement(props.uiElement.toggler, {
            onTouchEnd: (e) => show(e),
            onMouseUp: isMobile ? null : (e) => show(e)
        })}

        {showField ? React.cloneElement(props.uiElement.field, {ref: node => myRef.current= node}) : null}
    </>
)
};
export {DropdownToggler};

And get the error: Hooks can only be called inside the body of a function component. I have already checked existing information about this issue:

  1. react-hot-loader version is 4.8.0
  2. My react and react-dom are same version 16.8.4;
  3. My babel-config contains:

    'use strict';
    
    module.exports = {
       presets: ['@babel/preset-env', '@babel/preset-react'],
       plugins: ["@babel/plugin-proposal-class-properties", "jsx-control-statements", "react-hot-loader/babel"],
    };
    

Maybe the problem is that I am breaking the rules of hooks https://reactjs.org/warnings/invalid-hook-call-warning.html. Will appreciate detailed explanations (examples) or ideas why this is not working.

like image 695
IP_ Avatar asked May 26 '26 10:05

IP_


1 Answers

Change

export class ErrorTriangle extends Component {
   render(){
     return(
        {DropdownToggler({uiElement: {
            toggler: <img src={MyImg}/>,
            field: <div className={'errorMessage'}>
                       <p>Dropdown content</p>
                   </div >
        }})
       }
     )
   }
}

To

export function ErrorTriangle (props) {
     return(
        {DropdownToggler({uiElement: {
            toggler: <img src={MyImg}/>,
            field: <div className={'errorMessage'}>
                       <p>Dropdown content</p>
                   </div >
        }})
       }
     )
}

OR

Change

{DropdownToggler({uiElement: {
            toggler: <img src={MyImg}/>,
            field: <div className={'errorMessage'}>
                       <p>Dropdown content</p>
                   </div >
        }})
       }

To

{<DropdownToggler uiElement={{
    toggler: <img src={MyImg}/>,
    field: <div className={'errorMessage'}>
                <p>Dropdown content</p>
            </div >
  }}/>
}

Explanation

To put it simply if you are using class you should call your component function like this <Component /> and not like a normal function Component() or you can change from using class to using component function

like image 88
evgeni fotia Avatar answered May 27 '26 22:05

evgeni fotia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!