I have a first code that looks like this, it's a console list with on / off buttons:
const ConsoleDisplay = () => {
const statusData = useContext(StatusDataContext);
const [open, setOpen] = useState(false);
const [oneConsoleDataIndex, setOneConsoleDataIndex] = useState();
const handleClose = () => {
setOpen(false);
};
return (
<Fragment>
{statusData.map((data, index) => {
const handleOpen = () => {
setOpen(true);
setOneConsoleDataIndex(index);
};
const displayBtnStatusOncycle =
!timesUp &&
(data['cycle in progress 1'] || //boleen
data['cycle in progress 2'])
? 'var(--btn-green)'
: 'var(--btn-grey)';
const displayBtnStatusOffcycle = timesUp
? 'var(--btn-red)'
: 'var(--btn-grey)';
When I click on one of the consoles, it opens a modal that I can close. At the top right, I can switch consoles without closing the window by clicking on another console. In this modal, I have the same information in a new component:
const ConsoleDetails = ({ setIndex, index, open, close }) => {
const statusData = useContext(StatusDataContext);
const data = statusData[index];
return (
<Card
onClick={() => setIndex(i)}
>
<span>
{data && data.console_nickname
? data.console_nickname
: data && data.console_id}
</span>
</Card>
const displayBtnStatusOncycle =
!timesUp &&
data &&
(data['cycle in progress 1'] || //boleen
data['cycle in progress 2'])
? 'var(--btn-green)'
: 'var(--btn-grey)';
const displayBtnStatusOffcycle = timesUp
? 'var(--btn-red)'
: 'var(--btn-grey)';
There is something I don't understand, why use data && (data['cycle in progress 1'] in the second component instead of (data['cycle in progress 1']? or just {data.console_nickname ? data.console_nickname : data.console_id} ? Why use data &&?