after use withTranslation, react-native ts got errors, ref error and type error, I don’t know how to fix
"i18next": "^23.5.0", "react": "18.2.0", "react-i18next": "^13.2.2", "react-native": "0.72.4",
2
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
I think the most easiest way to solve this error would be to follow your IDE’s hint and make use of typeof
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>()
Click here to cancel reply.
2
Answers
find a way to solve the two errors while use ts and ref and class component with react-i18next withTranslation
I think the most easiest way to solve this error would be to follow your IDE’s hint and make use of
typeof
What you are trying to do is following