Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

reactjs - I want show a variable on the main page

I have a problem making a voice recognition app with React Js. I wrote this code where I have made a variable called "transcript" that contains what I speak inside the recognition.onresult and I made it print in the console but I want to show what I speak on the main page(like in an <h1></h1>). How can I do that?

import "./styles.css";

let img_url = "voice_off.png";

const SpeechRecognition =
  window.SpeechRecognition || window.webkitSpeechRecognition;

const recognition = new SpeechRecognition();

recognition.start();

function App(props) {
  const [count, setCount] = useState(0);

  const voiceCommands = () => {
    // On Start
    recognition.onstart = () => {
      console.log("voice is activated");
    };

    // Do something when we get a result
    recognition.onresult = (e) => {
      let current = e.resultIndex;

      let transcript = e.results[current][0].transcript;
      console.log(transcript);

      if (transcript === "next") {
        setCount(count + 1);
      } else {
        if (transcript === "go back") {
          setCount(count - 1);
        }
      }
    };
  };

  useEffect(() => {
    voiceCommands();
  });

  return (
    <div className="app">
      <p className="text">Counter: </p>
      <h1 className="text"> {count} </h1>
      <div className="image_cropper">
        <img className="voice_image" src={img_url} alt="nothing"></img>
      </div>
      <button className="button_plus" onClick={() => setCount(count + 1)}>
        +
      </button>
      <button className="button_minus" onClick={() => setCount(count - 1)}>
        -
      </button>
      <button className="button_microphone" onClick={() => recognition.start()}>
        {" "}
        click me to enable mic!!!
      </button>
    </div>
  );
}

export default App;```
question from:https://stackoverflow.com/questions/66058738/i-want-show-a-variable-on-the-main-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create a new variable to store the entire transcript like this

const [msg, setMsg] = useState("")

Next, whenever the recognition.onresult method is called, you can append the new transcript to what is being displayed on the browser like this

setMsg(msg + " " + transcript)

Now what you have done is set the transcript in the react component state.
Last step is to print it in the brower.

Just add a line in the return object like this

<h1>{msg}</h1>

Please don't flame me if my solution isn't what you are looking for thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...