Javascript 将Arrow函数转换为React.js中的类
我是react.js的新手。我需要实现构造函数和更多我在课堂上知道的函数。我试过了,但出现了Javascript 将Arrow函数转换为React.js中的类,javascript,reactjs,Javascript,Reactjs,我是react.js的新手。我需要实现构造函数和更多我在课堂上知道的函数。我试过了,但出现了useRef和useCallback错误。下面是我想将Arrow函数转换为类的代码。请帮助我,我对React.js非常陌生 const Webcams = () => { const webcamRef = React.useRef(null); const capture = React.useCallback( () => { const imageSrc
useRef
和useCallback
错误。下面是我想将Arrow函数转换为类的代码。请帮助我,我对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>
);
};
import React,{Component}来自'React';
类欢迎扩展组件{
建造师(道具){
超级(道具);
this.webcamRef=React.createRef();
}
捕获=()=>{
const imageSrc=this.webcamRef.current.getScreenshot();
...
}
show=()=>{
...
}
render(){
返回(
);
}
}
您需要导入React
import React, { Component } from 'react';
import React, { Component } from 'react';