title: Ionic react error on small screen devices: Cannot read properties of null in StacakManager
body: <p>I'm stuck with an error for a week now where I get an error <code>Uncaught (in Promise) TypeError: Cannot read properties of null (reading 'props') at StackManager.tsx:313</code>. I first ran into the error on mobile devices so Android and iOS devices. The application would run without a problem in the browser on a desktop device. But I then soon discovered that when I opened the developer tools in the Browser (Brave) and the screen size became smaller the error would suddenly appear as well on desktop devices. As soon as I close the developer tool or make the screen size big enough to be considered a desktop device the code would work after a refresh. Weirdly enough on Safari (MacBook Pro) it works even if you simulate an iPhone. On the iPhone if you open the website in Safari it doesn't work unless you simulate Safari (still on the phone) to be a Mac (desktop) then it works again...? So maybe there is a different JS depending on screen size?</p>
  <p>My first assumption was that it's related with <code>this</code> having a different scope. However I don't use this and only use arrow functions (no classes). I also find it very confusing that the error occurs after using a <code>useState()</code> function (see stacktrace). The closest similar issue I found: <a href="https://stackoverflow.com/questions/69108293/ionic-cannot-read-properties-of-null-reading-removechild-at-stackmanager-tra">Ionic Cannot read properties of null (reading &#39;removeChild&#39;) at StackManager.transitionPage</a> however I don't see how I would have too many rerenders from my code.</p>
  <p><strong>App.tsx</strong> (where the error originates from)</p>
  <pre><code>/* imports omitted */
  
  Amplify.configure(awsconfig);
  
  setupIonicReact();
  
  let default_user = {
      first_name: &quot;Max&quot;,
      last_name: &quot;Muster&quot;,
      email: &quot;max@muster.ch&quot;
  }
  export const UserContext = React.createContext(default_user)
  
  const App = () =&gt; {
      const [appUser, setUser] = useState(default_user);
      const [isAuthenticated, setLoggedIn] = useState(false);
  
      const rememberDevice = async () =&gt; {
          try{
              const result = await Auth.rememberDevice();
              console.log(&quot;Remembering Device:&quot;)
              console.log(result)
          }catch (error) {
              console.log('Error remembering device', error)
          }
      }
  
      const refreshAuthentication = async (bypassCache: boolean = false) =&gt; {
          let user = null;
          Auth.currentAuthenticatedUser({bypassCache: bypassCache})
              .then(user =&gt; {
                  let { attributes } = user;
                  setUser({
                      first_name: attributes.given_name,
                      last_name: attributes.family_name,
                      email: attributes.email
                  });
                  console.log(user);
                  setLoggedIn(true); /* ####### Error occurs on this line ####### */
              })
              .catch(rejected =&gt; {
                  // promise rejects if user is not authenticated.
                  // error is expected
                  console.log(&quot;Not authenticated&quot;);
                  setLoggedIn(false);
              })
      };
  
      const listener = (data :any) =&gt; {
          switch(data.payload.event){
              case 'signIn':
                  console.log(&quot;User signed in&quot;);
                  //refreshAuthentication();
                  //rememberDevice();
                  break;
              case 'signOut':
                  console.log(&quot;User signed out&quot;);
                  refreshAuthentication();
                  break;
          }
      }
  
      useEffect(() =&gt; {
          refreshAuthentication(true);
      }, []);
  
      Hub.listen('auth', listener);
      return (
          &lt;IonApp&gt;
              &lt;UserContext.Provider value={appUser}&gt;
                  { isAuthenticated ?
                      &lt;IonReactRouter&gt;
                          &lt;IonSplitPane contentId=&quot;main&quot;&gt;
                              &lt;Menu/&gt;
                              &lt;IonRouterOutlet id=&quot;main&quot;&gt;
                                  &lt;Route path=&quot;/register&quot;&gt;
                                      &lt;Register/&gt;
                                  &lt;/Route&gt;
                                  &lt;Route path=&quot;/&quot;&gt;
                                      &lt;Home/&gt;
                                  &lt;/Route&gt;
                                  &lt;Route path=&quot;/personen&quot;&gt;
                                      &lt;Personen/&gt;
                                  &lt;/Route&gt;
                                  &lt;Route path=&quot;/calendar&quot;&gt;
                                      &lt;Calendar/&gt;
                                  &lt;/Route&gt;
                                  &lt;Route path=&quot;/page/:name&quot; exact={true}&gt;
                                      &lt;Page/&gt;
                                  &lt;/Route&gt;
                                  {(isPlatform(&quot;desktop&quot;) || isPlatform(&quot;tablet&quot;) || isPlatform(&quot;ipad&quot;)) &amp;&amp;
                                      &lt;Route path=&quot;/admin&quot; exact={true}&gt;
                                          &lt;Home/&gt;
                                      &lt;/Route&gt;
                                  }
                              &lt;/IonRouterOutlet&gt;
                          &lt;/IonSplitPane&gt;
                      &lt;/IonReactRouter&gt;
                      : // not authenticated
                      &lt;Login refreshAuthentication={refreshAuthentication} setLoggedIn={setLoggedIn} rememberDevice={rememberDevice}/&gt;
                  }
              &lt;/UserContext.Provider&gt;
          &lt;/IonApp&gt;
      );
  };
  
  export default App;
  
  </code></pre>
  <p><strong>Login.tsx</strong></p>
  <pre><code>/* imports */
  
  // @ts-ignore
  const Login = ({refreshAuthentication, setLoggedIn, rememberDevice}) =&gt; {
  
      const [email, setEmail] = useState(&quot;&quot;);
      const [password, setPassword] = useState(&quot;&quot;);
      const [confirmPassword, setConfirmPassword] = useState(&quot;&quot;);
      const [confirmPassword2, setConfirmPassword2] = useState(&quot;&quot;);
      const [present, dismiss] = useIonToast();
      const [loading, doneLoading] = useIonLoading();
      const [showModal, setShowModal] = useState(false);
      const [alert] = useIonAlert();
  
      const history = useHistory();
  
      const successfulLogin = (user: any, message: string) =&gt; {
          rememberDevice();
          present(message, 3000);
          refreshAuthentication();
      }
  
      /**
       * Cognito requires the same session for password change
       * so a login attempt has to be made again and the
       * new password submitted in the callback
       */
      const completePassword = async () =&gt; {
          if (confirmPassword === confirmPassword2) {
              Auth.signIn(email,password)
                  .then(user =&gt; {
                      Auth.completeNewPassword(user, confirmPassword)
                          .then(user =&gt; {
                              // at this time the user is logged in if no MFA required
                              successfulLogin(user, &quot;Login &amp; password change successful&quot;);
                          }).catch(e =&gt; {
                              console.log(e);
                              alert(&quot;Error setting the new password&quot;);
                          });
                  }).catch(e =&gt; {
                      console.log(e);
                      alert(&quot;Error fetching current user&quot;);
              });
          } else {
              alert(&quot;Passwords do not match&quot;, [{text:&quot;Ok&quot;}]);
          }
      }
  
      const handleSubmit = async () =&gt; {
          await loading(&quot;Logging you in...&quot;);
  
          Auth.signIn(email, password)
          .then(user =&gt; {
              doneLoading();
              console.log(&quot;User signed in:&quot;);
              console.log(user);
              if (user.challengeName === 'NEW_PASSWORD_REQUIRED') {
                  setShowModal(true);
              } else {
                  // other situations
                  successfulLogin(user, &quot;Login Successful&quot;);
              }
          }).catch(e =&gt; {
              doneLoading();
              console.log(e);
              alert(&quot;Error logging you in: &quot; + e.message);
          }).finally(() =&gt; {
              doneLoading();
          });
      };
  
      return (
          &lt;IonPage&gt;
              &lt;IonContent className=&quot;ion-padding&quot;&gt;
                  &lt;IonGrid&gt;
                      &lt;IonRow&gt;
                          &lt;IonCol&gt;
                              &lt;IonItem&gt;
                                  &lt;IonLabel&gt;Email&lt;/IonLabel&gt;
                                      &lt;IonInput placeholder=&quot;name@email.com&quot; type=&quot;email&quot; onIonChange={(e: any) =&gt; {setEmail(e.target.value)}} /&gt;
                                  &lt;/IonItem&gt;
                                  &lt;IonItem&gt;
                                      &lt;IonLabel&gt;Password&lt;/IonLabel&gt;
                                      &lt;IonInput placeholder=&quot;&quot; type=&quot;password&quot; onIonChange={(e: any) =&gt; {setPassword(e.target.value)}} /&gt;
                                  &lt;/IonItem&gt;
                                  &lt;IonButton onClick={handleSubmit} color=&quot;primary&quot; expand=&quot;block&quot; size=&quot;large&quot; type=&quot;submit&quot;&gt;Login&lt;/IonButton&gt;
                          &lt;/IonCol&gt;
                      &lt;/IonRow&gt;
                  &lt;/IonGrid&gt;
                  &lt;IonModal isOpen={showModal} className=&quot;ion-padding&quot;&gt;
                      &lt;IonToolbar&gt;
                          &lt;IonTitle&gt;Configuration&lt;/IonTitle&gt;
                          &lt;IonButtons slot=&quot;end&quot;&gt;
                              &lt;IonButton onClick={() =&gt; setShowModal(false)}&gt;Cancel&lt;/IonButton&gt;
                          &lt;/IonButtons&gt;
                      &lt;/IonToolbar&gt;
                      &lt;IonContent&gt;
                          &lt;h2 className=&quot;ion-padding&quot;&gt;New password required&lt;/h2&gt;
                          &lt;p className=&quot;ion-padding&quot;&gt;You have to provide a new password for your account&lt;/p&gt;
                          &lt;IonItem&gt;
                              &lt;IonLabel&gt;Password&lt;/IonLabel&gt;
                              &lt;IonInput placeholder=&quot;&quot; type=&quot;password&quot; onIonChange={(e: any) =&gt; {setConfirmPassword(e.target.value)}} /&gt;
                          &lt;/IonItem&gt;
                          &lt;IonItem&gt;
                              &lt;IonLabel&gt;Confirm Password&lt;/IonLabel&gt;
                              &lt;IonInput placeholder=&quot;&quot; type=&quot;password&quot; onIonChange={(e: any) =&gt; {setConfirmPassword2(e.target.value)}} /&gt;
                          &lt;/IonItem&gt;
                          &lt;IonButton onClick={() =&gt; completePassword()} color=&quot;primary&quot; expand=&quot;block&quot; size=&quot;large&quot; type=&quot;submit&quot;&gt;Save&lt;/IonButton&gt;
                      &lt;/IonContent&gt;
                  &lt;/IonModal&gt;
              &lt;/IonContent&gt;
          &lt;/IonPage&gt;
      );
  }
  
  export default Login;
  </code></pre>
  <p>Stacktrace of the error:
  <a href="https://i.stack.imgur.com/Yljp4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yljp4.png" alt="Stacktrace" /></a></p>
  <p>StackManager.tsx:313
  <a href="https://i.stack.imgur.com/rLG6i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rLG6i.png" alt="StackManager.tsx:313" /></a></p>