'useFetchClient is not a function', axiosInstance migration

System Information
  • Strapi Version: 4.5.6
  • Operating System: MacOS Catalina
  • Database: SQLite
  • Node Version: 16.18.1
  • NPM Version: 8.19.2
  • Yarn Version: 1.22.19

Issue

As of Strapi v4.5.6 (and possibly prior releases), Strapi logs the following warning:

Deprecation warning: Usage of "axiosInstance" utility is deprecated and will be removed in the next major release. Instead, use the useFetchClient() hook, which is exported from the admin: { useFetchClient } from "@strapi/helper-plugin"

When attempting to use the new hook in Admin development, I consistently receive the following error:

Uncaught TypeError: (0 , _strapi_helper_plugin__WEBPACK_IMPORTED_MODULE_2__.useFetchClient) is not a function

Implementation

Based on what I can infer from the source code (https://github.com/strapi/strapi/blob/main/packages/core/helper-plugin/lib/src/hooks/useFetchClient/index.js), useFetchClient is essentially just a wrapper for an axios instance, however the following code returns the aforementioned error:

import { useFetchClient } from '@strapi/helper-plugin'

const Component = () => {

  const client = useFetchClient()
  
  const handleGetData = async () => {
    const response = await client.get('/posts')
    console.log(response)
  }

  return (
    <button onClick={handleGetData}>Test</button>
  )
}

I must assume I am simply using the hook incorrectly, however it us curious to me that even just invoking the hook (i.e. without implementing the handleGetData function) throws the same error.

Any help in this matter would be appreciated, as documentation on helper-plugin functions is currently nonexistent.

Thanks!

It appears useFetchClient is not even exported from the package, as it does not show up as part of the module when namespace imported.

e.g.

import * as helperPlugin from '@strapi/helper-plugin'

console.log(helperPlugin)

Console:
...


...

Hello, sorry for the confusion, the useFetchClient and getFetchClient at the beginning were implemented inside the admin, recently we decided to move it to the helper-plugin. In the next release, 4.6.0. it will be available and testable in the helper-plugin. Sorry again

2 Likes

Thank you for the clarification!

Hi Simone - does this mean that we should be using useFetchClient instead of request, or that it doesn’t matter and the warning is an internal message?

Yes, the best practice now is to use useFetchClient. Thanks

When I am using useFetchClient inside my plugin it logs me out from the admin. Is this a known problem?

2 Likes

This error happens with using request() as well. The weird thing is that it worked perfectly in Strapi v3. @DMehaffy

1 Like