Javascript 为什么我会得到“一个”呢;setShowModal未定义;react中的错误消息?

Javascript 为什么我会得到“一个”呢;setShowModal未定义;react中的错误消息?,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,我的代码: import React, { useState } from "react"; import Modal from "./Modal"; function showModal() { setShowModal(true); } export default function NewPost() { const [showModal, setShowModal] = useState(false); if (showModal) { re

我的代码:

import React, { useState } from "react";
import Modal from "./Modal";

function showModal() {
 setShowModal(true);
}

export default function NewPost() {
const [showModal, setShowModal] = useState(false);
if (showModal) {
return (
  <h1>
    <Modal />
  </h1>
);
 }

return (
<div className="new-post">
  <button onClick={showModal} type="button" className="new-post-form">
    Was machst du gerade, Toni?
  </button>
  <div className="options">
    <i style={{ color: "red" }} className="fas fa-stream"></i>
    <p className="option">Live-Video</p>
    <i style={{ color: "green" }} className="fas fa-camera"></i>
    <p className="option">Foto/Video</p>
    <i style={{ color: "yellow" }} className="fas fa-smile"></i>
    <p className="option">Gefühl/Aktivität</p>
  </div>
</div>
);
}
import React,{useState}来自“React”;
从“/Modal”导入模态;
函数showmodel(){
设置显示模式(真);
}
导出默认函数NewPost(){
const[showmodel,setshowmodel]=useState(false);
如果(显示模式){
返回(
);
}
返回(
是麦斯特·杜杰拉德吗,托尼?

现场视频

Foto/视频

Gefühl/Aktivität

); }
我必须描述更多。。。(堆栈溢出消息) .................................................
因为
setshowmodel
是在
NewPost
组件内部定义的,所以它在组件外部不可用,只需将
showmodel
功能放在组件内部即可


同时重命名
showmodel
函数,因为它已经由
useState
hook

定义,因为
setshowmodel
是在
NewPost
组件内部定义的,所以它在组件外部不可用,只需将
showmodel
函数放在组件内部即可


还可以重命名
showmodel
函数,因为它已经由
useState
hook

定义。您可以将
showmodel
函数放入
NewPost
中,并重命名
showmodel
函数

    import React, { useState } from "react";
    import Modal from "./Modal";
    
    
    export default function NewPost() {
    const [showModal, setShowModal] = useState(false);

     function newShowModal() {
      setShowModal(true);
    }

    if (showModal) {
    return (
      <h1>
        <Modal />
      </h1>
    );
     }
    
    return (
    <div className="new-post">
      <button onClick={showModal} type="button" className="new-post-form">
        Was machst du gerade, Toni?
      </button>
      <div className="options">
        <i style={{ color: "red" }} className="fas fa-stream"></i>
        <p className="option">Live-Video</p>
        <i style={{ color: "green" }} className="fas fa-camera"></i>
        <p className="option">Foto/Video</p>
        <i style={{ color: "yellow" }} className="fas fa-smile"></i>
        <p className="option">Gefühl/Aktivität</p>
      </div>
    </div>
    );
    }
import React,{useState}来自“React”;
从“/Modal”导入模态;
导出默认函数NewPost(){
const[showmodel,setshowmodel]=useState(false);
函数newshowmodel(){
设置显示模式(真);
}
如果(显示模式){
返回(
);
}
返回(
是麦斯特·杜杰拉德吗,托尼?

现场视频

Foto/视频

Gefühl/Aktivität

); }
您可以将
showmodel
函数放入
NewPost
中,并重命名
showmodel
函数

    import React, { useState } from "react";
    import Modal from "./Modal";
    
    
    export default function NewPost() {
    const [showModal, setShowModal] = useState(false);

     function newShowModal() {
      setShowModal(true);
    }

    if (showModal) {
    return (
      <h1>
        <Modal />
      </h1>
    );
     }
    
    return (
    <div className="new-post">
      <button onClick={showModal} type="button" className="new-post-form">
        Was machst du gerade, Toni?
      </button>
      <div className="options">
        <i style={{ color: "red" }} className="fas fa-stream"></i>
        <p className="option">Live-Video</p>
        <i style={{ color: "green" }} className="fas fa-camera"></i>
        <p className="option">Foto/Video</p>
        <i style={{ color: "yellow" }} className="fas fa-smile"></i>
        <p className="option">Gefühl/Aktivität</p>
      </div>
    </div>
    );
    }
import React,{useState}来自“React”;
从“/Modal”导入模态;
导出默认函数NewPost(){
const[showmodel,setshowmodel]=useState(false);
函数newshowmodel(){
设置显示模式(真);
}
如果(显示模式){
返回(
);
}
返回(
是麦斯特·杜杰拉德吗,托尼?

现场视频

Foto/视频

Gefühl/Aktivität

); }
谢谢,现在可以用了!谢谢,现在可以用了!