Search code examples
react-nativereact-reduxasyncstorage

React native AsyncStorage has weird \\ stuff


I use React native.

And When I get storage data from AsyncStorage as below.

const keys = await AsyncStorage.getAllKeys();
const result = await AsyncStorage.multiGet(keys);
const stringResult = result.toString();
console.log(stringResult);

The result has weird \\ stuffs like below.

persist:root,{"usersReducer":"{\"isLoggedIn\":true,\"token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NiwiaWF0IjoxNjU3MTI5MDEwfQ.iA2AsFXuKMNAM1vgrPswjdCU46PemIu771SDqWKWqAw\"}","_persist":"{\"version\":-1,\"rehydrated\":true}"}

I use redux-persist so that it has 'persist:root' in front, i guess.

Anyway that make me not able to get data with this code: await AsyncStorage.getItem("isLoggedIn")

Why does it has \? When I store data I don't put that things.


Solution

  • I get the data from below code..

    const keys = await AsyncStorage.getAllKeys();
    const result = await AsyncStorage.multiGet(keys);
    const modifiedResult = result.flat().splice(1);
    const parseResult = JSON.parse(
      JSON.parse(JSON.parse(JSON.stringify(modifiedResult)).toString())
        .usersReducer
    );
    const { isLoggedIn } = parseResult;
    

    I do multiple x2 JSON.parse and JSON.stringify 😂 not sure it is right way or not. Anyway value is extracted correctly.