React Native state fail to update and synchronize

  javascript, react-native, reactjs

I tried to use information of the initial state in UserContext.js to do user registration, and I used useReducer to change the value of initial state. But when the file Login.js first rendered, the information in the initial state is not updated and returns a null. Can someone help me with this problem

Login.js:

const nodeRegistration = (aesKey, email, password) => {

    console.log(state)
    
    if (state.publicKey !== null) {
      const p = state.publicKey.slice(6, -14)

      const registerdata = "usnm=" + encodeURIComponent(email) + "&pswd=" + encodeURIComponent(password)
      + "&nt=65540&key=" + hexToBase64(p) + "&mac=" + state.uniqueMacId + "&lip=" + state.localIpAddress 
      + "&host=" + state.diviceInfo.diviceName + "&osn=" + state.diviceInfo.operatingSystemName + "&osv=" 
      + state.diviceInfo.operatingSystemVersion

      console.log(registerdata)
    }
  };

UserContext.js:

    const initialState = {
    
      regionKey: null,
      aesKey: null,
    }    

const reducer = (state, action) => {
      switch (action.type) {
        case 'get_regionInfo':
          return {
            ...state, 
            regionKey: action.payload.regionKey,
            aesKey: action.payload.aesKey
          }
        default:
          return state;
      }
    };

const getRegionInfo = dispatch => {
  return () => {
    API().get("https://center.ubiq.network/regiondata").then(res => {
      console.log(res.data[0])
      dispatch({ type: 'get_regionInfo', payload: {
        regionKey: res.data[0].node,
        aesKey: getAESKey(res.data[0].node)
      }})
    }).catch(err => {
      console.log(err)
    })
  }
}

Source: Ask Javascript Questions

LEAVE A COMMENT