将类组件转换为功能组件

时间:2019-10-11 00:02:20

标签: javascript html reactjs camera

我将尝试解释任务,然后告诉您我的问题。我的任务是添加一个可以拍照的网络摄像头组件,然后单击按钮上传图片。

我正在使用react-webcam库,这个是https://www.npmjs.com/package/react-webcam 并创建了一个类组件“ class UploadPhoto extends Component”,其中我正在渲染react-webcam(Webcam标签)并能够成功显示该网络摄像头。

但是,当我在标签中添加以下属性(ref={webcamRef})时,会收到一个错误消息,我正在进行无效的钩子调用,因为webcamRef初始化为钩子useRef(null)。如下:

const webcamRef = React.useRef(null);

错误:

  

未捕获的永久违反:无效的挂接调用。钩子只能是   称为功能组件主体内部。

我知道我们不能在类组件中调用钩子,所以我的问题是如何将类组件转换为功能组件,以便可以使用此钩子来帮助捕获照片。< / p>

如果您需要我提供更多信息,请告诉我。

谢谢。 PS-我对ReactJS相当陌生,并尝试尝试学习它。

以下是我的上载Photo的类组件:

import React, { Fragment, useRef, Component } from 'react';
import ReactDOM from 'react-dom';
import { Camera } from '../../lib';
import Header from '../header';
import Webcam from "react-webcam";

import {
    Button,
} from 'reactstrap';
import axios from "axios/index";

class UploadPhoto extends Component {

constructor(props) {
    super(props);
    this.capture = this.capture.bind(this)
    this.next = this.next.bind(this)
}

capture(imgSrc) {
    console.log("image captured : " + imgSrc);
}

next() {
    console.log("next button clicked")
    //go to review details component
   // this.props.history.push("/review-details");
}

render() {
    const webcamRef = React.useRef(null);
    return (
        <div className="text-center">
            <div>
                Take a Picture
            </div>
            <Webcam
                audio={false}
                height={500}
                ref={webcamRef}
                screenshotFormat="image/jpeg"
                width={600} >
            <Button className="position-relative text-center" onClick={this.capture}>Capture photo</Button>
                </Webcam>
            <div>
            <Button className="position-relative text-center btn-start" onClick={this.next}>NEXT</Button>
            </div>
        </div>
    );
}
}
export default UploadPhoto;

2 个答案:

答案 0 :(得分:1)

尽管您可以将组件转换为功能组件,但这对于解决此问题不是必需的。引用也可以在类组件中使用,而useRef则不能。而是使用createRef

constructor(props) {
    super(props);
    this.capture = this.capture.bind(this)
    this.next = this.next.bind(this)
    this.webcamRef = React.createRef();
}

componentDidMount() {
    const imageSrc = this.webcamRef.current.getScreenshot();
    this.capture(imagesrc);
}

render() {
    return (
        <div className="text-center">
            <div>
                Take a Picture
            </div>
            <Webcam
                audio={false}
                height={500}
                ref={this.webcamRef}
                screenshotFormat="image/jpeg"
                width={600} >
            <Button className="position-relative text-center" onClick={this.capture}>Capture photo</Button>
                </Webcam>
            <div>
            <Button className="position-relative text-center btn-start" onClick={this.next}>NEXT</Button>
            </div>
        </div>
    );
}

答案 1 :(得分:1)

由于问题是如何将类转换为功能组件,因此以下是与功能相同的组件:

import React, { Fragment, useRef, Component } from 'react';
import ReactDOM from 'react-dom';
import { Camera } from '../../lib';
import Header from '../header';
import Webcam from "react-webcam";

import {
    Button,
} from 'reactstrap';
import axios from "axios/index";

const UploadPhoto = props => {
    const webcamRef = useRef(null);
    const capture = () => {
        const imgSrc = webcamRef.current.getScreenshot();
        console.log("image captured : " + imgSrc);
    }
    const next = () => {
        console.log("next button clicked");
        //go to review details component
       // props.history.push("/review-details");
    }
    return (
        <div className="text-center">
            <div>
                Take a Picture
            </div>
            <Webcam
                audio={false}
                height={500}
                ref={webcamRef}
                screenshotFormat="image/jpeg"
                width={600} >
            <Button className="position-relative text-center" onClick={capture}>Capture photo</Button>
                </Webcam>
            <div>
            <Button className="position-relative text-center btn-start" onClick={next}>NEXT</Button>
            </div>
        </div>
    )
}

export default UploadPhoto;