Return mapped data in fetch, or map in components?

Which do you prefer and why? Alternatively, what are the use cases for each?
2 Replies
Neto
Neto4mo ago
on data source (fetch) "pure" components are easier to reason with
type SomeData = {id: string, name: string}

async function SomeFunction(): Promise<SomeData[]> {
const data = await fetch('/somewhere')
const json = await data.json()

return json.map(Mapper)
}

function SomeComponent(props: { data: SomeData[] }) {
// whatever
}
type SomeData = {id: string, name: string}

async function SomeFunction(): Promise<SomeData[]> {
const data = await fetch('/somewhere')
const json = await data.json()

return json.map(Mapper)
}

function SomeComponent(props: { data: SomeData[] }) {
// whatever
}
SomeComponent is not tied to SomeFunction now you can change either side and be happy
Jin
JinOP4mo ago
Awesome, thanks. easy to understand.
Want results from more Discord servers?
Add your server