简体   繁体   English

React中具有useCallback的无效钩子调用

[英]Invalid hook call with useCallback in react

I have a problem, I tried to transform in a class this function from this example : https://codesandbox.io/s/5vn3lvz2n4 我有一个问题,我尝试通过以下示例在一个类中转换此函数: https : //codesandbox.io/s/5vn3lvz2n4

But, I got an error. 但是,我遇到了一个错误。 Here is the code : 这是代码:

import React, { Component, useCallback } from "react";
import Gallery from "react-photo-gallery";
import Carousel, { Modal, ModalGateway } from "react-images";

class Gallerie extends Component {

  constructor(props){
    super(props)

    this.state = {
      currentImage: 0,
      setCurrentImage: 0,
      viewerIsOpen: false,
      setViewerIsOpen: false,
    }   
}

  render(){
    const openLightbox = useCallback((event, { photo, index }) => {
      this.state.setCurrentImage(index);
      this.state.setViewerIsOpen(true);
    }, []);

    const closeLightbox = () => {
      this.state.setCurrentImage(0);
      this.state.setViewerIsOpen(false);
    };

    return (
      <div>
        <Gallery photos={this.props.photos} onClick={() => openLightbox} />
        <ModalGateway>
          {this.state.viewerIsOpen ? (
            <Modal onClose={() =>closeLightbox}>
              <Carousel
                currentIndex={this.state.currentImage}
                views={this.props.photos.map(x => ({
                  ...x,
                  srcset: x.srcSet,
                  caption: x.title
                }))}
              />
            </Modal>
          ) : null}
        </ModalGateway>
      </div>
    );
  }
}

export default Gallerie;

Here is the problem and what I got : 这是问题所在,我得到了什么:

在此处输入图片说明

I don't exaclty know what the useCallBack does. 我不知道useCallBack做什么。 If I just copy / paste the example, it works. 如果我只复制/粘贴示例,它就可以工作。 The problem is that the variable "photos" is used as props cause it will be different for each user. 问题是变量“ photos”用作道具,因为每个用户它都会不同。 So I need it into other components. 因此,我需要将其用于其他组件。 If I use a function like the example, I can't use props... 如果我使用类似示例的函数,则无法使用道具...

Thanks for your help. 谢谢你的帮助。

You're using a hook inside a class based component . 您正在基于classcomponent使用hook First convert it to a functional component 首先将其转换为功能组件

const Gallerie = props =>{
    const [state, setState] = useState({
        currentImage: 0,
        setCurrentImage: 0,
        viewerIsOpen: false,
        setViewerIsOpen: false,
    })

    const openLightbox = useCallback((event, { photo, index }) => {
        setState({...state, setCurrentImage: index, setViewerIsOpen: true});
    }, []);

    const closeLightbox = () => {
        setState({...state, setCurrentImage: 0,setViewerIsOpen: false })
    };

    return (
        <div>
          <Gallery photos={props.photos} onClick={() => openLightbox} />
          <ModalGateway>
            {state.viewerIsOpen ? (
              <Modal onClose={() =>closeLightbox}>
                <Carousel
                  currentIndex={state.currentImage}
                  views={props.photos.map(x => ({
                    ...x,
                    srcset: x.srcSet,
                    caption: x.title
                  }))}
                />
              </Modal>
            ) : null}
          </ModalGateway>
        </div>
      );
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM