I have an ID stored in AsyncStorage and depending on this call, I want to make a server request.
So something like this is needed:
AsyncStorage.getID().then(id => {
useLoadFromServer(id)
}
But I always struggle with getting errors about using hooks wrong. So far I tried:
// First Approach
const MyScreen = () => {
const [ID, setID] = useState()
AsyncStorage.getID()
.then(id => setID(id))
.catch(e => console.log(e))
const { data } = useLoadRoot(ID) // Not the ID from AsyncStorage is used
}
//Second Approach
const MyScreen = async () => {
const [ID, setID] = useState()
const IDPromise = AsyncStorage.getID()
const { data } = useLoadRoot(await IDPromise) // Possible Unhandled Promise Rejection
also I tried to use useEffect, which leads to React Hooks must be called in a React function component or a custom React Hook function
The useLoadRoot-hook just calls another hook UseLoadFromEndpoint doing the axios.get()
When trying to move the AsyncStorage-Request to the customHook, I get the same errors, as I call the useLoadFromEndpoint-hook incorrectly, but I have to / want to reuse the UseLoadFromEndpoint-Hook. How can I achive this?