breaking the Rules of Hooks?
okay, I don't understand react hooks well enough. i'm writing my component this way:
and it gives me this error:
I've made sure that react and react-dom are the same version and every package is calling the same copy of react, so I'm violating the rules of hooks. not sure how
function TableRowFromJob (job: JobType) {
const getDuration = function(startTime: string, endTime: string) {
const humanized = humanize(new Date(endTime).getTime() - new Date(startTime).getTime(), {
round: true,
units: ['h', 'm', 's'],
largest: 3,
})
return humanized
}
const [duration, setDuration] = useState('');
useEffect(() => {
const timer = setInterval(() => {
const nowDate = new Date().toISOString()
setDuration(getDuration(job.startTime.toISOString(), job.endTime?.toISOString() ?? nowDate));
}, 1000);
return () => clearInterval(timer);
}, []);
return (
...
<TableCell className="text-center" suppressHydrationWarning>{duration}</TableCell>
...
)
};
async function JobTable (data: { page: number }) {
const trpcClient = api.useUtils().client;
const result = await trpcClient.runs.getAllRuns.query({page: data.page});
return (
<TableBody>
{result.map((job) => TableRowFromJob(job))}
</TableBody>
);
};
export { JobTable, TableRowFromJob };
function TableRowFromJob (job: JobType) {
const getDuration = function(startTime: string, endTime: string) {
const humanized = humanize(new Date(endTime).getTime() - new Date(startTime).getTime(), {
round: true,
units: ['h', 'm', 's'],
largest: 3,
})
return humanized
}
const [duration, setDuration] = useState('');
useEffect(() => {
const timer = setInterval(() => {
const nowDate = new Date().toISOString()
setDuration(getDuration(job.startTime.toISOString(), job.endTime?.toISOString() ?? nowDate));
}, 1000);
return () => clearInterval(timer);
}, []);
return (
...
<TableCell className="text-center" suppressHydrationWarning>{duration}</TableCell>
...
)
};
async function JobTable (data: { page: number }) {
const trpcClient = api.useUtils().client;
const result = await trpcClient.runs.getAllRuns.query({page: data.page});
return (
<TableBody>
{result.map((job) => TableRowFromJob(job))}
</TableBody>
);
};
export { JobTable, TableRowFromJob };
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://react.dev/link/invalid-hook-call for tips about how to debug and fix this problem.
⨯ next/dist/compiled/react/cjs/react.development.js (1500:34) @ useState
⨯ TypeError: Cannot read properties of null (reading 'useState')
at TableRowFromJob (./src/app/[id]/table.tsx:53:84)
at eval (./src/app/[id]/table.tsx:183:37)
at Array.map (<anonymous>)
at JobTable (./src/app/[id]/table.tsx:183:26)
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://react.dev/link/invalid-hook-call for tips about how to debug and fix this problem.
⨯ next/dist/compiled/react/cjs/react.development.js (1500:34) @ useState
⨯ TypeError: Cannot read properties of null (reading 'useState')
at TableRowFromJob (./src/app/[id]/table.tsx:53:84)
at eval (./src/app/[id]/table.tsx:183:37)
at Array.map (<anonymous>)
at JobTable (./src/app/[id]/table.tsx:183:26)
0 Replies