skip to Main Content

I ran into a problem. when the page load, data is not loaded from the server, but when the page is saved again, everything works.
Perhaps the problem is related to Async Storage, which does not have time to give a token for a get request.

const tokenParse = async () =>{
    const token = await (AsyncStorage.getItem('@storage_token'));
    setvalue(JSON.parse(token))
}
const LoadData = async (message) =>{
    try{
        const value = await (AsyncStorage.getItem('@storage_token'));
        parsedValue = JSON.parse(value);
        if (value !== null) {

            const token = {
                headers: {
                    'Content-Type': 'application/json',
                },
                params:{
                    token: parsedValue,
                }
                
            }
            const LoadTasks = async () =>{
                const get = await axios.get(`${baseUrl}/api/get`, token) 
                setmessage(get.data.response.tasks);
                setObject(Object.values(message));
                console.log(1);
            }
            LoadTasks()
        }
    }catch (error) {
        console.log('Ошибка при получении значения из AsyncStorage', error);
       }
    finally {
    }
}

useEffect(() => {
    tokenParse();
    LoadData(message)        
}, []);
return(
{object?.map((object, index) => {
return(
    <Text key={object.someProperty} style={{color: 'black', marginTop: 5, marginLeft: '2%',}}>     {object.task_name}</Text>

                                )
                            }
                        )})

When loading the screen, data is not loaded from the server.

2

Answers


  1. AsyncStorage has been removed from the latest versions of React Native and should no longer be used, there are synchronous alternatives such as react-native-mmkv and others that would most likely solve your issue.

    Login or Signup to reply.
  2. In LoadData function, await LoadTasks() instead of LoadTasks().
    In useEffect(), message,the parameter of LoadData(), is unclear.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search