Search code examples
reactjsice

React JS (Ice js) Error: Invalid attempt to destructure non-iterable instance


I just used this code export const useStateValue = () => useContext(StateContext); I used it in my index.tsx inside jsx component

  const [{ user },dispatch] = useStateValue();

I imported everything am still getting errors

which I think is responsible for error . anyways to solve it?

Link to repo : https://github.com/dingus45191/Facebook-ice

I am using alibaba's ice framework

Error: TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a Symbol.iterator method.


Solution

  • The error is telling you you're trying to destructure an iterable

    const [{ user },dispatch] = useStateValue();
    

    when the value isn't an iterable.

    I suspect you're setting the context value to an object, not an array and may want something more like

    const { { user }, dispatch } = useStateValue();
    

    if you're setting the context value as an object.