Skip to content

useAxios

Category
Package
comuse/integrations
Last Changed
12月29日16时10分

Wrapper for axios.

Demo

Loading: true
Finished: false
Aborted: false

Install

bash
npm i axios@^1

Usage

ts
import { useAxios } from 'comuse-integrations/useAxios'

const { data, isFinished } = useAxios('/api/posts')

or use an instance of axios

ts
import axios from 'axios'
import { useAxios } from 'comuse-integrations/useAxios'

const instance = axios.create({
  baseURL: '/api',
})

const { data, isFinished } = useAxios('/posts', instance)

use an instance of axios with config options

ts
import axios from 'axios'
import { useAxios } from 'comuse-integrations/useAxios'

const instance = axios.create({
  baseURL: '/api',
})

const { data, isFinished } = useAxios('/posts', { method: 'POST' }, instance)

When you don't pass the url. The default value is {immediate: false}

ts
import { useAxios } from 'comuse-integrations/useAxios'

const { execute } = useAxios()
execute(url)

The execute function url here is optional, and url2 will replace the url1.

ts
import { useAxios } from 'comuse-integrations/useAxios'

const { execute } = useAxios(url1, {}, { immediate: false })
execute(url2)

The execute function can accept config only.

ts
import { useAxios } from 'comuse-integrations/useAxios'

const { execute } = useAxios(url1, { method: 'GET' }, { immediate: false })
execute({ params: { key: 1 } })
execute({ params: { key: 2 } })

The execute function resolves with a result of network request.

ts
import { useAxios } from 'comuse-integrations/useAxios'

const { execute } = useAxios()
const result = await execute(url)

use an instance of axios with immediate options

ts
import axios from 'axios'
import { useAxios } from 'comuse-integrations/useAxios'

const instance = axios.create({
  baseURL: '/api',
})

const { data, isFinished } = useAxios('/posts', { method: 'POST' }, instance, {
  immediate: false,
})

Source

SourceDemoDocs

Changelog

v4.1.7 on 12/29/2025
97204 - feat: add useAxios

Released under the MIT License.