i am trying to change flash mode on expo camera but nothing seems to work (react native: functional compnents)

Viewed 1141

i am trying to change flash mode on expo camera but it's not working and i can't find any documentation for that, here is some code snippets: -the hook i am using for the flash mode:

const [flash, setFlash] = useState(Camera.Constants.FlashMode.off);

-and here is the function i am using:

<TouchableWithoutFeedback style={styles.flash}
            onPress={() => {
              setFlash(
                flash === Camera.Constants.FlashMode.off
                  ? Camera.Constants.FlashMode.torch
                  : Camera.Constants.FlashMode.off);
            }}>
            <MaterialCommunityIcons
              style={styles.flash}
              name="flash"
              color={"#9c1937"}
              size={50}
            />
          </TouchableWithoutFeedback>

i cant change the flash mode using this (def: off), however changing front and back cameras is achieved using exactly the same way so please help, (i am using functional components).

2 Answers

Add the following to your camera component.

<Camera 
   flashMode={flash}
/>

Here's a basic code for toggling flash on expo-camera.

<Camera
    type={this.state.type}
    style={{flex: 1,}}
    ref={(ref) => { this.camera = ref }}
    flashMode={this.state.flashMode? Camera.Constants.FlashMode.torch : Camera.Constants.FlashMode.off}
>
    <View>
        <TouchableOpacity onPress={() => this.setState({flashMode: !this.state.flashMode})}>
            <FontAwesome
                name="flash"
                size={40}
                color={"#fff"}
            />
        </TouchableOpacity>
    </View>
</Camera>
Related