Tzvetlin Velev
Tzvetlin Velev

Reputation: 2027

Setting state using hooks resulting in null value in subsequent function call

First I call this where result is an object

const _onSaveEvent = async (result) => {
    console.log(result);
    setSignature(result);
    followUp();
};

Then I invoke,

const handleActualSubmission = async () => {
    console.log(signature);
    const { encoded, pathName } = signature;
    const extension = pathName.split('.').pop();
    const path = `${RNFS.DocumentDirectoryPath}/${uuidv4()}.${extension}`;
    await RNFS.writeFile(path, encoded, 'base64');
    stops.forEach(async (stopForPhotoConf) => {
      await saveDeliveryProof(driverRoute, stopForPhotoConf, path, extension, 'signature');
    });
    close();
  };

The resulting error is signature being null on line 1 of handleActualSubmission where const [signature, setSignature] = useState(null);

I have verifeid that the _onSaveEvent function always returns a truthy value. This issue does not appear every time this sequence of functions runs.

Upvotes: 0

Views: 44

Answers (1)

Tzvetlin Velev
Tzvetlin Velev

Reputation: 2027

This code should work:

useEffect(() => {
    if (signature) {
      processSavedSignature();
    }
  }, [signature]);

Upvotes: 1

Related Questions