Skip to content

Only display icon in parent if there’s the relevant data in child component, but in order to have the data I must click on that icon

Alright, it took me awhile to even formulate the question for this. This is more related to a design strategy rather than anything.

I have a view that contains a table that displays a list of all users.Each row of the table has an icon on the left that will expand (or collapse) a component called UsersTrainingSummary that just brings a summary of some specific data. See image:

enter image description here

Here is the full code of this view component:

import React from 'react';
import { Card, CardContent, CardHeader, Chip, Divider, Grid, Typography } from '@material-ui/core';
import { gridSpacing } from '../../../store/constant';
import TableContainer from '@material-ui/core/TableContainer';
import Table from '@material-ui/core/Table';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import MuiTableCell from '@material-ui/core/TableCell';
import TableBody from '@material-ui/core/TableBody';
import { makeStyles,  withStyles } from '@material-ui/core/styles';
import {Link} from "react-router-dom";
import IconButton from '@material-ui/core/IconButton';
import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown';
import KeyboardArrowUpIcon from '@material-ui/icons/KeyboardArrowUp';
import moment from 'moment';

import UsersTrainingSummary from './UsersTrainningSummary';
import useLocalStorage from 'react-use-localstorage';
import { useQuery } from '@apollo/client';
import ALL_USERS from '../../../graphql/AllUsers';
import Loader from '../../../component/Loader/Loader';

const useStyles = makeStyles({
    table: {
        minWidth: 350,

const TableCell = withStyles({
    root: {
        borderBottom: "none"

function createData(userId, username, firstName, lastName, dateJoined, lastLogin, email, isActive, trainings ) {
    return { userId, username, firstName, lastName, dateJoined, lastLogin, email,isActive, trainings };

const UserDashboard = () => {
    const classes = useStyles();
    const [storage, setStorage] = useLocalStorage('orgId');
    const orgId = storage
    const { data, error , loading} = useQuery(ALL_USERS, {
        variables: {
            orgId: Number(orgId)

    function Row(props){
        const { row } = props;
        const [open, setOpen] = React.useState(false);
        return (
                <TableRow key={row.userId}>
                        <IconButton aria-label="expand row" size="small" onClick={() => setOpen(!open)}>
                            {open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />}
                    <Link to={{pathname: `/users/${row?.userId}`, state: { user: row}}}
                          style={{ textDecoration: 'none' }}>
                            className={classes.root} hover={true}
                            component="th" scope="row">{row?.username}
                    <TableCell>{row?.isActive? <React.Fragment>Yes</React.Fragment> : <React.Fragment>No</React.Fragment>}</TableCell>
                    <TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}>
                        {open && <UsersTrainingSummary userId={row.userId} trainings={row.trainings}/>}
    if(data) {
        let userList = data.organization?.user
        const rows = [];
        if (userList) {
   => {
                rows.push(createData(, user.username, user.firstName, user.lastName, user.dateJoined, user.lastLogin,
          , user.isActive, user.trainings))
        return (
            <Grid container spacing={gridSpacing}>
                <Grid item xs={12}>
                                <Typography component="div" className="card-header">
                                    List of all trainees
                        <CardContent className="p-0">
                                <Table className={classes.table} aria-label="simple table">
                                            <TableCell>First Name</TableCell>
                                            <TableCell>Last Name</TableCell>
                                            <TableCell>Date Joined</TableCell>
                                            <TableCell>Last Login</TableCell>
                                            <TableCell>Is Active</TableCell>

                                        {rows?.length > 0 && => (
                                            <Row key={row?.userId} row={row}/>
        return ( <Loader />)

export default UserDashboard

Because the query that brings the summary data can be quite slow if you try to bring for too many users at once due to the involved tables in the DB having a huge amount of data, and because most of the time you will only need for one or a few users, I decided to use a lazy strategy: only when the user clicks on that arrow icon, the component UsersTrainingSummary is rendered and there I have the graphql query that brings the data from the backend and also do all the necessary logic to render that Collapse component.

What I am trying to achieve now is to only display that icon for users that actually have something to show e.g query does not bring empty results from BE, because as user it feels a little misleading to me to have that icon just so you can click and see a message “oh…nothing here”. However, since I need to execute the query first in order to know if there’s something to display, it doesn’t seem possible to me unless I perform that query first for all users, which is something I really don’t wanna do, as the weight on performance is much bigger than the weight on user friendly interface.

Or, is there a way I can achieve this?



As mentioned in the comment, an alternative to querying the DB in order to find all users who have additional information, could be managing the issue on back-end side, by adding a boolean field (e.g. “hasMoreInfo”) to the table that you use to populate the first view and use that value to decide whether to render or not the icon.