Javascript 反应本机状态更新和同步失败
我尝试使用UserContext.js中的初始状态信息进行用户注册,并使用useReducer更改初始状态的值。但是,当文件Login.js首次呈现时,初始状态下的信息不会更新并返回null。有人能帮我解决这个问题吗 Login.js:Javascript 反应本机状态更新和同步失败,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我尝试使用UserContext.js中的初始状态信息进行用户注册,并使用useReducer更改初始状态的值。但是,当文件Login.js首次呈现时,初始状态下的信息不会更新并返回null。有人能帮我解决这个问题吗 Login.js: const nodeRegistration = (aesKey, email, password) => { console.log(state) if (state.publicKey !== null) { c
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)
})
}
}