Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

316
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda