tttstudios / react-native-otp-input

Tiny Javascript library which provides an elegant UI for user to input one time passcode.
MIT License
531 stars 241 forks source link

Android OTP copied from message could not setState #29

Closed vaibsshukla closed 4 years ago

vaibsshukla commented 4 years ago

Copied OTP codefrom message could not able to setState(code)

To Reproduce Steps To produce:

  1. Entered Contact no.
  2. received OTP
  3. Received OTP
  4. on click Button Send OTP the OTP does not set in a state.

Expected behavior On Send button I need OTP copied OTP on the state.

Smartphone (please complete the following information): Android Devices

Code:

export default class Verification extends Component<Props> {
    constructor(props) {
        super(props)
        s = Strings()
        this.state = {
            loading: false,
            code : '',
        }
    }

render(){
<View>
  <OTPInputView
                                        style={{ width: '60%', height: 50, alignItems: 'center' }}
                                        pinCount={6}
                                        // code=""

                                        code={this.state.code} //You can supply this prop or not. The component will be used as a controlled / uncontrolled component respectively.
                                        onCodeChanged = {code => { this.setState({code},()=>alert(code))}}
                                        codeInputFieldStyle={styles.underlineStyleBase}
                                        codeInputHighlightStyle={styles.underlineStyleHighLighted}
                                        onCodeFilled={(code => {
                                            code => { this.setState({code})}
                                            alert('code Alert'+code)
                                            // alert(`Code is ${code}, you are good to go!`)
                                            // setTimeout(() => {
                                            //     // this.hitVerifyMobileApiCall(code);
                                            // }, 200);
                                        })}
                                    />
                                </View>
                            <View style={{ marginTop: (d.height*0.036), alignItems: 'center', }}>
                                <AppButton
                                    containerStyle={[styles.submit, { backgroundColor: c.cinnabar }]}
                                    textStyle={{fontSize: d.height < 600 ? 14 : 16, color: "white" }}
                                    value={s.send}
                                    onPress={()=>{
                                                 this.hitVerifyMobileApiCall(this.state.code);

                                    }}
                                    />
                            </View>
</View>
}
rrust commented 4 years ago

This works for me:

<OTPInputView
 pinCount={6}
 onCodeChanged={(code) => {
   if (code.length === 6) {
    this._handleVerificationCode(code)
   }
  }}
  onCodeFilled={this._handleVerificationCode}
  />
BeckyWu220 commented 4 years ago

Thanks @rrust for providing the walk around. This issue should had been fixed in PR https://github.com/Twotalltotems/react-native-otp-input/pull/58.