[英]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.