好吧,请对我温柔一点,我对 React Native 还很陌生,但我正在使用 Context 来管理我的 React Native 项目中的一些状态。这是我陷入困境的一个例子
在功能组件的顶部我有这个......
import DataContext from "../../contexts/DataContext";
import AsyncStorage from "@react-native-community/async-storage";
const AccountHomepageScreen = ({navigation}) => {
const { data, updateInfo} = useContext(DataContext);
const getUserFromAsync = async () => {
try {
const value = await AsyncStorage.getItem('user_id');
if(value !== null) {
//shows user_id in Alert no problem
alert(value);
//but this does not work?!?!
updateInfo('userId', value);
}
} catch(e) {
// error reading value
return false;
}
}
}
useEffect(() => {
getUserFromAsync();
}, …Run Code Online (Sandbox Code Playgroud)