I am working on a screen which has a button to record audio. And depending on the state of the recording I'd like to hide a text input box which is on the same screen. To hide it I am changing the opacity and making it not editable.
I am setting 2 states (isTextInputEditable
and textInputOpacity
) based on 2 other ones (isRecording
and isRecorded
). I am having issues with my current code because useState is asynchronous and the state of isRecording and isRecorded are not updated when I perform the checks in setTextInputVisibility
function.
Any tips would be helpful, thanks.
const [isTextInputEditable, setIsTextInputEditable] = useState(true);
const [textInputOpacity, setTextInputOpacity] = useState(1);
const [isRecording, setIsRecording] = useState(false);
const [isRecorded, setIsRecorded] = useState(false);
const onRecord = () => {
if (!isRecording) {
console.log("Started recording audio")
setIsRecording(true)
setIsRecorded(false)
}
else {
console.log("Stopped recording audio")
setIsRecording(false)
setIsRecorded(true)
}
setTextInputVisibility();
}
const setTextInputVisibility = () => {
if (!isRecording && !isRecorded){
setIsTextInputEditable(true)
setTextInputOpacity(1)
console.log("Text input visible")
}
else{
setIsTextInputEditable(false)
setTextInputOpacity(0)
console.log("Text input invisible")
}
}