I am new to react native and have recently implemented state in my code.
I currently have 2 buttons: one to open the camera in my app and one to close the camera.
What I want to know is this: would there be a way for me to change a button's state based on when it's been pressed? for example: if I press a button, the button will record a video, but then if I press that same button again it'll stop. I currently only know how to change the state in a situation like this by using 2 different buttons opposed to making the state change within a singular button.
Here's my code that I have so far for my 2 buttons that open and close the camera:
class CameraButton extends Component {
constructor(props) {
super(props);
this.state = {
showCamera: false,
};
}
showCamera = () => this.setState({showCamera: true});
hideCamera = () => this.setState({showCamera: false});
render() {
return (
<View style={styles.container}>
<Button
onPress={this.showCamera}
title="click me to open the camera!"
color="#841584"
/>
{this.state.showCamera && <DeviceCamera />}
<Button
title="click me to close the camera!"
onPress={this.hideCamera}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
export default CameraButton;
it's very simple. Would there be a way for me to have the user press the "click me to open the camera button!" and then when they camera opens, instead of having another button that closes out of the camera, I want to have users click the "click me to open the camera!" button (again) and change the state of this button if they click it again? This is more so for proof of concept so I can know for future use - I need to be able to actually record/stop recording a video within react native and I need to understand this concept.