readylayerone_
readylayerone_
TTCTheo's Typesafe Cult
Created by readylayerone_ on 12/12/2024 in #questions
breaking the Rules of Hooks?
okay, I don't understand react hooks well enough. i'm writing my component this way:
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 };
and it gives me this error:
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)
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
2 replies
TTCTheo's Typesafe Cult
Created by readylayerone_ on 6/18/2024 in #questions
Redis EventEmitter?
Hi, I'd like to use trpc's subscription endpoints properly. I want to deploy it on an AWS ECS service and so I'll be needing a global event emitter. I'm hoping to use upstash for this, but I'm not finding any good libraries for it. Best I could find is this one: https://github.com/jksdua/redis-events thanks!
2 replies