Modal shows on page refresh

huangapple go评论58阅读模式
英文:

Modal shows on page refresh

问题

以下是您要翻译的内容:

Having a difficult time with figuring out why modal shows on page refresh but does not show when About link is clicked. I think it has something to do with prop passing but do not understand what is going wrong.

App.js file:

function App() {
  const [showAboutModal, setShowAboutModal] = useState(false);
  const navLinkClicked = (selectedKey) => {
    if (selectedKey==='about') {
      alert(showAboutModal)
      setShowAboutModal(true)
    }
  }

  return (
    <div className="App">
      <AboutModal showAboutModal={showAboutModal}/>

      <Navbar collapseOnSelect expand="lg" bg="light">
      <Container>
        <img width="50" height="50" src={beacon} className="App-logo" alt="logo" />
        <Navbar.Brand href="/">Title</Navbar.Brand>
        <Navbar.Toggle aria-controls="responsive-navbar-nav" />
        <Navbar.Collapse id="responsive-navbar-nav">
          <Nav className="me-auto">  
          </Nav>
          <Nav
            onSelect={(selectedKey) => navLinkClicked(selectedKey)}>
            <Nav.Link eventKey="about">About</Nav.Link>
          </Nav>
        </Navbar.Collapse>
      </Container>
    </Navbar>
    </div>
  );
}

export default App;

AboutModal.js file:

function AboutModal(showAboutModal) {


  const [showModal, setShowModal] = useState(showAboutModal);

  return (
      <Modal show={showModal}>
        <Modal.Header>
          <Modal.Title>About</Modal.Title>
        <Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={()=>setShowModal(false)}>
            Close
          </Button>
        </Modal.Footer>
      </Modal>
  );
}

export default AboutModal;

I think the problem is in this line: `const [showModal, setShowModal] = useState(showAboutModal);`
英文:

Having a difficult time with figuring out why modal shows on page refresh but does not show when About link is clicked. I think it has something to do with prop passing but do not understand what is going wrong.

App.js file:

function App() {
  const [showAboutModal, setShowAboutModal] = useState(false);
  const navLinkClicked = (selectedKey) => {
    if (selectedKey==='about') {
      alert(showAboutModal)
      setShowAboutModal(true)
    }
  }

  return (
    <div className="App">
      <AboutModal showAboutModal={showAboutModal}/>

      <Navbar collapseOnSelect expand="lg" bg="light">
      <Container>
        <img width="50" height="50" src={beacon} className="App-logo" alt="logo" />
        <Navbar.Brand href="/">Title</Navbar.Brand>
        <Navbar.Toggle aria-controls="responsive-navbar-nav" />
        <Navbar.Collapse id="responsive-navbar-nav">
          <Nav className="me-auto">  
          </Nav>
          <Nav
            onSelect={(selectedKey) => navLinkClicked(selectedKey)}>
            <Nav.Link eventKey="about">About</Nav.Link>
          </Nav>
        </Navbar.Collapse>
      </Container>
    </Navbar>
    </div>
  );
}

export default App;

AboutModal.js file:

function AboutModal(showAboutModal) {


  const [showModal, setShowModal] = useState(showAboutModal);

  return (
      <Modal show={showModal}>
        <Modal.Header>
          <Modal.Title>About</Modal.Title>
        </Modal.Header>
        <Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={()=>setShowModal(false)}>
            Close
          </Button>
        </Modal.Footer>
      </Modal>
  );
}

export default AboutModal;

I think the problem is in this line: const [showModal, setShowModal] = useState(showAboutModal);

答案1

得分: 1

AboutModal 组件中的 showAboutModal 变量始终为真,并且会评估为一个对象,如 { "showAboutModal": false }。这意味着模态框会在页面加载时可见,因为您正在使用对象来切换可见性,而不是该对象的特定键的值。

AboutModal 组件可以在函数声明中解构 props,或者在 useState 调用中指定键,如下所示:

function AboutModal({ showAboutModal }) { // 解构
  const [showModal, setShowModal] = useState(showAboutModal);
  //...

// 或者
function AboutModal(props) {
  const [showModal, setShowModal] = useState(props.showAboutModal); // 指定键
  //...
英文:

The value for showAboutModal in the AboutModal component is always truthy and evaluates to an object like { "showAboutModal": false }. This means the modal will be visible on page load because you are using the object to toggle visibility and not the value of a specific key of that object.

The AboutModal component could either destructure the props in the function declaration or specify the key in the useState call as below:

function AboutModal({ showAboutModal }) { // destructure
  const [showModal, setShowModal] = useState(showAboutModal);
//...

//OR
function AboutModal(props) {
  const [showModal, setShowModal] = useState(props.showAboutModal); // specify key
//...

huangapple
  • 本文由 发表于 2023年2月6日 17:30:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定