0

I have a function which I will generate SHA256 signature. But I cannot bring out the value from it.

Function Property:

  timer = null
  ScanCode_Change_Event = (e) => {
    clearTimeout(this.timer);
    this.timer = setTimeout(() => { this.triggerChange() }, 500);
  }
  triggerChange = async () => {
    await this._onSendSignature();
  }

_onSendSignature = async () => {
    const BarcodeNo = this.state.BarcodeNo;
    const valueToHash = MerchantKey + BarcodeNo + Amount.replace('.', '').replace(',', '');
    sha256(valueToHash).then( hash => {
        // This can show the signature
        ToastAndroid.show('Signature: ' + hash, ToastAndroid.LONG, ToastAndroid.BOTTOM);
        this.setState({
          hashSignature: hash
        })
      })
    // This cannot show the signature
    ToastAndroid.show('Signature: ' + this.state.hashSignature, ToastAndroid.LONG, ToastAndroid.BOTTOM);
}

Render:

<Input style={{ color: 'brown' }}
                    placeholder='Scan Barcode'
                    placeholderTextColor='rgba(0, 0, 0, 0.3)'
                    autoFocus={true}
                    onChange={evt => this.ScanCode_Change_Event(evt)}
                    onChangeText={(BarcodeNo) => this.setState({ BarcodeNo})}>{this.state.BarcodeNo}</Input>

I'm currently lost of what is happening. BarcodeNo can get the value in this.state but .

I have try to add this._onSendSignature = this._onSendSignature.bind(this); on constructor. Do I missed something?

Luiey
  • 843
  • 2
  • 23
  • 50
  • You need to move the `ToastAndroid.show` call into the completion callback of the `setState` inside the `then` callback. See the first two questions linked above ([here](https://stackoverflow.com/questions/14220321/), [here](https://stackoverflow.com/questions/23667086/)) for why you have to do it inside the `then` callback, and the third question linked above ([here](https://stackoverflow.com/questions/41278385/setstate-doesnt-update-the-state-immediately)) for why it has to be in the completion callback of `setState`. (Ignore the answer there [currently the accepted one] using `await`.) – T.J. Crowder Aug 19 '19 at 09:09
  • the sha256 is the module I install from https://www.npmjs.com/package/react-native-sha256 and I have followed from https://aboutreact.com/generate-sha256-encoded-hash-in-react-native/. – Luiey Aug 19 '19 at 09:13
  • Please, again, review the answers to the first two linked questions, in particular [the first one](https://stackoverflow.com/questions/14220321/) addresses *"But I cannot bring out the value from it."* (though it's not clear to me why you want to, just use the code you have that works [inside `then`]). Your `setState` call will also work, it just (see [the third question](https://stackoverflow.com/questions/41278385/setstate-doesnt-update-the-state-immediately)) is asynchronous. – T.J. Crowder Aug 19 '19 at 09:28
  • why you assign to time? `this.timer = setTimeout(() => { this.triggerChange() }, 500);` – Idan Aug 19 '19 at 10:13
  • @Idan the timer is for textinput. User request not to have button to proceed. After I scan barcode, it should automatically perform it's job. I'm using Honeywell Android Smartphone comes with barcode scanner. Before this I use react-native-camerakit and not have problem to get after scanner done event. So, I cannot control the hardware. What I can do currently is check the after last key in textinput, the function will be trigger to perform the function to proceed. If new char is add (onChanged), it will reset the timer back. That one I get from another SO question. – Luiey Aug 20 '19 at 03:25
  • remove the assign to the timer and check if the problem solved if yes tell me and we get way to put in the timer again – Idan Aug 20 '19 at 10:30

0 Answers0