如何从功能组件转换为类组件?

时间:2021-06-01 11:02:18

标签: reactjs react-native

嗨,我正在为我的协作白板进行语音识别。我的白板组件是一个类组件,我想把语音识别的代码(这是一个功能组件)放在白板组件中。但为此,我需要将其转换为类组件。我正在使用图书馆 react-speech-recognition 这是需要转换的代码:

import React from 'react'
import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition'

const Mic = () => {
    const { transcript, resetTranscript } = useSpeechRecognition()

    if (!SpeechRecognition.browserSupportsSpeechRecognition()) {
        return null
    }

    return (
        <div>
            <button onClick={SpeechRecognition.startListening}>Start</button>
            <button onClick={SpeechRecognition.stopListening}>Stop</button>
            <button onClick={resetTranscript}>Reset</button>
            <p>{transcript}</p>
        </div>
    )
}
export default Mic

谁能告诉我怎么做?使用 transcriptresetTranscript 变量的语音识别功能让我更难了。

1 个答案:

答案 0 :(得分:0)

你的钩子 useSpeechRecognition 有问题,你不能在类组件中使用它,你可以查看 React 文档来验证:Hooks FAQ,作为替代,你应该使用 HOC { {1}}:

SpeechRecognition