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

Build custom state object with failed promises in React Native using axios within a custom Hook

$
0
0

I'm trying to build a custom state within a custom Hook (RN init, not expo)
This state is

interface ResponseState {    api1: {        error: boolean;        errorMsg?: string;        data?: DataResponse; // interfaces/blablabla    };    api2: {        error: boolean;        errorMsg?: string;        data?: DataResponse; // interfaces/blablabla    };}...const [myData, setMyData] = useState<ResponseState>({    api1: {        error: false,        errorMsg: '',    },    api2: {        error: false,        errorMsg: '',    }});

Then, I try to work with Promise.all and build state accordingly in case some endpoint fails. I don't want to remove failed ones from the final state

The following code works (in case all promises work fine)

const p1 = MyAxios.get<ResponseState>("/404api1");const p2 = MyAxios.get<ResponseState>("/api2");await Promise.all([p1, p2])    .then(function (res) {        setMyData({            api1: { error: false, datos: res[0].data },            api2: { error: false, datos: res[1].data },        })    })    .catch(function (err) {        console.error('====!!!!!err', err);    });

But... How should I build my state in case some of the promises fail? And get its corresponding error message for each one that failed... 404, maybe other gives 500, and so. I have around 15 endpoints in different urls, domains to manage in this hook

Thanks


Viewing all articles
Browse latest Browse all 6287

Trending Articles



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