0

I have the following stateless component in react:

import React, {useState, useEffect} from 'react';
import ReactDOM from 'react-dom';
import {Button, Form, FormGroup, Label, Input, FormText} from 'reactstrap';


function Trend() {
    const [state, setState] = useState({
        dataTypes: []
    });

    return (
        <Form>
            <FormGroup>
                <Input type="select" multiple>
                    {state.dataTypes.map(type => {
                        return <option>{type}</option>
                    })}
                </Input>
            </FormGroup>
        </Form>
    );
}

However, whenever I try to run it, I get the following error:

react.development.js:88 Uncaught Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/warnings/invalid-hook-call-warning.html for tips about how to debug and fix this problem.

However, none of these possibilities are correct, as you can see from the code above, and my npm environment:

$ npm ls react
megaqc@1.0.0 /home/michael/Programming/MegaQC
└── react@16.8.6 

$ npm ls react-dom
megaqc@1.0.0 /home/michael/Programming/MegaQC
└── react-dom@16.8.6 

What is causing this error?

Migwell
  • 18,631
  • 21
  • 91
  • 160
  • I'm not using `html-webpack-plugin`, so that can't be it. And none of my HTML loads React except through the webpack bundle – Migwell Jun 06 '19 at 04:17
  • also issue with hot module replacement is still in open state https://github.com/gaearon/react-hot-loader/issues/1088 – skyboyer Jun 06 '19 at 04:18
  • better check precisely if react is not loaded twice for any reason possible. I'd say it's most typical root cause for "invariant violation". reason behind may be not html-webpack-plugin only. say having links in modules https://stackoverflow.com/questions/31169760/how-to-avoid-react-loading-twice-with-webpack-when-developing/31170775#31170775 – skyboyer Jun 06 '19 at 04:36
  • Is there a definitive way to check if it's being loaded twice? – Migwell Jun 06 '19 at 04:46
  • But how is it that I have 16.8.6 installed if it depends on 16.3.0? – Migwell Jun 06 '19 at 05:08
  • up to `npm ls` you may just find react package code in your bundle and set breakpoint on first line of it, then reload page. – skyboyer Jun 06 '19 at 05:35
  • Are you using Next.js or another libarry with hot reloading? – Tom Jun 06 '19 at 07:37
  • Try to reproduce the issue in a CodeSandbox – cbdeveloper Jun 07 '19 at 09:04

0 Answers0