React App Kata 4 Typescript
April 13, 2018 ยท View on GitHub
Code for Kata 4 Typescript is available in the app-ts-4 folder.
Learning aims
The idea here is learn how to add a router to our react app.
Task
You are given an app that lists all redgate products by name.
Write the TypeScript/React code to:
- Navigate to each product page
- Be able to share hard links of any product page
- Install
react-router-domand it's TypeScript bindings:- yarn add
react-router-dom@types/react-router-dom
- yarn add
- Add
BrowserRouteraround theAppcomponent inindex.tsx - Create hard links in the
ProductItemcomponent to link to/products/productNamee.g:/products/ReadyRoll - The
<ProductContainer />component displays details about the selected component. Make the<ProductContainer>render ONLY when the location changes to a product name. i.e: when the URL islocalhost:3000/products/ReadyRoll, render<ProductContainer>. - Show the product name given by the URL in the
<ProductContainer>- For
<ProductContainer />to pick up/products/:productName, it'sPropsinterface needs a field with the nameproductName - Access Route Params in React Router v4
- Using parameters in Route path
- NOTE: if you use
this.props.match.params.productNameto get the product name you will have to extend your Props interface to include theRouteComponentProps
import { RouteComponentProps } from 'react-router-dom'; interface Props extends RouteComponentProps<{ productName: string }> { } - For
- Inside
<ProductContainer>find the correct product fromdata.productsand display it using the<ProductComponent>component.