Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
hook call error: Hooks can only be called inside of the body of a function component

I'm new to react component, getting this error when using react-hooks, here is my code, can anyone help me with that? This is the detail of the error.

This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
const newParams = "params"

// eslint-disable-next-line react-hooks/rules-of-hooks
const history = useHistory();
// eslint-disable-next-line react-hooks/rules-of-hooks
const location = useLocation();


export class FullscreenDialog extends React.Component{

    constructor(props: any) {
        super(props);

        this.state = { dialogShow: false };

        this.onDismissFullscreen = this.onDismissFullscreen.bind(this);
        this.openDialogWithButton = this.openDialogWithButton.bind(this);
    }

    openDialogWithButton() {
        updateSearch({history, location, newParams})
    }

    onDismissFullscreen() {

        closeDialog({ history, location, key: 'key' })
    }

    render(){
        const uniqueDialogId3 = 'notes';

        return (
            <>
                <DialogRoute id={uniqueDialogId3}>
                    <Layer id="fullscreenDialog" >
                        <UitkFullscreenDialog ariaLabel="Demo" dialogShow={true} returnFocusOnClose={true}>
                            <UitkToolbar
                                header="Toolbar heading"
                                iconLabel="Close the dialog"
                                key="UitkToolbar"
                                type={ToolbarType.CLOSE}
                            />
                            <UitkDialogContent key="UitkDialogContent-1">
                                <UitkParagraph key="UitkDialogContentParagraph" size={2}>
                                    test
                                </UitkParagraph>
                            </UitkDialogContent>
                        </UitkFullscreenDialog>
                    </Layer>
                </DialogRoute>
                <UitkLink inline={true}>
                    <button onClick={this.openDialogWithButton}>Open Fullscreen Dialog</button>
                </UitkLink>
            </>
        );
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A general rule of thumb is anything starting with "use" is going to be a hook and will not work in a class based component. You can Wrap your component in withRouter from react router dom to access the history prop that way if you want to stick with a class based component, check that out here. Otherwise you can switch to a functional component which would look like this:

import React, {useState} from 'react';
const newParams = "params"

// eslint-disable-next-line react-hooks/rules-of-hooks
const history = useHistory();
// eslint-disable-next-line react-hooks/rules-of-hooks
const location = useLocation();


const FullscreenDialog = ({closeDialog, updateSearch}) => {
const [dialogShow, setDialogShow] = useState(false)

const openDialogWithButton = () => {
    updateSearch({history, location, newParams})
}

const onDismissFullscreen = () => {
    closeDialog({ history, location, key: 'key' })
}

const uniqueDialogId3 = 'notes';

return (
    <>
    <DialogRoute id={uniqueDialogId3}>
        <Layer id="fullscreenDialog" >
            <UitkFullscreenDialog ariaLabel="Demo" dialogShow={true} returnFocusOnClose={true}>
                <UitkToolbar
                    header="Toolbar heading"
                    iconLabel="Close the dialog"
                    key="UitkToolbar"
                    type={ToolbarType.CLOSE}
                />
                <UitkDialogContent key="UitkDialogContent-1">
                    <UitkParagraph key="UitkDialogContentParagraph" size={2}>
                        test
                    </UitkParagraph>
                </UitkDialogContent>
            </UitkFullscreenDialog>
        </Layer>
    </DialogRoute>
    <UitkLink inline={true}>
        <button onClick={openDialogWithButton}>Open Fullscreen Dialog</button>
    </UitkLink>
</>
)
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!