1

I have a function to take a photo with android, with Expo-Image-Picker. I save the result in a local state pickedUri, then sending it to Redux state through dispatch. The first time I get pickedUri as undefined, but the second time it saves the first photo taken. I think the problem is the async function.

const handlerTakeImage = async () => {
    const isCameraOk = await verifyPermissions();
    if (!isCameraOk) return;

    const image = await ImagePicker.launchCameraAsync({
        allowsEditing: true,
        aspect: [16, 9],
        quality: 0.8,
    });

    setPickedUri(image.uri);
    console.log(pickedUri);

    pickedUri && dispatch(saveImage(pickedUri, props.itemId));
};
Kirill Novikov
  • 2,576
  • 4
  • 20
  • 33

1 Answers1

0

I fixed it by sending to the dispatch the image.uri data directly, instead of saving it in a local state.

const handlerTakeImage = async () => {
    const isCameraOk = await verifyPermissions();
    if (!isCameraOk) return;

    const image = await ImagePicker.launchCameraAsync({
        allowsEditing: true,
        aspect: [16, 9],
        quality: 0.8,
    });

    dispatch(saveImage(image.uri, props.itemId))
};

I don't know if it's a good solution. I'm open to other options.

Kirill Novikov
  • 2,576
  • 4
  • 20
  • 33
  • setting any data to the state is a async task. useState takes time to store the data. You were storing the data in setPickedUri, and on the next line you were trying to print that state, thus it was null. – Akshay Shenoy Dec 21 '21 at 17:30