I have this library type application that lists every object owned by a users. Users can select an object and open up a page about the details for that object alone (Or enter its specific URL using a slug). Both components are siblings.
Currently with my set-up, I keep pinging the server for data that is already loaded up in my parent component.
How can I pass an ID of my buckets props to the child component to then only display data pertaining to that JSON object?
Library List:
In this comp, I'm mapping out all user objects known as buckets
const UserBuckets = (props) => {
const { buckets } = props;
const classes = useStyles();
if (!buckets || buckets.length === 0) return <p>Can not find any buckets, make one below!</p>;
return (
<React.Fragment>
<Container maxWidth="md" component="main">
<Grid container spacing={5} alignItems="flex-end">
{buckets.map((buckets) => {
return (
// Enterprise card is full width at sm breakpoint
<Grid item key={buckets.id} xs={12} md={4}>
<Card className={classes.card}>
<CardHeader className={classes.bucketTitle} classes={{ title: classes.bucketTitle }}
title = {buckets.name.substr(0, 50)}
subheader = {"Total Stocks " + buckets.stock_count}
action={
<IconButton aria-label="settings">
<MoreVertIcon />
</IconButton>
}
/>
<Link
color="textPrimary"
href={'dash/' + buckets.slug}
className={classes.link}
>
<CardContent className={classes.cardContent}>
<div className={classes.bucketText}>
<Typography
component="p"
color="textPrimary"
></Typography>
<Typography variant="subtitle1" color="textSecondary">
{buckets.stock_list}...
</Typography>
</div>
</CardContent>
</Link>
</Card>
</Grid>
);
})}
</Grid>
</Container>
</React.Fragment>
);
};
export default UserBuckets;
Specific object from library list:
For this comp, I'm display data for only one bucket.
If there is no state management already loaded for specific bucket object, then the UseEffect will still be in play here (correct me if I'm wrong please).
export default function Bucket() {
const { slug } = useParams();
const classes = useStyles();
const [data, setData] = useState({ bucket: [] });
useEffect(() => {
axiosInstance.get('bucket/' + slug + '/').then((res) => {
setData({ bucket: res.data });
console.log(res.data);
});
}, [setData, slug]);
return (
<Container component="main" maxWidth="md">
<CssBaseline />
<div className={classes.paper}></div>
<div className={classes.heroContent}>
<Container maxWidth="sm">
<Typography
component="h1"
variant="h2"
align="center"
color="textPrimary"
gutterBottom
className = {classes.bucketTitle}
>
{data.bucket.name}
</Typography>
<Typography
variant="h5"
align="center"
color="textSecondary"
paragraph
className = {classes.bucketAbout}
>
{data.bucket.about}
</Typography>
<SymbolInput/>
</Container>
</div>
<div>< DataTables /></div>
</Container>
);
}
Which Hook or state management do I pass to the children pages, without having to load the JSON object again via the server? Thank you for helping me understand.
EDIT:
Got confused with my terminology, here is the parent app that loads the data through GetUserBuckets().
function App()
{
const user = useSelector(selectUser);
return (
<div>
{user ? GetUserBuckets() : AppWelcome()}
</div>
);
}
export default App;
Here is the function that does the actual get call to my database.
export default function GetUserBuckets()
{
const classes = useStyles();
const ListLoading = LoadingComponent(UserBuckets);
const [appState, setAppState] = useState({
loading: true,
posts: null,
});
useEffect(() =>
{
axiosInstance.get('all/buckets/').then((res) =>
{
const allBuckets = res.data;
setAppState({ loading: false, buckets: allBuckets });
console.log(res.data);
});
}, [setAppState]);
return (
<div className={classes.text}>
<h1>Buckets</h1>
<ListLoading isLoading={appState.loading} buckets={appState.buckets} />
<Container className={classes.createBucket}>
<CreateDialog />
</Container>
</div>
);
};