2

I'm doing a React + Electron application and I'm getting this error:

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. 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

But my class is already a function component (I used this post as reference Invalid hook call. Hooks can only be called inside of the body of a function component):

import React, {useState} from 'react'
import {  HashRouter, Route, Link } from 'react-router-dom';
import { AppBar } from '@material-ui/core';

import Login from './loginView/Login.jsx';
import vendaView from './vendaView/TelaDeVenda.jsx';
import relatorioView from './relatorioView/Relatorio.jsx';
import estoqueView from './estoqueView/Estoque.jsx';
import configuracoesView from './configuracoesView/Configuracoes.jsx'
import cargosView from './cargosView/Cargos.jsx';
import historicoView from './historicoView/HistoricoDeVendas.jsx';

const Index = () => {
    const [esta_logado, setLogado] = useState(0);
    const [usuario, setUsuario] = useState({});


    function liberarLogin(usuario) {
        setLogado(1);
        setUsuario(usuario)
    }

    function deslogar(usuario) {
        setLogado(0);
        setUsuario(usuario)
    }

        return (
            <div>
                {
                    !esta_logado ?  
                        (<Login liberarLogin = {liberarLogin} />) :
                           (<AppBar position="static">
                                <HashRouter>
                                    <Link to={'/vendaView'}>Caixa</Link> <br/>
                                    <Link to={'/relatorioView'}>Relatorio</Link> <br/>
                                    <Link to={'/estoqueView'}>Estoque</Link> <br/>
                                    <Link to={'/configuracoesView'}>Configuracoes</Link> <br/>
                                    <Link to={'/cargosView'}>Cargos</Link> <br/>
                                    <Link to={'/historicoView'}>Histórico de Vendas</Link> <br/>
                                    <button onClick={deslogar}>Sair</button>

                                    <hr></hr>
                                    <Route path='/vendaView' component={vendaView}/>
                                    <Route path='/relatorioView' component={relatorioView}/>
                                    <Route path='/estoqueView' component={estoqueView}/>
                                    <Route path='/configuracoesView' component={configuracoesView}/>
                                    <Route path='/cargosView' component={cargosView}/>
                                    <Route path='/historicoView' component={historicoView}/>
                                </HashRouter> 
                            </AppBar>)
                 }     
            </div>             
        )

};

export default Index;

When I delete <AppBar position="static"> and </AppBar> the error stops... What am I doing wrong? ERROR MESSAGE

Magrini Bruna
  • 81
  • 1
  • 7
  • Look at the stack trace of the error and figure out the hook call it's complaining about. Unless you've done `Index()` instead of `` somewhere, this is not where the problem is located. – Patrick Roberts Apr 10 '20 at 02:25
  • It's true. But it doesn't show where the error in my code is, it just says " in WithStyles(ForwardRef(AppBar)) (created by Index) in Index ". I just update the post with the print of the error message – Magrini Bruna Apr 10 '20 at 02:28
  • Have you eliminated the possibility of a version mismatch between react and react-dom, or of @material-ui/core using a different version of react than the rest of your application? – Patrick Roberts Apr 10 '20 at 02:36
  • I think it's all good. Material-ui/core: "@material-ui/core": "^4.0.0-rc.0" React: "react": "^16.13.1" And It's all ok by https://material-ui.com/pt/guides/migration-v3/ – Magrini Bruna Apr 10 '20 at 02:40
  • The source tagged `4.0.0-rc.0` on github does not align with the line numbers in your stack trace. Can you confirm what version of @material-ui/core is actually installed? `^4.0.0-rc.0` isn't a version, it's a semver range. – Patrick Roberts Apr 10 '20 at 02:54
  • Sorry. "@material-ui/core": "^4.0.0" – Magrini Bruna Apr 10 '20 at 02:58
  • Again, `^4.0.0` is a [semver range](https://devhints.io/semver). This is not the version installed. You'll need to inspect the output of `npm ls` or read the `package.json` in `node_modules/@material-ui/core` of your project. – Patrick Roberts Apr 10 '20 at 03:03
  • is it "_from": "@material-ui/core@^4.0.0", "_id": "@material-ui/core@4.9.9",? – Magrini Bruna Apr 10 '20 at 03:09
  • It appears 4.9.9 is the version you have installed, thank you that is helpful. – Patrick Roberts Apr 10 '20 at 03:09
  • I solved it. If you want to know how, I answer the question. Thanks A LOT for your time! – Magrini Bruna Apr 10 '20 at 03:13

1 Answers1

0

Solved! I change the signature from const Index = () => to export default function Index() and delete export default Index;

That was enought!

Magrini Bruna
  • 81
  • 1
  • 7
  • Interesting... I'm not convinced that was the real problem, personally. What happens if you change it to `export default () =>` instead? Does it error again? – Patrick Roberts Apr 10 '20 at 03:19
  • 1
    The error message should really add this possible cause if it is a possible cause. It's a miracle that you guessed it. In any event, this didn't solve the problem for me. – Dan Cancro Jun 08 '20 at 17:08