Reactjs,使用材料 UI:无效的钩子调用错误

时间:2021-04-18 22:52:03

标签: javascript reactjs material-ui

我对 React 非常陌生,我尝试使用 Material UI 中的组件。我使用 npm install 将它本地安装到我的项目中,然后将我想要的组件导入到我的项目中。但是当我开始运行时,我得到: 错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。这可能是由于以下原因之一造成的:

  1. 您的 React 和渲染器版本可能不匹配(例如 React DOM)
  2. 你可能违反了钩子规则
  3. 您可能在同一个应用中拥有多个 React 副本 有关如何调试和修复此问题的提示,请参阅 https://reactjs.org/link/invalid-hook-call。 ▶ 22 个堆栈帧被折叠。 模块。 src/index.js:7

我一直在研究,发现这是一个很常见的错误,很多人都有,但我已经尝试了所有提出的解决方案,但似乎没有一个有效。有人建议这可能会发生,因为我有很多 React 副本,在运行 npm ls react 后我得到了这个: terminal screenshot。虽然我有很多react的副本,但我查了一下,deduped就是de重复,所以我得出的结论是这不是问题的根源。 一旦我删除了组件,项目就会运行而没有任何问题。如果有人能解决我的问题,我将不胜感激。

import React from 'react';
import Accordion from '@material-ui/core/Accordion';
import AccordionSummary from '@material-ui/core/AccordionSummary';
import AccordionDetails from '@material-ui/core/AccordionDetails';

常见问题解答:

const Faq = (props) =>{

        return (
            <Accordion>
                <Accordion>
                    <AccordionSummary
                    
                    aria-controls="panel1a-content"
                    id="panel1a-header"
                    >
                    <p>Pregunta1</p>
                    </AccordionSummary>
                    <AccordionDetails>
                    <p>HOLAA</p>
                    </AccordionDetails>
                </Accordion>
                <Accordion>
                    <AccordionSummary
                    
                    aria-controls="panel2a-content"
                    id="panel2a-header"
                    >
                    <p>Pregunta2</p>
                    </AccordionSummary>
                    <AccordionDetails>
                   <p>Holaaaa</p>
                    </AccordionDetails>
                </Accordion>
            </Accordion>
        )
    }
    
    export default Faq;

MainCard.js:

    import React from 'react';
import Faq from './Faq'


const MainCard = () =>{

    return(
        <div className="cardcontainer">
            <div className="nosotros">
                <div className="titulosnosotros">
                    <h1 className="titulo">Estudio sobre el diario <br />pionero de la transformación digital</h1>
                    <h2 className="subtitulo">¿Querés concoer más acerca de <br /> su reinvención?</h2>
                    <button className="download">¡Descargue aquí!</button>
                </div>
            </div>
            <div className="informacion">
                <ul className="informacionlist"> 
                    <li>
                        <img src="./images/cliente.png" />
                        <p className="informacion-texto">Cliente siempre en el centro</p>
                    </li>
                    <li>
                        <img src="./images/multiplataforma.png" />
                        <p className="informacion-texto">Más que un diario, una multiplataforma</p>
                    </li>
                    <li>
                        <img src="./images/barreras.png" />
                        <p className="informacion-texto">Atravesando barreras en la era digital</p>
                    </li>
                </ul>
            </div>
            <div className="transformacion">
                <h3 className="titulo-transformacion">Un diario que supo adaptarse</h3>
                <img className="fotodiario" src="./images/diario.jpg" />
                <h4 className="subtitulo-informacion">Desde 1869 hasta el presente digital</h4>
            </div>
            <div className="comentarios informacion">
            <h3 className="titulo-comentarios titulo-transformacion">Comentarios</h3>
            <ul className="informacionlist"> 
                    <li>
                        <img src="./images/icono.png" />
                        <p className="comentarios-texto">"¡Muy interesante!"</p>
                    </li>
                    <li>
                        <img src="./images/icono.png" />
                        <p className="comentarios-texto">"No puedo parar de leer"</p>
                    </li>
                    <li>
                        <img src="./images/icono.png" />
                        <p className="comentarios-texto">"Muchas cosas que no sabía"</p>
                    </li>
                    <li>
                        <img src="./images/icono.png" />
                        <p className="comentarios-texto">"¡Me encantó!"</p>
                    </li>
                </ul>
            </div>
            <Faq />
            
        </div>
    )
}

export default MainCard;

App.js:

import logo from './logo.svg';
import './App.css';
import MainCard from './components/MainCard';
import Contact from './components/Contact';
import Footer from './components/Footer';
import Header from './components/Header';



function App() {
  return (
    <div className="App">
      <Header />
      <MainCard />
      <Contact />
      <Footer />
    </div>
  );
}

export default App;

package.json:

{
  "name": "landingpage-lanacion",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.10",
    "@testing-library/react": "^11.2.6",
    "@testing-library/user-event": "^12.8.3",
    "react-scripts": "^4.0.3",
    "web-vitals": "^1.1.1"
  },
  
  "devDependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "@material-ui/core": "^7.0.0"
},

  

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

试试:

npm uninstall @material-ui/core
npm uninstall @material-ui/icons
npm uninstall @material-ui/lab

那么:

npm i @material-ui/core @material-ui/icons @material-ui/lab