title: initialprops not works on first run, the dynamic routing also strange in the ui but it is correct inside the console log
body: <p>I have written a simple nextjs with next-i18next as i18n library. I have used dynamic routing by putting the locale inside the path as /pages/mysite/mysubsite/[locale]/section/. I do not use the  localeSubpaths because I would like the fix the path as above instead of putting the locale at the end of the path.</p>
  
  <p>The main program is like this</p>
  
  <p>/pages/mysite/mysubsite/[locale]/section/index.js</p>
  
  <pre><code>import { useRouter } from 'next/router';
  import PropTypes from 'prop-types'
  
  import { i18n, Link, withTranslation } from '../../../../../i18n'
  
  const Index = ({isMobileView, t}) =&gt; {
      const router = useRouter();
      const { locale } = router.query;
      if (i18n.language !== locale) {
          console.log('switching locale from ' + i18n.language + ' to ' + locale);
          i18n.changeLanguage(locale);
      }
      if (isMobileView) {
          return (
              &lt;div&gt; {t('mobile')}
                  {locale}
              &lt;/div&gt;
          )
      } else {
          return (
              &lt;div&gt; {t('desktop')}
                  {locale}
              &lt;/div&gt;
          )
      }
  };
  
  Index.getInitialProps = async function (context) {
      let isMobileView = (context.req
          ? context.req.headers['user-agent']
          : navigator.userAgent).match(
          /Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i
      )
  
      //Returning the isMobileView as a prop to the component for further use.
      return {
          isMobileView: Boolean(isMobileView),
          namespacesRequired: ['items'],
      }
  }
  
  Index.propTypes = {
      t: PropTypes.func.isRequired,
  }
  
  export default withTranslation('items')(Index);
  </code></pre>
  
  <p>the /pages/i18n is like this:-</p>
  
  <pre><code>const NextI18Next = require('next-i18next').default;
  
  module.exports = new NextI18Next({
      defaultLanguage: 'en',
      otherLanguages: ['tw'],
  });
  </code></pre>
  
  <p>and I have stored the json items.json two keys only, the tw one below:-</p>
  
  <pre><code>{
    "mobile": "手機",
    "desktop": "桌面"
  }
  </code></pre>
  
  <p>and the end one below:-</p>
  
  <pre><code>{
    "mobile": "Mobile",
    "desktop": "Desktop"
  }
  </code></pre>
  
  <p>I found that when the program first run, the result would be like:-</p>
  
  <blockquote>
    <p>desktopopen</p>
  </blockquote>
  
  <p>and from the chrome consolelog it is printing:-</p>
  
  <blockquote>
    <p>switching locale from undefined to en</p>
  </blockquote>
  
  <p>And the server consolelog looks simular:-</p>
  
  <blockquote>
    <p>react-i18next:: i18n.languages were undefined or empty undefined
    switching locale from undefined to en</p>
  </blockquote>
  
  <p>That means I have changed the local using i18n.changeLanguage(locale) and the locale itself is correct.</p>
  
  <p>But I m pretty lost. Why is the locale in the UI is open whereas I can print the correct locale from the console log?</p>
  
  <p>Another strange thing is that I have opened the console of chrome and also switched to responsive mobile view. That means the mobile agent detection also doesn't work in the first run. Then I pressed shift F5 to refresh the page. And the UI turns:-</p>
  
  <blockquote>
    <p>mobileen</p>
  </blockquote>
  
  <p>and console log doesn't print anything.</p>
  
  <p>Again, it is printing the key instead of the value of the json while the mobile detection is correct this time.</p>
  
  <p>The things would goes normal when I press the shift F5 the third time. And the correct UI is shown:-</p>
  
  <blockquote>
    <p>Mobileen</p>
  </blockquote>
  
  <p>During the whole debugging exercise, I have tried to close everything (browser and the node process) and restart again and again to observe the behavior, while it is kind of strange that some time desktopopen will not happen while it will be like mobileen instead. That means whenever desktopopen is there, the mobile detection doesn't work as well. I really cannot cache the behavior.</p>
  
  <p>The version of libraries using are as below:-</p>
  
  <pre><code>  "dependencies": {
      "next": "^9.1.6",
      "react": "^16.12.0",
      "react-dom": "^16.12.0",
      "@quentin-sommer/react-useragent": "^3.1.0",
      "next-i18next": "^3.0.1"
    }
  </code></pre>
  
  <p>Also the /pages/_app.js are as below:-</p>
  
  <pre><code>import React from 'react'
  import App from 'next/app'
  import { appWithTranslation } from '../i18n'
  
  class MyApp extends App {
      render() {
          const { Component, pageProps } = this.props
          return (
              &lt;Component {...pageProps} /&gt;
          )
      }
  }
  
  export default appWithTranslation(MyApp)
  </code></pre>
  
  <p>I am suspecting it is bcoz the problem is relating to state management and have tried switch using react hooks by modifying the main program as:-</p>
  
  <pre><code>import React, { useState } from 'react';
  import { useRouter } from 'next/router';
  import PropTypes from 'prop-types'
  
  import { i18n, Link, withTranslation } from '../../../../../i18n'
  
  const Index = ({isMobileView, t}) =&gt; {
      const router = useRouter();
      const [ locale ] = useState(router.query.locale);
      if (i18n.language !== locale) {
          console.log('switching locale from ' + i18n.language + ' to ' + locale);
          i18n.changeLanguage(locale);
      }
      if (isMobileView) {
          return (
              &lt;div&gt; {t('mobile')}
                  {locale}
              &lt;/div&gt;
          )
      } else {
          return (
              &lt;div&gt; {t('desktop')}
                  {locale}
              &lt;/div&gt;
          )
      }
  };
  
  Index.getInitialProps = async function (context) {
      let isMobileView = (context.req
          ? context.req.headers['user-agent']
          : navigator.userAgent).match(
          /Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i
      )
  
      //Returning the isMobileView as a prop to the component for further use.
      return {
          isMobileView: Boolean(isMobileView),
          namespacesRequired: ['items'],
      }
  }
  
  Index.propTypes = {
      t: PropTypes.func.isRequired,
  }
  
  export default withTranslation('items')(Index);
  </code></pre>
  
  <p>But the problem is almost the same. The only difference is I cannot cache the moment where the output is "desktopopen".</p>
  
  <p>My question can be summarized as:-</p>
  
  <ol>
  <li>Why is the mobile detection not working on the first run some of the time?</li>
  <li>Why the dynamic path variable is not correctly printing in the UI but it does work in the console log on the first run some of the time?</li>
  <li>Why is the defaultlanguage not working even I have specify the defaultLanguage in the i18n.js?</li>
  <li>Is changing language success or not? Why it is printing the key of the json instead of the json value? I would assume the change language is not successful, right? Is it that I should take a look in the react hook? Coz I suspect it is a matter of state problem. But why is the problem persist even after I have deployed react hook? Does that means it is not state management problem?</li>
  </ol>