I'm having an issue with calling useEffect from this function. I haven't been using React that long so I may have some glaring issues but this is taking up too much time.
My function is as follows:
import React, { Component } from 'react';
import { useState, useEffect } from "react";
export function Sessions() {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [count, setCount] = useState(null);
useEffect(() => {
fetch(`https://localhost:7126/api/`)
.then((response) => {
if (!response.ok) {
throw new Error(
`This is an HTTP error: The status is ${response.status}`
);
}
return response.json();
})
.then((actualData) => {
setData(actualData);
setError(null);
setCount(actualData.length);
})
.catch((err) => {
console.log(err.message);
});
}, []);
return (
<div className="App">
<h1>Stream Request: {count} records</h1>
{loading && <div>A moment please...</div>}
{error && (
<div>{`There is a problem fetching the post data - ${error}`}</div>
)}
<ul>
{data &&
data.map(({ StreamId, EntryTime }) => (
<li key={StreamId}>
<h3>{EntryTime}</h3>
</li>
))}
</ul>
</div>
);
}
The error i'm receiving is
Warning: 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
#1)I have updated React and React-Dom they were both up to date.
#2) ? I don't think I am?
#3) I have verified this using npm ls react and my output is as follows:
+-- [email protected]
| `-- [email protected] deduped
+-- [email protected]
| +-- [email protected]
| | `-- [email protected] deduped
| `-- [email protected] deduped
+-- [email protected]
`-- [email protected]
+-- [email protected]
| `-- [email protected] deduped
+-- [email protected]
| `-- [email protected] deduped
`-- [email protected] deduped
All seem to be deduped? Please help, none of the other questions have helped my issue such as :Invalid Hook Call but not really
Edit I use Sessions via App.js & AppRoutes.js App.js:
import "./stylesheet.css";
import React, { Component } from 'react';
import { Route, Routes } from 'react-router-dom';
import AppRoutes from './AppRoutes';
import { Layout } from './components/Layout';
export default class App extends Component {
static displayName = App.name;
render() {
return (
<Layout>
<Routes>
{AppRoutes.map((route, index) => {
const { element, ...rest } = route;
return <Route key={index} {...rest} element={element} />;
})}
</Routes>
</Layout>
);
}
}
AppRoutes.js:
import { Sessions } from "./components/Sessions";
import { Messages } from "./components/Messages";
import { Home } from "./components/Home";
const AppRoutes = [
{
index: true,
element: <Home />
},
{
path: '/sessions',
element: <Sessions />
},
{
path: '/messages',
element: <Messages />
}
];
export default AppRoutes;
Sessions