reactJS - 无效的钩子调用。 Hooks 只能在函数组件内部调用

时间:2021-07-21 14:29:08

标签: reactjs twitter-bootstrap

我对 reactJS 还很陌生,我正在尝试像这样使用 bootstrap Modal:

class Footer extends Component {

  render() { 

    const [show, setShow] = useState(false);

    const handleClose = () => setShow(false);
    const handleShow = () => setShow(true);

    return ( 

    <footer className="footer-bottom">
      <Container>
           <Modal show={show} onHide={handleClose}>
          <Modal.Header closeButton>
          <Modal.Title>Modal heading</Modal.Title>
          </Modal.Header>
          <Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
          <Modal.Footer>
          <Button variant="secondary" onClick={handleClose}>
              Close
          </Button>
          <Button variant="primary" onClick={handleClose}>
              Save Changes
          </Button>
          </Modal.Footer>
      </Modal>
      </Container> 
     </footer>
  );
  }
}

但是我收到此错误:

Invalid hook call. Hooks can only be called inside of the body of a function component.

我的问题是如何将我的 const(show、setShow、handleClose、handelShow)转换成一个函数来让它工作?

4 个答案:

答案 0 :(得分:0)

Hooks 只能用在函数组件中,所以你的组件不能扩展 React.Component。而不是这样:

class Footer extends Component {

这样做:

Footer = (props) => {

   //rest of the code 

}

因为您现在没有扩展 Component 类,所以不需要 render() 函数。只需从页脚函数返回即可。

答案 1 :(得分:0)

问题是你现在制作的组件是一个类组件,而不是一个函数组件。截至目前,hooks don't work inside classes。尝试将您的代码更改为:

export default function Footer() {


    const [show, setShow] = useState(false);

    const handleClose = () => setShow(false);
    const handleShow = () => setShow(true);

    return ( 

    <footer className="footer-bottom">
      <Container>
           <Modal show={show} onHide={handleClose}>
          <Modal.Header closeButton>
          <Modal.Title>Modal heading</Modal.Title>
          </Modal.Header>
          <Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
          <Modal.Footer>
          <Button variant="secondary" onClick={handleClose}>
              Close
          </Button>
          <Button variant="primary" onClick={handleClose}>
              Save Changes
          </Button>
          </Modal.Footer>
      </Modal>
      </Container> 
     </footer>
  );
  
}

答案 2 :(得分:0)

Hooks 不能在类组件中使用,你可以将其转换为函数式组件:

const Footer = () => {
    return (
         ...
    )
}

您也可以将其保留为类组件:

class Footer extends Component {
    constructor(props){
        super(props)
        this.state = {
             show: false
        }
    }
}

答案 3 :(得分:0)

以下是您应该如何创建函数:

//add your imports
import { useState } from 'react';

//pass props if you're using prop drilling
function Footer(props) {
    const [show, setShow] = useState(false);

    const handleClose = () => setShow(false);
    const handleShow = () => setShow(true);
    return (
        <footer className="footer-bottom">
            <Container>
                <Modal show={show} onHide={handleClose}>
                    <Modal.Header closeButton>
                        <Modal.Title>Modal heading</Modal.Title>
                    </Modal.Header>
                    <Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
                    <Modal.Footer>
                        <Button variant="secondary" onClick={handleClose}>
                            Close
                        </Button>
                        <Button variant="primary" onClick={handleClose}>
                            Save Changes
                        </Button>
                    </Modal.Footer>
                </Modal>
            </Container>
        </footer>
    );
}

//export the function
export default Footer;