0

I got error: "Invalid hook call. Hooks can only be called inside of the body of a function component." in react function:

interface Items {
  items: any[]
}

const [items, setItems] = useState<Items>();

const ItemsList: React.FC<Items> = ({ items }) => {
  useEffect(() => {
    const response = Http.get<Items>(url).then(res => setItems(res)) || [] as Items[]
  }, []) //something wrong with this hook?

  return (
      <ul>
        {items.map((item) => {
          return (
            <ItemPreview key={item.id} item={item}/>
          );
        })}
      </ul>
  )
}

export default ItemsList

I can't understand what I did wrong? can anyone show me where is error?

2
  • pretty much exactly what the error message says: you're calling the useState hook before your function component is declared. Commented Jan 14, 2021 at 17:20
  • 3
    Move const [items, setItems] = useState<Items>(); right above useEffect(() => { and below ItemsList: React.FC<Items>. Commented Jan 14, 2021 at 17:21

1 Answer 1

1

You need to call React Hooks from within a component, like so:

interface Items {
  items: any[]
}

const ItemsList: React.FC<Items> = ({ items }) => {
  const [items, setItems] = useState<Items>();
 
  useEffect(() => {
    const response = Http.get<Items>(url).then(res => setItems(res)) || [] as Items[]
  }, []);

  return (
      <ul>
        {items.map((item) => {
          return (
            <ItemPreview key={item.id} item={item}/>
          );
        })}
      </ul>
  )
}

export default ItemsList

See the Rules of Hooks for more information:

Only Call Hooks from React Functions

Don’t call Hooks from regular JavaScript functions. Instead, you can:

  • ✅ Call Hooks from React function components.
  • ✅ Call Hooks from custom Hooks.
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you.I fixed error like @Sam R.said, before your answer (you need also remove { items } ), and now I have another error :) "Object is possibly 'undefined'. In "items.map" line.
This is correct. It might also be worth you checking out this blog post about avoiding race conditions when making api calls within a useEffect hook. flufd.github.io/avoiding-race-conditions-use-current-effect
@eu97 Not sure if it's of any use to you, but I use use-async-resource with <Suspense /> boundaries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.