skip to Main Content

after use withTranslation, react-native ts got errors, ref error and type error, I don’t know how to fix

enter image description here

enter image description here

    "i18next": "^23.5.0",
    "react": "18.2.0",
    "react-i18next": "^13.2.2",
    "react-native": "0.72.4",

2

Answers


  1. Chosen as BEST ANSWER

    find a way to solve the two errors while use ts and ref and class component with react-i18next withTranslation

    datePickerRef: React.RefObject<DatePickerRef> = React.createRef<DatePickerRef>();
    
    //region withTranslation with ts and ref
    //https://github.com/i18next/react-i18next/blob/fdab4e8953b2cdaf29d21d716d33ebfd0eca99f2/src/withTranslation.js#L5
    //https://stackoverflow.com/questions/77677338/react-i18next-use-withtranslation-got-type-and-ref-error-i-dont-how-to-fix
    //https://github.com/i18next/react-i18next/issues/1122#issuecomment-1859634674
    export interface DatePickerRef extends DatePicker {}
    export default withTranslation(undefined, { withRef: true })(DatePicker) as React.ForwardRefExoticComponent<
      Omit<DatePickerProps, keyof WithTranslation> & RefAttributes<DatePickerRef>
    >;
    //endregion
    

  2. I think the most easiest way to solve this error would be to follow your IDE’s hint and make use of typeof

    datePicker: React.RefObject<typeof DatePicker> = React.createRef<typeof DatePicker>()
    

    What you are trying to do is following

    const MyObject = {
     name: "Joe Doe",
     age: "99"
    }
    
    //Wrong
    React.RefObject<MyObject> = React.createRef<MyObject>()
    
    //Correct
    React.RefObject<typeof MyObject> = React.createRef<typeof MyObject>()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search