0

I am trying to call API calls using Axios and React hooks. I want to call it when certain criteria are met. For example, if language is equal 'ru', 'en', 'fr' call first API call, if language is equal 'zh', 'ko', 'ar'. below is my code.

 const reqTranslate = () => {
    if(language === 'ru' || 'en' || 'de' || 'fr'  || 'tr' ||  'it' || 'es'){
      axios.all([requestTranslate, requestDictionary])
      .then(
       
          axios.spread((...responses) => {
            const responseOne = responses[0]?.data?.text[0];
            const responseTwo = responses[1]?.data?.def[0]?.tr;
            const responseThree = responses[1]?.data?.def[0]?.pos
            const responseFour = responses[1]?.data?.def[0]?.ts
            const responseFive = responses[1]?.data?.def[0]?.text
            const responseSix = responses[1]?.data?.def?.[0]?.tr
    
            // use/access the results
            console.log(responseOne, responseTwo);
            setTextresult(responseOne);
            console.log(responseOne)
            setSynTransLang(responseTwo);
            setPos(responseThree);
            setTs(responseFour);
            setTxt(responseFive)
            setMapValue(responseSix)
          })
        )
    } 
else if (language === 'ko' || 'zh'){
   axios.all([requestTranslate, requestDictionary])
  .then(axios.spread((...responses)=>{
    const responseOne = responses[0]?.data?.text[0];
   
    // use/access the results
    console.log(responseOne);
    setTextresult(responseOne);
    
   })).catch(errors => {
      // react on errors.
      console.error(errors);
    })

}}
skyboyer
  • 22,209
  • 7
  • 57
  • 64
  • 2
    You can't use conditionals that way. [Javascript: The prettiest way to compare one value against multiple values](https://stackoverflow.com/questions/9121395/javascript-the-prettiest-way-to-compare-one-value-against-multiple-values) – Andy Ray Aug 24 '21 at 05:01

1 Answers1

1

If you don't prefer doing language === 'ru' || language === 'en' then you can do this:

const langArr = ['ru','en','de','fr','tr','it','es'];

if (langArr.indexOf(language) > -1) {
   ...
}
Micko Magallanes
  • 261
  • 1
  • 12