I have a question regarding data passing between parent/child element.
The parent element StylePage retrieves data via SWR which retrieves the data without problem and the application is able to display it with a Text. Problem is that it isn't being passed to the child element (StyleCard). The same goes with currentAssets
. What have I missed here that might cause the props not being passed to StyleCard?
// StylePage
export default function StylePage(): JSX.Element {
const { data } = useSWR(`/api/styles/style-number/${styleNumber}`);
const currentAssets = useMemo(() => {
const result: IAsset[] = data?.assets?.map(x => {
return {
perspective_number: x.perspective.number,
storage_url: x.file.storageUrl
} as IAsset
})
return result;
}, [data?.assets]);
return (
<Stack spacing={6}>
<Stack ml="100px" minWidth="480px" maxWidth="480px">
<StyleCard
assets={currentAssets} // Has value but doesn't display
styleNumber={data?.styleNumber} /> // Has value but doesn't display
<Text>{data?.name}</Text> // Displays value
</Stack>
</Stack>
);
}
// StyleCard
export interface IStyleCard{
assets: IAsset[],
styleNumber: number
}
export function StyleCard(
props: IStyleCard
): JSX.Element {
return (
<Stack>
<Text> Props Assets {JSON.stringify(props.assets)}</Text> // Props.assets is empty
<Text> Props styleNumber {props.styleNumber}</Text> // props.styleNumber is blank
</Stack>
);
}
Edit:
There's a global configuration SWRConfig
(source) which is wrapped around the top layer component. That's why fetcher isn't specified in StylePage.
Also fixing a typo in the code sample.
<SWRConfig
value={{
refreshInterval: 3000,
fetcher: (url) =>
axios
.get(url)
.then((res) => res.data),
}}
>
...
</SWRConfig>