错误:无效的挂钩调用。挂钩只能在功能组件的主体内部调用

时间:2019-10-29 01:29:52

标签: reactjs

我是React的新手,我正在尝试制作CRUD。 <div className="conteudo">中的表格可以正常工作。接下来,我用function App()交换了class App extends Component { render() {并编写了处理Facebook身份验证的代码。它编译良好,在终端上没有错误。

但是在浏览器中,它向我显示了(错误)著名的消息:“错误:无效的挂钩调用。只能在函数组件的主体内部调用挂钩。”

作为一个完整的菜鸟,我在做什么错了?

非常感谢!

贝勒是我的代码...

================================================ ========

import React, { useState, Component } from 'react';
import api from './services/api';
import './App.css';
import logotipo from './assets/logotipo.png';
import FacebookLogin from 'react-facebook-login';

class App extends Component  {
  render() {

    const responseFacebook = (response) => {
    }

    const [nome, setNome] = useState('');
    const [ra, setRa] = useState('');
    const [telefone, setTelefone] = useState('');
    const [email, setEmail] = useState('');
    const [senha, setSenha] = useState('');

    async function processaSubmit (event) {
      event.preventDefault();

      const response =  await api.post('/users', {
        nome: nome,
        ra: ra,
        telefone: telefone,
        email: email,
        senha: senha,
      })

      console.log(response)
    }

    return (
      <div className="contenedor">

        <img src={logotipo} alt="Um logotipo qualquer" />

        <div className="App">
        <h1>LOGIN WITH FACEBOOK AND GOOGLE</h1>

        <FacebookLogin
          appId="381163456103277" //APP ID NOT CREATED YET
          fields="name,email,picture"
          callback={responseFacebook}
        />

        </div>

        <div className="conteudo">
          <p className="big">Bem vindes ao <strong>Sistema de Textos CAPed</strong>. <br />Faça seu cadastro para aproveitar o nosso <strong>acervo</strong>.</p>

          <button className="btn" type="Submit">Usar login do Facebook</button>

          <p className="big"></p>
          <p className="big">Ou faça o cadastro <strong>manualmente</strong> preenchendo o formulário abaixo.</p>

          <form onSubmit={processaSubmit}>

            <label htmlFor="Nome">Nome</label>
            <input 
              type="txt" 
              id="nome" 
              placeholder="Nome e sobrenome, pfvr"
              value={nome}
              onChange={event => setNome(event.target.value)}
            >
            </input>

            <label htmlFor="Ra">RA</label>
            <input 
              type="txt" 
              id="ra" 
              placeholder="Seu RA"
              value={ra}
              onChange={event => setRa(event.target.value)}
            >
            </input>

            <label htmlFor="telefone">Telefone</label>
            <input 
              type="tnumber" 
              id="telefone" 
              placeholder="Podemos precisar ;-)"
              value={telefone}
              onChange={event => setTelefone(event.target.value)}
            >
            </input>

            <label htmlFor="email">Email</label>
            <input 
              type="email" 
              id="email" 
              placeholder="Digite seu melhor email"
              value={email}
              onChange={event => setEmail(event.target.value)}
            >
            </input>

            <label htmlFor="senha">Senha</label>
            <input 
              type="password" 
              id="senha" 
              placeholder="Digite uma senha bacana"
              value={senha}
              onChange={event => setSenha(event.target.value)}
            >
            </input>

            <p className="little">&nbsp;</p>

            <button className="btn" type="Submit">Fazer meu cadastro</button>

          </form>

          <p className="little"></p>
          <p className="little">Já tem cadastro mas não lembre a senha? Clique bem <strong>aqui</strong>.</p>

        </div>

      </div>
    );
  };
}

export default App;

1 个答案:

答案 0 :(得分:0)

要使用钩子,必须实现一个功能组件。以下内容将帮助您入门。本质上,您的JSX是从函数中返回的,而不是使用render方法。

import React, { useState } from 'react';
import api from './services/api';
import './App.css';
import logotipo from './assets/logotipo.png';
import FacebookLogin from 'react-facebook-login';

const App = () => {
  const responseFacebook = (response) => {}
  const [nome, setNome] = useState('');
  const [ra, setRa] = useState('');
  const [telefone, setTelefone] = useState('');
  const [email, setEmail] = useState('');
  const [senha, setSenha] = useState('');

  async function processaSubmit (event) {
    event.preventDefault();
    const response =  await api.post('/users', {
      nome: nome,
      ra: ra,
      telefone: telefone,
      email: email,
      senha: senha,
    })

    console.log(response)
  }

  return ( ... )
}

export default App;