Happy bean
Happy bean

Reputation: 89

How to use i18n translations as a key for an object

I am working in react js . I need to dynamically use translation values as a key for an object . How can I do that. Below show the way I tried , but I am sure this is not the right way. Can anyone suggest me a better way for this purpose


    const initialValues = {
                i18n.t("name.drawing"): {
                items: [],
                startDate: null,
                endDate: null,
                
            },
            i18n.t("name.dancing"): {
                items: [],
                startDate: null,
                endDate: null,
            },
        };

Upvotes: 2

Views: 2039

Answers (1)

larz
larz

Reputation: 5766

Wrap the keys in brackets -

    const initialValues = {
                [i18n.t("name.drawing")]: {
                items: [],
                startDate: null,
                endDate: null,
                
            },
            [i18n.t("name.dancing")]: {
                items: [],
                startDate: null,
                endDate: null,
            },
        };

Upvotes: 3

Related Questions