简体   繁体   English

在 React.js 中将 Arrow Function 转换为 Class

[英]Convert Arrow Function to Class in React.js

I am very new to react.js.我对 react.js 很陌生。 I need to implement constructor and more functions which I am aware in Class.我需要实现我在 Class 中知道的构造函数和更多函数。 I tried but I am getting useRef and useCallback error.我试过了,但我收到了useRefuseCallback错误。 So below are the code I want to convert Arrow function to Class.所以下面是我想将 Arrow function 转换为 Class 的代码。 Please help I am very very new to React.js请帮助我对 React.js 非常陌生

const Webcams = () => {
  const webcamRef = React.useRef(null);

  const capture = React.useCallback(
    () => {

      const imageSrc = webcamRef.current.getScreenshot();
      console.log(imageSrc);
      image64 = imageSrc;
      var newImg = imageSrc.replace('data:image/jpeg;base64,', '');

      var rand = makeid(5)+'.jpg';

      const uploadTask = storage.ref(`images/${rand}`).putString(newImg,'base64');
      uploadTask.on('state_changed',
      (snapshot) => {

        const progress = Math.round((snapshot.bytesTransferred / snapshot.totalBytes) * 100);
        console.log(progress);
      },
      (error) => {

        console.log(error);
      },
      () => {

        storage.ref('images').child(rand).getDownloadURL().then(url => {
            console.log(url);

            firebase
            .firestore()
            .collection('notes')
            .add({
              url: url
            })
            .then(() => {
               console.log('Finished');
            })
        })
      });
    },
    [webcamRef]
  );

  const show = React.useCallback(
    () => {

      var f = document.getElementById('flash');
         setTimeout(function() {
            f.style.display = (f.style.display == 'none' ? '' : 'show');
         }, 100);
    },
  );

  return (
    <div>
    <div id="flash"> </div>
    <div className="web">
      <Webcam
        audio={false}
        height={400}
        ref={webcamRef}
        screenshotFormat="image/jpeg"
        width={400}
        videoConstraints={videoConstraints}
      />
      <span><button onClick={capture}></button></span>
      </div>

      </div>

  );
};

something like this Class像这样的东西 Class

class Webcams extends React.Component {

    // Some Code ....

    }
import React, { Component } from 'react';

class Welcome extends Component {
  constructor(props) {
    super(props);

    this.webcamRef = React.createRef();
  }

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

  show = () => {
    ...
  }

  render() {
    return (
      <div>
        <div id="flash"> </div>
          <div className="web">
          <Webcam
            audio={false}
            height={400}
            ref={this.webcamRef}
            screenshotFormat="image/jpeg"
            width={400}
            videoConstraints={videoConstraints}
          />
          <span><button onClick={this.capture}></button></span>
        </div>
      </div>
    );
  }
}

You need to import React你需要导入 React

import React, { Component } from 'react';

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

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