Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6212

set state not working react hooks(string) asyncStorage [duplicate]

$
0
0

hey all so im using react-natives-community async storage and i can't for the life of me get the state to save and im not too sure why. the first console.log from the await variable returns the correct information, but when i set the state the second console.log returns null and im not sure what's going on

const [userEmail, setUserEmail] = useState<string | null>(null);const getEmail = await AsyncStorage.getItem('email')console.log(getEmail +'first')setUserEmail(getEmail);console.log(userEmail +'second')

I made this custom hook but still no luck

const useGetAsyncStorage = (AsyncStorageItem: string): string => {  try {    const [data, setData] = useState<string | null>(null);    useEffect(() => {      const fetchAsyncStorage = async () => {        const AsyncStorageData = await AsyncStorage.getItem(AsyncStorageItem);        console.log(AsyncStorageData)        setData(AsyncStorageData);      };      fetchAsyncStorage();    }, [AsyncStorageItem]);    return data as string  } catch (error) {    return error  }};

Viewing all articles
Browse latest Browse all 6212

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>