I am learning React by making a motor cycle spec searching web application, and I am trying to import fetched data into makePicker.jsx to essentially make a drop down menu with those data.
However I am getting an error message saying:
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:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app
I am not sure what applies to my situation. Can you see why?
makePicker.jsx
import React, {useState, useEffect} from 'react'; import {NativeSelect, FormControl} from '@material-ui/core'; import styles from './makePicker.module.css'; import { makeList } from '../../api'; const MakePicker = ()=>{ const [fetchedMakes, setFetchedMakes] = useState([]); useEffect(()=>{ const fetchAPI = async()=>{ setFetchedMakes(await makeList()); } fetchAPI(); },[]) return ( <h1>makePicker</h1> ); } export default MakePicker; App.js
import logo from './logo.svg'; import './App.css'; import {fetchData, makeList} from './api/index'; import React, {Component} from 'react'; import MakePicker from './components/makePicker/makePicker'; class App extends React.Component{ state = { data:[], makes:[], } async componentDidMount(){ // const fetchedData = await fetchData(); const fetchedMakeList = await makeList(); this.setState({makes:fetchedMakeList}); } render(){ return ( <div className="App"> <header className="App-header"> {MakePicker()}; <h1>Some line-ups from YAMAHA</h1> {this.state.makes.map(make=>{ return <p>{make.name}</p> })} <a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer" > Open React </a> </header> </div> ); } } export default App; Please let me know if need more information from my project.
MakePickerin another component?MakePickeris a component; not the function. So instead of{MakePicker()};define as JSX element i.e.<MakePicker />