我对react.js非常陌生。我需要实现Class中知道的构造函数和更多函数。我尝试过,但是遇到useRef
和useCallback
错误。所以下面是我想将Arrow函数转换为Class的代码。请帮助我对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>
);
};
类似此类
class Webcams extends React.Component {
// Some Code ....
}
答案 0 :(得分:2)
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>
);
}
}
答案 1 :(得分:0)
您需要导入React
import React, { Component } from 'react';