#am i doing this correctly and what am i doing wrong?
1 messages · Page 1 of 1 (latest)
hi
why is home red?
and am i doing it correctly?
whats the problem
Home is red
n im wondering if im doing swr correctly
hover over the Home component and take a screenshot
@fickle tangle specifically I want to know whats the error
it disappears when i go to take a screenshot
well it basically says you're trying to use undefined instead of an Element.
is this like a Next.js thing?
i assume so
matt was helping me yesterday lol
Unfortunately I don't actually know any Next.js
really
no way
i really thought u knew
I know TypeScript but not Next.js haha
Looks like the issue is the return type of your function
let me check this out
i used this yesterday
if i remove this it works
see
Type (props: Props) => Element | undefined is not assignable to type NextPage<Props, Props> means that you've told it that Home will be a NextPage, but given it something else. I believe that the thing you've given it is unable to be coerced into NextPage because it sometimes returns undefined.
yeah, use a ternary operator to send a fallback component
i kinda understand what u r saying
Does it work if you change the "Refreshing...." into JSX?
nope
is it a huge problem or we can ignore it?
try:
if (!data) return <p>NOOO!!!!</p>
return (
<>
<Head>...
<Head/>
</>
);
this worked

Yeah so you just have to turn "Refreshing...." into an Element like <span>Refreshing....</span> instead of a string
thank you myy brother
may god bless Ben Awad
what??
yeah this is bc you're sending <p></p> when there's no data
once u get data it renders the latter
so it will take a while?
probably, if that's an issue try react-query
hmm i just want it to update when someone donates lol
SWR is more like a wrapper around fetch
yeah that might work. the issue with that is you're re-fetching every n-seconds and the data might have not changed during that time.
not sure if SWR has cashing built-in
or can we ignore it?
oh ok
bc it isnt really a eror
erroor
error
if it isn't critical I'd just re-fetch on demand (refresh the page)
i think there's a way to make the page static and still have updated data
alright ill have to get someone to donate a dollar to test
u could set up a fake api
i forgot abt tht but the dollar helps for the hackathon
it turns out tht is a big problem
send the code with triple ` surrounding it
ok
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import qrcode from "../public/assets/qrcode_bank.hackclub.com.png"
import useSWR from 'swr'
const api_url = 'https://bank.hackclub.com/api/v3/organizations/unite-hacks/donations'
interface Donation {
id: string;
object: string;
href: string;
donor: {
name: string;
};
amount_cents: number;
}
interface Props {
donations: Donation[];
}
const Home: NextPage<Props> = (props) => {
const { data } = useSWR(api_url);
if (!data) return (
<>
<Head>
<title>Unite Hacks Bake Sale</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<div className="flex min-h-screen flex-col items-center justify-center py-2">
<div className='font-extrabold text-[24px]'>Unite Hacks Bake Sale🍪</div>
<Image src={qrcode} alt="qrcode" width={300} height={300}/>
{props.donations.map((donation: Donation) => (
<div key={donation.id}>
<div className='flex text-center background2'>
<span className='text-[18px] background'>DONATION FROM {donation.donor.name}</span>
<span className='text-[18px] background'>${donation.amount_cents/100}.00</span>
</div>
</div>
))}
</div>
</>
)
}
export const getServerSideProps = async () => {
const res = await fetch(api_url);
const data = await res.json();
return {
props: {donations: data}
}
}
export default Home;
const Home: NextPage<Props> = (props) => {
const { data } = useSWR(api_url);
if (!data) {
return <div>
We're sorry there's no data.... (Make a spinner of something)
</div>
}
return (
<>
<Head>
<title>Unite Hacks Bake Sale</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<div className="flex min-h-screen flex-col items-center justify-center py-2">
<div className='font-extrabold text-[24px]'>Unite Hacks Bake Sale🍪</div>
<Image src={qrcode} alt="qrcode" width={300} height={300}/>
{props.donations.map((donation: Donation) => (
<div key={donation.id}>
<div className='flex text-center background2'>
<span className='text-[18px] background'>DONATION FROM {donation.donor.name}</span>
<span className='text-[18px] background'>${donation.amount_cents/100}.00</span>
</div>
</div>
))}
</div>
</>
);
}
but it still dosent return the data
my bad, you're getting the data from the server
huh
like you don't need this const { data } = useSWR(api_url);
bc you are using const res = await fetch(api_url);
but then i would get a error if i remove this
and in theory you're getting props: {donations: data} inside props
correct
is more a problem with typescript rather than a fetch problem
const Home: NextPage<Props> = (props) => {
const { donations } = props;
return (
<>
<Head>
<title>Unite Hacks Bake Sale</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<div className="flex min-h-screen flex-col items-center justify-center py-2">
<div className='font-extrabold text-[24px]'>Unite Hacks Bake Sale🍪</div>
<Image src={qrcode} alt="qrcode" width={300} height={300}/>
{donations.length > 0 && donations.map((donation) => (
<div key={donation.id}>
<div className='flex text-center background2'>
<span className='text-[18px] background'>DONATION FROM {donation.donor.name}</span>
<span className='text-[18px] background'>${donation.amount_cents/100}.00</span>
</div>
</div>
))}
</div>
</>
);
}
wait wat did u change lol
so it should update when someone donates?
let's say someone donates, how in the world is the client (your react code) going to know that?
(you need a way to trigger an "event" on the client)
you could use: websockets to have a real-time update and I'll let u decide if that's too much work
yeah i agree but if i just refresh it works
a good-enough solution would be this #1021224125978656829 message
literally fetch the api every 10 minutes, setup a cashing mechanism and you're done
yeah, the getServerSideProps means get this props from the server (fetch your endpoint)
ok
thank you really appreciate it
np
I haven't done stuff in next lately. Hopefully some React chad might be able to help next time
wanna donate to my hackathon lol
yeah but atleast ur better than me
business is booming
a dollar wont hurt
u should pay me a dollar
i would
our highest donation is 500 dollars lol
heroes don't ask for rewards (I'm vengeance)
wtf r u guys doing to get donations?
its a in-person hackathon
so we need alot of money
now where going to do a bakesale but i have to fix this
to make sure it updates
cool. Feel free to ask if you need help again 😎 🤘