Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to use ref #90

Open
vksgautam1986 opened this issue Dec 27, 2024 · 3 comments
Open

trying to use ref #90

vksgautam1986 opened this issue Dec 27, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@vksgautam1986
Copy link

vksgautam1986 commented Dec 27, 2024

Describe the bug
A clear and concise description of what the bug is.
i am trying to use ref to setvalue but its giving null. otpInputRef.current its giving null

Expected behavior
can use ref to setvalue

Environment (please complete the following information):

  • OS: mac os
  • Device OS: IOS
  • Device version:17.6
  • Library version: latest

Code `
const OTPVerificationModal =forwardRef((props, ref) => {
const { showOtpVerificationModal, hideOtpVerificationModal, onChangeTextOtp, onSubmitOtp }=props;
const otpInputRef = useRef(null); // Explicitly type the ref

// Expose methods through ref
React.useImperativeHandle(ref, () => ({
  setValueOtp: (newValue: string) => {
    console.log("otpInputRefotpInputRef ", otpInputRef)
    otpInputRef.current?.setValue('123456')
  },
  // Add other methods you want to expose
}));

return (
    <Modal statusBarTranslucent={true} animationType="slide" transparent={true} visible={showOtpVerificationModal} onRequestClose={() => { }} style={styles.centeredView}>
        <View style={styles.centeredView}>
            <View style={styles.parentView}>
                <View style={styles.bodyView}>
                    <Text style={[styles.text, styles.margin]}>{i18next.t('otpVerification')}</Text>
                    <TouchableOpacity
                        style={[styles.cross, styles.margin]}
                        onPress={hideOtpVerificationModal}>
                        <Image
                            style={{
                                width: moderateScale(18),
                                height: moderateScale(18),
                                tintColor: colors.gray,
                            }}
                            source={Images.cross}
                        />
                    </TouchableOpacity>
                </View>
                <Text style={{ fontWeight: '600', color: colors.gray }}>{i18next.t('enterOTP')}</Text>
                <View style={styles.base}>                       
                    <View style={{ flexDirection: 'row', justifyContent: 'space-evenly', marginTop: 5 }}>                            
                        <OtpInput ref={otpInputRef} theme={{pinCodeContainerStyle:{backgroundColor:'#F1F1F1'}, focusedPinCodeContainerStyle:{borderColor:colors.appColor}}}type="numeric" numberOfDigits={6} onTextChange={(text) => onChangeTextOtp(text)} />
                    </View>
                    <View >
                        <TouchableOpacity style={[styles.button,{marginTop:30}]} onPress={onSubmitOtp}>
                            <Text style={styles.laterStyle}>{i18next.t('verifyProceed')}</Text>
                        </TouchableOpacity>
                    </View>
                </View>                   
            </View>
        </View>
    </Modal>
)

})

`

@vksgautam1986 vksgautam1986 added the bug Something isn't working label Dec 27, 2024
@vksgautam1986
Copy link
Author

Screenshot 2024-12-27 at 1 33 18 PM

@vksgautam1986
Copy link
Author

now i defined this modal inside the main component .
this is how it shows and if if i try to access its showing null
console.log("this.otpRef.current", this.otpRef)
console.log( this.otpRef.current)
Screenshot 2024-12-27 at 2 43 00 PM

@anday013
Copy link
Owner

From the first sight, it makes me think that the issue is in the initialization time of the component. As the modal is not rendered straight away on the screen, setting reference skips first renders. So it becomes available in the later renders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants