Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React JS Error: Invalid attempt to destructure non-iterable instance

I have a sort filter that takes an array to populate the options. Trying to see the option value equal to the text within the array but I get the error within the title:

Invalid attempt to destructure non-iterable instance

I need to pass the text as the value within the option tag so that when the user updates the filter, the correct text displays to the choice the user made.

Here is my code:

function Sorting({by, order, rp}: SortingProps) {
    const opts = [
        ['Price (low)', 'price', 'asc'],
        ['Price (high)', 'price', 'desc'],
        ['Discount (low)', 'discount', 'asc'],
        ['Discount (high)', 'discount', 'desc'],
        ['Most popular', 'arrival', 'latest'],
        ['Most recent', 'arrival', 'latest'],
    ];

    const onChange = (i) => {
        const [text, by, order] = opts[i];
        refresh({so: {[by]: order}});
        /* GA TRACKING */
        ga('send', 'event', 'My Shop Sort By', text, 'Used');
    };

    return (
        <div className={cn(shop.sorting, rp.sorting.fill && shop.sortingFill)}>
            <Select className={shop.sortingSelect} label="Sort By" onChange={onChange} value={`${by}:${order}`}>
                {opts.map(([text], i) =>
                    <Option key={i} value={text}>{text}</Option>
                )}
            </Select>
        </div>
    )
}
like image 491
Filth Avatar asked Feb 09 '17 16:02

Filth


3 Answers

I caused this error a few times because whenever I write a useState hook, which I would do often, I'm used to using an array to destructure like so:

const [ state, setState ] = useState();

But my custom hooks usually return an object with properties:

const { data, isLoading } = useMyCustomFetchApiHook();

Sometime I accidentally write [ data, isLoading ] instead of { data, isLoading }, which tiggers this message because you're asking to destructure properties from an array [], when the object you're destructuring from is an object {}.

like image 174
conor909 Avatar answered Oct 29 '22 04:10

conor909


I also encountered a similar error and honestly, I did a very silly mistake maybe because of editor autocomplete.

I had to make use of the useState hook but somehow due to autocomplete, I wrote it like this.

  const [state, setState] = useEffect(defaultValue);

instead of :(.

  const [state, setState] = useState(defaultValue);

Hope it will help as an error message, in this case, was not helpful at all until I spent some time debugging this.

like image 27
Divyanshu Rawat Avatar answered Oct 29 '22 03:10

Divyanshu Rawat


The error Invalid attempt to destructure non-iterable instance occurs because of a logic/coding error. The following javascript is used to illustrate the problem:

[aaa,bbb] = somefunc()

When somefunc() is called it must return an array of at least two items. If it doesn't there is no way to convert the result from somefunc() into values for aaa and bbb. For example, the code:

[aaa,bbb] = { 'some': 'object'}

would produce this error.

So the error is really a Javascript coding error and it is just happening inside React code that handles this situation by printing the error shown. See MDN for destructuring assignment documentation.

As @Mayank Shukla states in his answer, the answer to the OP question is to fix this line of code:

const [text, by, order] = opts[i];

By changing it to this: const [text, by, order] = opts[i.target.value];

With my above description it should be clearer that opts[i] the original code by the OP was not returning an array of at least 3 items so the javascript runtime was not able to set the values of the variables text, by and order. The modified/fixed code does return an array so the variables can be set.

After looking for an answer to this question I realized that the other answers were correct, and I am just summarizing the root cause of the error message.

like image 9
PatS Avatar answered Oct 29 '22 04:10

PatS