React结合API调用报错:Uncaught TypeError: (0 , f.default) is not a function
Hey there, let’s work through this issue together— that Uncaught TypeError: (0 , f.default) is not a function error is one of the most common gotchas when dealing with module imports/exports in React, especially when using utility classes for API calls. Let’s break down how to diagnose and fix this step by step.
1. First, fix the import/export mismatch (the likely cause of your TypeError)
This error almost always happens when there’s a mismatch between how your item utility class exports its API function and how your Items component imports it. Here’s what to check:
- If your utility uses
export default:
For example, initemUtils.js:
Make sure your component imports it without curly braces:const fetchItems = async () => { // Your API call logic here }; export default fetchItems;import fetchItems from './path-to-itemUtils'; - If your utility uses named exports:
InitemUtils.js:
Your component must use curly braces to import it:export const fetchItems = async () => { // Your API call logic here };import { fetchItems } from './path-to-itemUtils';
Mixing these two styles is the #1 culprit for that exact TypeError.
2. Verify if your API request is actually being sent
To confirm whether the request is leaving your app, use your browser’s DevTools:
- Open Chrome/Firefox DevTools and switch to the Network tab
- Refresh your page or trigger the action that loads the Items component
- Look for your API endpoint in the request list:
- If you see it: Check the status code (200 means success) and response body to ensure it matches what you get when calling the API directly.
- If you don’t see it: The function from your utility class isn’t being called at all— jump to step 4 to check your component’s call logic.
You can also add quick console.log statements to your utility function to confirm it’s executing:
export const fetchItems = async () => { console.log('Fetch items function was triggered!'); // Check your browser console for this const response = await fetch('your-api-url'); console.log('API response:', response); return response.json(); };
3. Test your utility class logic independently
Since calling the API directly works, isolate the utility class to test its logic outside of React:
- Create a simple test file (e.g.,
test-item-utils.js) in your project:import { fetchItems } from './path-to-itemUtils'; // Run the function directly (async () => { try { const data = await fetchItems(); console.log('Test successful! Data:', data); } catch (error) { console.error('Test failed with error:', error); } })(); - Run this file with Node (if your utility uses browser APIs like
fetch, you might need to use a tool likenode-fetchor test it directly in your browser’s console). This will tell you if the utility itself has bugs in its request logic (like missing headers, incorrect URL formatting, or broken async handling).
4. Double-check your Items component’s function call
Make sure you’re invoking the utility function correctly in your component:
- Avoid common mistakes like calling an async function directly inside
useEffect(wrap it in a nested function instead):useEffect(() => { const loadItems = async () => { try { const items = await fetchItems(); // Update state with the fetched items } catch (error) { console.error('Failed to load items:', error); } }; loadItems(); }, []); - Ensure you’re not accidentally treating the function as an object (e.g.,
fetchItems.someMethod()instead offetchItems()).
内容的提问来源于stack exchange,提问作者Abbas Soloki

