I'm making an input form and I would like to use the React Speech Recognition package to give users the option to record their input instead of typing it for two fields in the form - "review" and "order". I've made two versions of my dictaphone, as ReviewDict and OrderDict, and pass them respectively a "changeReview/changeOrder" prop to update the field.
However, whenever I click to start recording on one, it starts them both, and pulls the transcript of what I say into both input fields.
Is there a simple way to make the button for each only effect that instance of the package?
Here's the relevant parts of my code:
Input form:
<TextField
value={postData.review}
onInput={(e) =>
setPostData({ ...postData, review: e.target.value })
}
/>
</div>
<ReviewDict changeReview={dictUpdate} />
<div>
<TextField
value={postData.order}
onInput={(e) =>
setPostData({ ...postData, order: e.target.value })
}
/>
</div>
<OrderDict changeOrder={orderUpdate} />
And then my dictaphones look like this:
import React, { useState } from "react";
import { CardContent } from "@mui/material";
import { useEffect } from "react";
import MicIcon from "@mui/icons-material/Mic";
import MicOffIcon from "@mui/icons-material/MicOff";
import SpeechRecognition, {useSpeechRecognition} from "react-speech-recognition";
export const ReviewDict = ({ changeText, changeOrder, changeReview }) => {
const [reviewText, setReviewText] = useState("");
const {
transcript,
listening,
resetTranscript,
browserSupportsSpeechRecognition,
} = useSpeechRecognition();
useEffect(() => {
setReviewText(" ");
changeReview(transcript)
}, [transcript]);
if (!browserSupportsSpeechRecognition) {
return <span>Browser doesn't support speech recognition.</span>;
}
return (
<div>
<CardContent sx={{ paddingTop: 5, fontSize: 25 }}>
<center>
{!listening ? (
<>
{" "}
<span onClick={SpeechRecognition.startListening}>
<MicIcon fontSize="normal" /> review
</span>
</>
) : (
<>
<span onClick={SpeechRecognition.stopListening}>
<MicOffIcon fontSize="normal" />
</span>
</>
)}
</center>
</CardContent>
</div>
);
};
and
import React, { useState } from "react";
import { CardContent } from "@mui/material";
import { useEffect } from "react";
import MicIcon from "@mui/icons-material/Mic";
import MicOffIcon from "@mui/icons-material/MicOff";
import RestartAltIcon from "@mui/icons-material/RestartAlt";
import { Card, Rating, TextField, Box } from "@mui/material";
import SpeechRecognition, {
useSpeechRecognition,
} from "react-speech-recognition";
export const OrderDict = ({ changeText, changeOrder, changeReview }) => {
const [reviewText, setReviewText] = useState("");
const {
transcript,
listening,
resetTranscript,
browserSupportsSpeechRecognition,
} = useSpeechRecognition();
useEffect(() => {
setReviewText(" ");
changeOrder(transcript);
}, [transcript]);
if (!browserSupportsSpeechRecognition) {
return <span>Browser doesn't support speech recognition.</span>;
}
return (
<div>
<CardContent sx={{ paddingTop: 5, fontSize: 25 }}>
<center>
{!listening ? (
<>
{" "}
<span onClick={SpeechRecognition.startListening}>
<MicIcon fontSize="normal" /> order
</span>
</>
) : (
<>
<span onClick={SpeechRecognition.stopListening}>
<MicOffIcon fontSize="normal" />
</span>
</>
)}
{/*
<br />
{transcript && (
<>
<span onClick={resetTranscript}>
<RestartAltIcon fontSize="large" />
Re-record
</span>
</>
)}
*/}
</center>
</CardContent>
</div>
);
};
Any help much appreciated!