我是原生react 的初学者,我正在try 通过axios API GET请求从MySQL数据库中获取用户信息.
登录后,我将邮箱地址存储在AsyncStorage中,然后希望使用AsyncStorage中的该邮箱地址作为参数或参数来获取用户详细信息.
我编写了一段代码,将setState的初始状态设置为‘na’.请告诉我如何将AsyncStorage中的邮箱地址作为参数或参数进行传递.
这是我的代码.
// to load email address
const [SessionEmail, setSessionEmail] = useState('na');
// to load users info
const [users, setUsers] = useState([]);
useFocusEffect(
React.useCallback(() => {
getUsername();
getUsersInfoFromAPI();
}, [])
);
// to get the session username from localstorage
const getUsername = async () => {
try {
const username = await AsyncStorage.getItem('Username')
if (username !== null) {
setSessionEmail(username);
}
} catch (e) {
console.log(e);
}
}
// API Calling user details
const getUsersInfoFromAPI = async () => {
await axios.get(`https://myapi.co.in/api/user/?email=${SessionEmail}`)
.then(response => {
setUser(response.data);
})
.catch(error => {
console.log(error);
});
}
在页面呈现后,我从Metro加载页面,我可以看到参数已经发送到服务器.