简体   繁体   English

当同一组件中的道具更新时,如何运行 function?

[英]How to run a function when a prop updates within the same component?

I have a prop called transcript inside one of my components.我的一个组件中有一个名为 transcript 的道具。 It gets updated when I speak a voice intent.当我说出语音意图时,它会更新。 I would like to run a function anytime it changes which takes in the transcript as an argument我想在 function 任何时候运行它,它以成绩单作为参数

Here I am trying to do an OnChange={} function inside the span which I have loaded the transcript into but I know this method won't work, it was simply the easiest way of explaining what I wanted to accomplish在这里,我尝试在已加载成绩单的跨度内执行 OnChange={} function 但我知道这种方法行不通,它只是解释我想要完成的最简单的方法


import React, { Component } from "react";
import SpeechRecognition from "react-speech-recognition";
import { Button } from 'react-bootstrap';


class Dictaphone extends Component {

highlightOnRead=(transcript, cursor)=>{

         console.log("transcript",transcript)
         console.log("cursor",cursor.anchorNode.parentNode.id) //we only need the word

         if(transcript.includes(" ")){
           transcript = transcript.split(" ").pop()
         }
         if(transcript = cursor.textContent.toLowerCase()){
             cursor.style.backgroundColor = 'yellow'; //highlight the span matching the intent
         }
         cursor = document.getElementById(cursor.anchorNode.parentNode.id).nextSibling.nextElementSibling;
               return cursor
     };



  render() {
   const {transcript, resetTranscript, browserSupportsSpeechRecognition} = this.props
    var cursor=''

    if (!browserSupportsSpeechRecognition) {
      return null
    }
    if(transcript==="notes"||transcript==="cards"||transcript==="home"|| transcript==="settings" || transcript==="read document"){
      this.libraryOfIntents(transcript,resetTranscript);
    }

    return (
      <div>
        <span id="transcriptSpan"className="transcriptspan" onChange={()=>{

          if(this.props.readAlongHighlightState===true){
            if(cursor===''){
              this.highlightOnRead(transcript,window.getSelection())
            }else{
                this.highlightOnRead(transcript,cursor)
            }
          }
        }}> {transcript}  </span>
        <Button variant="outline-dark" onClick={resetTranscript}>Reset</Button>
      </div>
    )
  }
}

export default SpeechRecognition(Dictaphone)

You can use lifecycle method called componentDidUpdate您可以使用名为componentDidUpdate的生命周期方法

componentDidUpdate(prevProps) {
  if (this.props.transcript !== prevProps.transcript) { // check if your props is changed
   // Make your function call here
  }
}

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

相关问题 如何将 function(在数组内)作为 prop 传递给 React 中的子组件 - How to pass a function (within an array) as a prop to a sub component in React React Native-在同一组件中修改道具 - React Native - Modify a prop within the same component 如何避免为组件的 prop 调用相同的函数:ReactJS - How to avoid calling same function for a prop for a component : ReactJS prop 更新时更新 VueJS 组件数据属性 - Updating VueJS component data attributes when prop updates 如何使用相同的道具重复渲染相同的组件? - How to render the same component repeatedly with the same prop? 如何在另一个组件中运行 React 函数 - How to run React function within another component 当 Pinia 商店中的 state 发生更改时,如何在 vue3 组件中运行 function - How to run a function within a vue3 component when state within a Pinia store changes 加载相同的组件时,React-prop未定义 - React - prop is undefined when loading the same component 在将 function 传递给子组件时,React 似乎只允许事件侦听器道具名称与 function 名称相同 - React seems to only allow the event listener prop name to be same as the function name when passing that function to the child component 当该道具位于组件内部时,在Hoc函数中传递该道具 - Pass a prop in a Hoc function when this prop is inside the component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM