title: getState() in redux giving different userId in react native
body: <p>In my app, I am trying to add a new product that uses getState().userId as a key so that If I use the same email I will see that added product in my admin section but sometimes, I am not able to add product, and my app giving me an error saying: <code>VirtualizedList: missing keys for items, make sure to specify a key or id property on each item or provide a custom keyExtractor</code>.</p>
  <p>So, for debugging purposes, I checked the getState.userId I am getting and I get different userId from redux that's why I can't see my product in my admin area as I have added their <code>ownerId===userId</code>.</p>
  <p>But, After reloading or refreshing the app, I am able to see my product there and also able to add product with the correct userId on firebase.</p>
  <p>store/action/Auth.js:</p>
  <pre><code>import { AsyncStorage } from &quot;react-native&quot;;
  
  let timer;
  
  export const AUTHENTICATE = &quot;AUTHENTICATE&quot;;
  export const LOGOUT = &quot;LOGOUT&quot;;
  
  export const Authenticate = (userId, token, expiryTime) =&gt; {
    return (dispatch) =&gt; {
      dispatch(setLogoutTimer(expiryTime));
      dispatch({ type: AUTHENTICATE, userId: userId, token: token });
    };
  };
  
  export const signup = (email, password) =&gt; {
    return async (dispatch) =&gt; {
      const response = await fetch(
        &quot;https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyAnaJyjjGppk9-FiWocva_cP0vaMrKp4_4&quot;,
        {
          method: &quot;POST&quot;,
          headers: {
            &quot;Content-Type&quot;: &quot;application/json&quot;,
          },
          body: JSON.stringify({
            email: email,
            password: password,
            returnSecureToken: true,
          }),
        }
      );
      if (!response.ok) {
        const responseData = await response.json();
        const errorMessage = responseData.error.message;
        if (errorMessage === &quot;EMAIL_EXISTS&quot;) {
          throw new Error(&quot;Email already exists&quot;);
        }
      }
      const resData = await response.json();
      console.log(resData);
      dispatch(
        Authenticate(
          resData.idToken,
          resData.localId,
          parseInt(resData.expiresIn) * 1000
        )
      );
      const expirationDate = new Date(
        new Date().getTime() + parseInt(resData.expiresIn) * 1000
      );
      saveDataToStorage(resData.idToken, resData.localId, expirationDate);
    };
  };
  
  export const login = (email, password) =&gt; {
    return async (dispatch) =&gt; {
      const response = await fetch(
        &quot;https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyAnaJyjjGppk9-FiWocva_cP0vaMrKp4_4&quot;,
        {
          method: &quot;POST&quot;,
          headers: {
            &quot;Content-Type&quot;: &quot;application/json&quot;,
          },
          body: JSON.stringify({
            email: email,
            password: password,
            returnSecureToken: true,
          }),
        }
      );
      if (!response.ok) {
        const responseData = await response.json();
        const errorMessage = responseData.error.message;
        if (
          errorMessage === &quot;EMAIL_NOT_FOUND&quot; ||
          errorMessage === &quot;INVALID_PASSWORD&quot;
        ) {
          throw new Error(&quot;Email or Password is wrong!!&quot;);
        }
      }
      const resData = await response.json();
      console.log(resData);
      dispatch(
        Authenticate(
          resData.idToken,
          resData.localId,
          parseInt(resData.expiresIn) * 1000
        )
      );
      const expirtationDate = new Date(
        new Date().getTime() + parseInt(resData.expiresIn) * 1000
      );
      saveDataToStorage(resData.idToken, resData.localId, expirtationDate);
    };
  };
  
  export const logout = () =&gt; {
    clearLogoutTimer();
    AsyncStorage.removeItem(&quot;userData&quot;);
    return { type: LOGOUT };
  };
  
  const clearLogoutTimer = () =&gt; {
    if (timer) {
      clearTimeout(timer);
    }
  };
  
  const setLogoutTimer = (expirationTime) =&gt; {
    return (dispatch) =&gt; {
      timer = setTimeout(() =&gt; {
        dispatch(logout());
      }, expirationTime);
    };
  };
  
  const saveDataToStorage = (token, userId, expirtationDate) =&gt; {
    AsyncStorage.setItem(
      &quot;userData&quot;,
      JSON.stringify({
        token: token,
        userId: userId,
        expiryDate: expirtationDate.toISOString(),
      })
    );
  };
  
  </code></pre>
  <p>store/action/product.js:</p>
  <pre><code>import { createAsyncThunk } from &quot;@reduxjs/toolkit&quot;;
  
  import Product from &quot;../../models/Product&quot;;
  
  export const CREATE_PRODUCT = &quot;CREATE_PRODUCT&quot;;
  export const SET_PRODUCT = &quot;SET_PRODUCT&quot;;
  
  export const fetchProducts = () =&gt; {
    return async (dispatch, getState) =&gt; {
      const userId = getState().Auth.userId;
      try {
        const response = await fetch(
          &quot;https://shopping-app-62e38-default-rtdb.firebaseio.com/products.json&quot;
        );
  
        if (!response.ok) {
          throw new Error(&quot;Something went wrong!&quot;);
        }
  
        const resData = await response.json();
        const loadedProducts = [];
  
        for (const key in resData) {
          loadedProducts.push(
            new Product(
              key,
              resData[key].ownerId,
              resData[key].title,
              resData[key].imageUrl,
              resData[key].description,
              resData[key].price
            )
          );
        }
        dispatch({
          type: SET_PRODUCT,
          products: loadedProducts,
          userProducts: loadedProducts.filter((prod) =&gt; prod.ownerId === userId),
        });
      } catch (error) {
        throw error;
      }
    };
  };
  
  
  export const createProduct = (title, description, imageUrl, price) =&gt; {
    return async (dispatch, getState) =&gt; {
      const token = getState().Auth.token;
      const userId = getState().Auth.userId;
      console.log(getState());
      const response = await fetch(
        `https://shopping-app-62e38-default-rtdb.firebaseio.com/products.json?auth=${token}`,
        {
          method: &quot;POST&quot;,
          headers: {
            &quot;Content-Type&quot;: &quot;application/json&quot;,
          },
          body: JSON.stringify({
            title,
            description,
            imageUrl,
            price,
            ownerId: userId,
          }),
        }
      );
      const resData = await response.json();
      console.log(userId);
  
      dispatch({
        type: CREATE_PRODUCT,
        productData: {
          id: resData.name,
          title,
          description,
          imageUrl,
          price,
          ownerId: userId,
        },
      });
    };
  };
  </code></pre>
  <p>I tried adding a product. But I got same error mentioned above.</p>
  <p>console.log()- getState() :</p>
  <pre><code>&quot;Auth&quot;: Object {
      &quot;token&quot;: &quot;9WwCUWtUeDaeHPHfURuBMSAaT553&quot;,
      &quot;userId&quot;: &quot;eyJhbGciOiJSUzI1NiIsImtpZCI6IjFiYjk2MDVjMzZlOThlMzAxMTdhNjk1MTc1NjkzODY4MzAyMDJiMmQiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vc2hvcHBpbmctYXBwLTYyZTM4IiwiYXVkIjoic2hvcHBpbmctYXBwLTYyZTM4IiwiYXV0aF90aW1lIjoxNjI3MDY5NTM1LCJ1c2VyX2lkIjoiOVd3Q1VXdFVlRGFlSFBIZlVSdUJNU0FhVDU1MyIsInN1YiI6IjlXd0NVV3RVZURhZUhQSGZVUnVCTVNBYVQ1NTMiLCJpYXQiOjE2MjcwNjk1MzUsImV4cCI6MTYyNzA3MzEzNSwiZW1haWwiOiJ0ZXN0QGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJlbWFpbCI6WyJ0ZXN0QGdtYWlsLmNvbSJdfSwic2lnbl9pbl9wcm92aWRlciI6InBhc3N3b3JkIn19.Yq2CjY7anrwuQSt2N5nMSa3vUKh1gFiIuyO1Mw4-WQwbf_y9b9IYOHSG1YkXgFoyONOV71DOlT9JmWzGu9g2aFSKKUU3LffTY5WNQd5-Qm9QdbhHB0CtOAPJZ124FKiLLS0wJh0nVZKez8_6dH2ICOCw3e-lyktHOc6R11s_9xhEF1LLYv20b7BziWy6T_VHZOcUsiJbBwn09lx1SKkK4Ic3iEsWNeg-lSAgPJGO-sClUmwW9ffA3L1p8OvZubSNirKcGbRYkC323wXnlWv-tqqj6CW-uNc448i6qsmrs2RRR_f4WdLFN9dvv_PBjsf7V8r0m_HY4jjDzScNLLA4Hg&quot;,
    },
    &quot;cart&quot;: Object {
      &quot;items&quot;: Object {},
      &quot;totalAmount&quot;: 0,
    },
    &quot;orders&quot;: Object {
      &quot;orders&quot;: Array [],
    },
    &quot;products&quot;: Object {
      &quot;availableProducts&quot;: Array [
        Product {
          &quot;description&quot;: &quot;Apple Macbook Air M1 chip&quot;,
          &quot;id&quot;: &quot;-Md76UnJ1I1lNhGgldH3&quot;,
          &quot;imageUrl&quot;: &quot;https://www.zdnet.com/a/hub/i/r/2020/11/23/77824151-c578-4431-8f00-633b1b7c0b99/thumbnail/770x433/3b5a2366cdeef2dc08730041abdcf426/macbook-air-m1-header.jpg&quot;,
          &quot;ownerId&quot;: undefined,
          &quot;price&quot;: 1200,
          &quot;title&quot;: &quot;Macbook Air M1 2020&quot;,
        },
        Product {
          &quot;description&quot;: &quot;Apple · iPhone 64GB Blue· iPhone 12 mini · iOS · 5.4 inches screen · Facial Recognition · 12 MP Front Camera · 12 MP Rear Camera · Smartphone · Wireless Charging&quot;,
          &quot;id&quot;: &quot;-Mekel4gfFtgNUviidau&quot;,
          &quot;imageUrl&quot;: &quot;https://encrypted-tbn3.gstatic.com/shopping?q=tbn:ANd9GcS_WPpNagW5q_-KAep1QkiInHfUYADs8qv84Z_FqKwMmW-J7nleMbl5y6muot5dxxRCOBZ-S_dYxxp_QpmTqzSnTj9oKQuH84kSCYjAZXT6UIsrnWJowbMMLw&amp;usqp=CAE&quot;,
          &quot;ownerId&quot;: &quot;9WwCUWtUeDaeHPHfURuBMSAaT553&quot;,
          &quot;price&quot;: 956.99,
          &quot;title&quot;: &quot;Apple iPhone 12 Mini&quot;,
        },
      ],
      &quot;userProducts&quot;: Array [],
    },
  }
  
  </code></pre>
  <p>console.log()-userID:</p>
  <pre><code>eyJhbGciOiJSUzI1NiIsImtpZCI6IjFiYjk2MDVjMzZlOThlMzAxMTdhNjk1MTc1NjkzODY4MzAyMDJiMmQiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vc2hvcHBpbmctYXBwLTYyZTM4IiwiYXVkIjoic2hvcHBpbmctYXBwLTYyZTM4IiwiYXV0aF90aW1lIjoxNjI3MDY5NTM1LCJ1c2VyX2lkIjoiOVd3Q1VXdFVlRGFlSFBIZlVSdUJNU0FhVDU1MyIsInN1YiI6IjlXd0NVV3RVZURhZUhQSGZVUnVCTVNBYVQ1NTMiLCJpYXQiOjE2MjcwNjk1MzUsImV4cCI6MTYyNzA3MzEzNSwiZW1haWwiOiJ0ZXN0QGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJlbWFpbCI6WyJ0ZXN0QGdtYWlsLmNvbSJdfSwic2lnbl9pbl9wcm92aWRlciI6InBhc3N3b3JkIn19.Yq2CjY7anrwuQSt2N5nMSa3vUKh1gFiIuyO1Mw4-WQwbf_y9b9IYOHSG1YkXgFoyONOV71DOlT9JmWzGu9g2aFSKKUU3LffTY5WNQd5-Qm9QdbhHB0CtOAPJZ124FKiLLS0wJh0nVZKez8_6dH2ICOCw3e-lyktHOc6R11s_9xhEF1LLYv20b7BziWy6T_VHZOcUsiJbBwn09lx1SKkK4Ic3iEsWNeg-lSAgPJGO-sClUmwW9ffA3L1p8OvZubSNirKcGbRYkC323wXnlWv-tqqj6CW-uNc448i6qsmrs2RRR_f4WdLFN9dvv_PBjsf7V8r0m_HY4jjDzScNLLA4Hg
  </code></pre>
  <p>After some refresh or log in again, I am able to see my product added before and which was already on firebase now fetched from and shown on admin area as I am getting correct userId from getState().</p>
  <p>Note: The product I tried above is not added but some products I already added and which were not fetched as userId I am getting is incorrect.</p>
  <p>When I am getting the correct userId from firebase which is also my ownerId then I am able to see my product in my admin area.</p>
  <p><strong>I am getting idToken in userId instead of localId. I don't know why and how to solve this.</strong></p>