【发布时间】:2021-04-28 16:54:59
【问题描述】:
我开始使用 React Native,应该使用 AsyncStorage 而不是 LocalStorage
但我无法处理 useEffect 和 AsyncStorage 中的异步,因为 fetch 后的令牌集发送所有内容。请帮帮我
const [tkn, setTkn] = useState('')
const getData = async () => {
try {
const value = await AsyncStorage.getItem('token')
if(value !== null) {
setTkn(value)
}
} catch(e) {
}
}
useEffect(() => {
AsyncStorage.getItem('token').then((value) => {
if (value) {
setTkn(value)
}
});
fetch('http://127.0.0.1:8000/home', {
method: "GET",
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
'Token': tkn
}
})
.then(function (response) {
if (response.status >= 200 && response.status <= 299) {
return response.json();
} else {
}
})
.then(funds => {
///
}).catch(function (error) {
})
}, [history])
【问题讨论】:
标签: reactjs react-native asynchronous asyncstorage