简体   繁体   English

试图创建一个反应组件,但无法在顶层调用 React Hook “React.useState”。”

[英]Trying to Create a react Component but getting React Hook "React.useState" cannot be called at the top level."

I am trying to add a bunch of modals to my React component but I keep getting the error " "React.useState" cannot be called at the top level ".我正在尝试向我的 React 组件添加一堆模式,但我不断收到错误“无法在顶层调用“React.useState”。 This code was originally in my App.js file and it ran but I wanted to make it neater by making it into component.这段代码最初在我的 App.js 文件中,它可以运行,但我想通过将其制成组件来使其更整洁。

const[showTotModal, setTotShowModal] = React.useState(false)
    const openTotModal = () => {
    setTotShowModal(prev => !prev);
    };

    const[showCasModal, setCasShowModal] = React.useState(false)
    const openCasModal = () => {
    setCasShowModal(prev => !prev);
    };

    const[showGraModal, setGraShowModal] = React.useState(false)
    const openGraModal = () => {
    setGraShowModal(prev => !prev);
    };

    const[showKikModal, setKikShowModal] = React.useState(false)
    const openKikModal = () => {
    setKikShowModal(prev => !prev);
    };

    const[showSpiModal, setSpiShowModal] = React.useState(false)
    const openSpiModal = () => {
    setSpiShowModal(prev => !prev);
    };

    const[showHowModal, setHowShowModal] = React.useState(false)
    const openHowModal = () => {
    setHowShowModal(prev => !prev);
    };

class GhibliModal extends Component{

    render(){

        return(
        <Container>
            <Button onClick={openTotModal}> <img src={tot} alt="tot" height='300' /> </Button>
            <Button onClick={openCasModal}> <img src={cas} alt="cas" height='300' /> </Button>
            <Button onClick={openGraModal}> <img src={gra} alt="gra" height='300' /> </Button>
            <Button onClick={openKikModal}> <img src={kik} alt="gra" height='300' /> </Button>
            <Button onClick={openSpiModal}> <img src={spi} alt="gra" height='300' /> </Button>
            <Button onClick={openHowModal}> <img src={how} alt="gra" height='300' /> </Button>
            
            <TotModal showModal={showTotModal} setShowModal={setTotShowModal}/>
            <CasModal showModal={showCasModal} setShowModal={setCasShowModal}/>
            <GraModal showModal={showGraModal} setShowModal={setGraShowModal}/>
            <KikModal showModal={showKikModal} setShowModal={setKikShowModal}/>
            <SpiModal showModal={showSpiModal} setShowModal={setSpiShowModal}/>
            <HowModal showModal={showHowModal} setShowModal={setHowShowModal}/>
        </Container>

        )

    }

}

export default GhibliModal

First Thing Hooks only work inside Functional components you are creating a class component and placing hooks on top of that here is the same code I just converted the class component to an Arrow function and placed the hooks inside the component First Thing Hooks 仅适用于功能组件,您正在创建 class 组件并在其上放置挂钩 这是相同的代码,我刚刚将 class 组件转换为箭头 ZC1C425268E68385D1AB5074C17A94F4 组件并将挂钩放置在组件内部

const GhibliModal  = () =>{

const[showTotModal, setTotShowModal] = React.useState(false)
const[showHowModal, setHowShowModal] = React.useState(false)
const[showCasModal, setCasShowModal] = React.useState(false)
 const[showGraModal, setGraShowModal] = React.useState(false)
const[showKikModal, setKikShowModal] = React.useState(false)
const[showSpiModal, setSpiShowModal] = React.useState(false)
    const openTotModal = () => {
    setTotShowModal(prev => !prev);
    };

    
    const openCasModal = () => {
    setCasShowModal(prev => !prev);
    };

   
    const openGraModal = () => {
    setGraShowModal(prev => !prev);
    };

    
    const openKikModal = () => {
    setKikShowModal(prev => !prev);
    };

    
    const openSpiModal = () => {
    setSpiShowModal(prev => !prev);
    };

    
    const openHowModal = () => {
    setHowShowModal(prev => !prev);
    };
  

        return(
        <Container>
            <Button onClick={openTotModal}> <img src={tot} alt="tot" height='300' /> </Button>
            <Button onClick={openCasModal}> <img src={cas} alt="cas" height='300' /> </Button>
            <Button onClick={openGraModal}> <img src={gra} alt="gra" height='300' /> </Button>
            <Button onClick={openKikModal}> <img src={kik} alt="gra" height='300' /> </Button>
            <Button onClick={openSpiModal}> <img src={spi} alt="gra" height='300' /> </Button>
            <Button onClick={openHowModal}> <img src={how} alt="gra" height='300' /> </Button>
            
            <TotModal showModal={showTotModal} setShowModal={setTotShowModal}/>
            <CasModal showModal={showCasModal} setShowModal={setCasShowModal}/>
            <GraModal showModal={showGraModal} setShowModal={setGraShowModal}/>
            <KikModal showModal={showKikModal} setShowModal={setKikShowModal}/>
            <SpiModal showModal={showSpiModal} setShowModal={setSpiShowModal}/>
            <HowModal showModal={showHowModal} setShowModal={setHowShowModal}/>
        </Container>

        )

    

}

export default GhibliModal

useState hook is a replacement for function components as the latter do not have state. useState钩子是 function 组件的替代品,因为后者没有 state。

Add a constructor:添加构造函数:

constructor(props) {
    super(props);
    this.state = {tot: false, how: false, cas: false, gra: false, kik:false, spi: false };
}

And add the other methods to the body of your class component.并将其他方法添加到 class 组件的主体中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Hook "React.useState" 在 function "form" 中调用,它既不是 React function 组件也不是自定义 React Hook ZC1C425268E68385D1AB5074C17A94F - React Hook "React.useState" is called in function "form" that is neither a React function component nor a custom React Hook function React.useState() 的问题 - Troubles with React.useState() React Hook“useState”无法在 class 组件中调用。 React Hooks 必须在 React function 组件或自定义 React Hook function 中调用 - React Hook "useState" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function 不能在回调中调用 React Hook “useState” - React Hook “useState” cannot be called inside a callback React.useState() 钩子不反映新值 - React.useState() hook doesn't reflect new value 具有动画和useEffect的React.useState - React.useState with animation and useEffect React 16.7 TypeError: (0 , _react.useState) 不是函数 - React 16.7 TypeError: (0 , _react.useState) is not a function 在 function 中调用 React Hook “useState” - React Hook “useState” is called in function React Hook &quot;useState&quot; 在函数 &quot;setResults&quot; 中调用,它既不是 React 函数组件,也不是自定义 React Hook 函数 - React Hook "useState" is called in function "setResults" which is neither a React function component or a custom React Hook function React Hook“useState”在 function“increaseCounter”中被调用,它既不是 React function 组件,也不是自定义 React Hook function - React Hook "useState" is called in function "increaseCounter" that is neither a React function component nor a custom React Hook function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM