繁体   English   中英

React setstate只能在重新渲染时更新已安装或安装的组件

[英]React setstate can only update a mounted or mounting component — on rerender

我正在使用react-speech-recognition软件包在我的应用程序中进行语音到文本。

app.js内部渲染:

            <ChatContainer
              micActive={this.state.micActive}
              sendData={this.sendData}
              makeInactive={this.makeInactive}
            >
                <SpeechToText>
                    sendData={this.sendData}
                    makeInactive={this.makeInactive}
                    micActive={this.state.micActive}
                </SpeechToText>

                  <div>
                      <button
                        id="micInactive"
                        type="button"
                        onClick={this.makeActive}
                      />
                  </div>

            </ChatContainer>

如上所示,我的ChatContainer有两个Children

  1. SpeechToText

  2. div包含一个按钮

SpeechToText.js:

class SpeechToText extends Component {

    componentWillReceiveProps(nextProps) {
        if (nextProps.finalTranscript && nextProps.micActive) {
            this.props.sendData(nextProps.finalTranscript);
            this.props.resetTranscript();
            this.props.makeInactive();
        }
    }

    render() {
        return (
            <div>
                <button
                  id="micActive"
                  type="button"
                />
            </div>
        );
    }
}

export default SpeechRecognition(SpeechToText);

SpeechToTextSpeech Recognition接收语音识别props

ChatContainer.js

const ChatContainer = props => (

    <div>
        {
             React.Children.map(props.children, (child, i) => {
                 if (i === 0 && child.props.active) {
                     return React.cloneElement(child, {
                         sendData: props.sendData,
                         makeInactive: props.makeInactive,
                         micActive: props.micActive,
                     });
                 }

                 if (i === 1 && child.props.inactive) {
                     return child;
                 }
                 return null;
             })
        }
    </div>
);

export default connect()(ChatContainer);

最后, ChatContainer决定要渲染的child ChatContainer 如果状态为非活动状态,则使用非活动按钮渲染div

编辑

默认情况下,状态为inactive - this.state.micActive: false 如果状态为非活动状态,则使用button渲染<div> 如果单击该按钮,则调用makeActive方法并使状态处于活动状态 - 如果状态为活动状态,则呈现<SpeechToText> 一旦我完成了语音转文本,我就调用makeInactive - 这会使状态处于非活动状态,并再次呈现<div>

我第一次单击按钮SpeechToText得到渲染,语音到文本工作。

但是第二次单击按钮 - 我尝试重新SpeechToText组件时出现错误:

setstate can only update a mounted or mounting component

有时错误不会出现,但语音到文本不起作用。

为什么会发生这种情况 - 我是否需要强行删除组件?

事实证明,这是SpeechRecognitionContainer一个问题。 该软件包更新了新的道具和配置选项,我解决了我的问题。

您可以在此处阅读有关react-speech-recognition更多信息。

现在我可以像这样渲染组件:

render() {
    return (
        <SpeechToText
          sendSpeechToText={this.sendSpeechToText}

        />
    );
}

和SpeechToText看起来像这样:

class SpeechToText extends Component {

    constructor(props) {
        super(props);

        this.reactivate = this.reactivate.bind(this);
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.finalTranscript && nextProps.micActive) {
            this.props.sendSpeechToText(nextProps.finalTranscript);
            this.props.resetTranscript();
            this.props.stopListening();
        }
    }

    componentWillUnmount() {
        if (this.props.listening) {
            this.props.abortListening();
        }
    }

    reactivate() {
        if (!this.props.listening) {
           this.props.startListening();
    }

    render() {
        return (
            <div>
                <button
                  id="micButton"
                  type="button"
                  onClick={this.reactivate}
                />
            </div>
        );
    }
}

const options = {
  autoStart: false
}

export default SpeechRecognition(options)(SpeechToText)

暂无
暂无

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

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