Exploring DetailView and App Components
Learn about DetailView and App components in a React product catalog, managing product details and visibility.
We'll cover the following...
We have discussed the CollectionView and ItemView components in detail, but there are still more important components of our application that we need to go through: the DetailView component and the App component.
Let’s start with the DetailView component.
The DetailView component
When a user clicks on one of our products, we would like a panel to slide in from the right of the screen with a detailed view of the product in question. This DetailView component in the DetailView.tsx file is as follows:
export interface IDetailsProps {open: boolean;product: IProduct | null;handleClose(): void;}const Transition = React.forwardRef(function Transition(props: TransitionProps & { children?: React.ReactElement },ref: React.Ref<unknown>,) {return <Slide direction="left" ref={ref} {...props} />;});export class DetailView extends React.Component<IDetailsProps> {constructor(props: IDetailsProps) {super(props);this.handleClose = this.handleClose.bind(this);}render() {return (<div className="full-screen-details-dialogue"><DialogfullScreenopen={this.props.open}TransitionComponent={Transition}><AppBar><Toolbar><IconButtonedge="start"color="inherit"onClick={this.handleClose}aria-label="close"><Close></Close></IconButton></Toolbar></AppBar>// Other screen elements, we aren't showing the complete code here.</Dialog></div>)}handleClose() {console.log(`Details: handleClose()`)this.props.handleClose();}}
In this component:
-
We start with an interface named
IDetailsProps, which is on lines 1-4 of our code. It contains the properties and methods that ourDetailViewcomponent will need. The first property is namedopen, is of typeboolean, and controls whether the fullscreenDialogcomponent is open or closed. The second property is namedproductand will contain the currently selected product. We then have a function namedhandleClose, which is the callback function that is passed into ourDetailViewcomponent by the parent component. -
We then define a constant named ...