Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

[QUESTION] Cannot read properties of undefined (reading 'value') - i18n locales #82

Closed
belkocik opened this issue Apr 11, 2022 · 1 comment
Assignees
Labels
Question Further information is requested

Comments

@belkocik
Copy link

belkocik commented Apr 11, 2022

I'd like to change the locale: "en" and "pl" but I occured an error Cannot read properties of undefined (reading 'value')
here

  const changeLanguage = e => {
> 63 |     const locale = e.target.value
     |                            ^
  64 |     router.push(router.pathname, router.asPath, { locale })
  65 |   }

changing the locale logic

const Navbar = props => {
  const { path } = props
  const router = useRouter()
  const { locale } = router
  const t = locale === 'en' ? en : pl

  const changeLanguage = e => {
    const locale = e.target.value
    router.push(router.pathname, router.asPath, { locale })
  }

Here is my Select component

<Select
  onChange={changeLanguage}
  defaultValue={locale}
  width="74px"
  display={{ base: 'none', md: 'inline-block' }}
  variant="outline"
  cursor="pointer"
  ml="30px"
  options={[
    {
      label: 'EN',
      value: 'en'
    },
    {
      label: 'PL',
      value: 'pl'
    }
  ]}
/>

the previous one chakra's one orginal which worked

<Select
  onChange={changeLanguage}
  defaultValue={locale}
  width="74px"
  display={{ base: 'none', md: 'inline-block' }}
  variant="outline"
  cursor="pointer"
  ml="30px"
  _focus={{
    boxShadow: 'teal'
  }}
>
  <option value="en">EN</option>
  <option value="pl">PL</option>
</Select>
@belkocik belkocik added the Question Further information is requested label Apr 11, 2022
@csandman
Copy link
Owner

Unlike the Chakra UI Select which is a wrapper for the native HTML <select> element, this component doesn't return a change event (with how you're getting e.target.value). It instead just returns the object itself from your list of options. So in order for this to work right, you'll need to change your changeLanguage function like so:

/**
 * newLanguage = {
 *    label: 'EN',
 *    value: 'en'
 *  }
 */

const changeLanguage = newLanguage => {
  const locale = newLanguage.value; // <-- just .value not .target.value
  router.push(router.pathname, router.asPath, { locale })
}

Repository owner locked and limited conversation to collaborators May 5, 2022
@csandman csandman converted this issue into discussion #98 May 5, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants