Search code examples
reactjsaxiosfetch

reactjs getting api: Can we avoid using fetch and only use axios


I am thinking to use axios and not fetch because of the below ease of use

axios.post(url,data,auth).then(res => {
        toast("Successful!");
      })
      .catch(err => {
          toast(<pre style={{whiteSpace: 'pre-line' }}>Error!<br></br>{JSON.stringify(err.response['data'])}</pre>);
      });

Main advantage:

  • status is 2xx then i can show success
  • status is not 2xx then i can show the err

but in the fetch it not so easy to do this.

But i found many projects people use fetch inspite the difficulty. But i want to stick to axios. Will i have to worry about this


Solution

  • Axios is popular library so you shouldn't to worry about using this. Axios have many advantage on fetch. For example, I like instances in axios. Here is list of the advantages and differences.