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

pass t useI18n hook inside a another component #43

Closed
chaosLegacy opened this issue Nov 8, 2022 · 2 comments
Closed

pass t useI18n hook inside a another component #43

chaosLegacy opened this issue Nov 8, 2022 · 2 comments

Comments

@chaosLegacy
Copy link

chaosLegacy commented Nov 8, 2022

I would like to know if there's an easy way to pass {t} as a props inside another components
hooks don't work inside loops so I need a way to find the type of t in order to pass it as prop in my other component
here an example:

const { scopedT } = useI18n();
const t = scopedT('table')
buildTableHeader(t);

export const buildHeadIncidents = (t: //What type should I put here): HeadCellItem[] => {
    // const { t } = useI18n(); // I can't call the hook here
    return [
        {
            name: 'title1',
            label: t('mykey'),
        },
        {
            name: 'title2',
            label: t('mykey'),
        },
        {
            name: 'title3',
            label: t('mykey'),
        }
    ];
};

Here is definition of t and scopedT used by next-international

    useI18n: () => {
        t: {
            <Key extends Extract<keyof Locale, string>, Value extends international_types.LocaleValue = Locale[Key]>(key: Key, ...params: international_types.Params<Value>["length"] extends 0 ? [] : [international_types.ParamsObject<Value>]): string;
            <Key_1 extends Extract<keyof Locale, string>, Value_1 extends international_types.LocaleValue = Locale[Key_1]>(key: Key_1, ...params: international_types.Params<Value_1>["length"] extends 0 ? [] : [ReactParamsObject<Value_1>]): React$1.ReactNode;
        };
        scopedT: <Scope extends international_types.Scopes<Locale>>(scope: Scope) => {
            <Key_2 extends international_types.LocaleKeys<Locale, Scope, Extract<keyof Locale, string>>, Value_2 extends international_types.LocaleValue = international_types.ScopedValue<Locale, Scope, Key_2>>(key: Key_2, ...params: international_types.Params<Value_2>["length"] extends 0 ? [] : [international_types.ParamsObject<Value_2>]): string;
            <Key_3 extends international_types.LocaleKeys<Locale, Scope, Extract<keyof Locale, string>>, Value_3 extends international_types.LocaleValue = international_types.ScopedValue<Locale, Scope, Key_3>>(key: Key_3, ...params: international_types.Params<Value_3>["length"] extends 0 ? [] : [ReactParamsObject<Value_3>]): React$1.ReactNode;
        };
    };
@QuiiBz
Copy link
Owner

QuiiBz commented Nov 8, 2022

You should be able to get the type of t or scopedT like this:

type T = ReturnType<typeof useI18n>['t']

But I'm not sure how that would play with scopedT. The easiest way might be to use international-types directly, something like:

// Locale is the type of your locale, same when using createI18n
// 'table' is the scope
export const buildHeadIncidents = (t: (key: LocaleKeys<typeof Locale, 'table'>) => string): HeadCellItem[] => {
    return [
        {
            name: 'title1',
            label: t('mykey'),
        },
        {
            name: 'title2',
            label: t('mykey'),
        },
        {
            name: 'title3',
            label: t('mykey'),
        }
    ];
};

@chaosLegacy
Copy link
Author

Amazing thank you so much 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants