React App Kata 2 Typescript
April 26, 2018 ยท View on GitHub
Code for Kata 2 Typescript is available in the app2-ts folder.
Learning aims
The idea here is understand the concept of state and callbacks in a Typescript React component.
Task
Write the Typescript code to:
- Add new products to the listed products
- Be able to remove products from the list of products
React components can have a state alongside props (we only saw props in the first kata). We want to use state in the App component to store the list of products
- In
App.tsxcreate aninterfaceto define the state of<App />:interface AppState { products: Product[]; //imported from `/Models/Product.ts newProductName: string; newProductDescription: string; } - Alter the signature of
<App />to useAppStateclass App extends Component<{}, AppState>
- Currently
productsis defined globally at the top of the file (line 7). Change this so that the list of products is retrieved in the constructor<App /> - Store the list of products in
<App />s state in the constructor.- See adding state to a class
- You'll also have to initialise
newProductNameadnnewProductDescriptionto empty strings
- Create a
<form>to add new products within theadd-productdiv. It should contain:labelfor the new product nameinputfor the new product namelabelfor the new descriptioninputfor the new descriptionbuttonfor form submission
- Make the input fields display their corresponding values stored in
<App />'s state.- Use the
valueattribute, e.g.<input name="newProductName" value={this.state.newProductName} />
- Use the
- We're going to use an event handler to respond to changes in
nameanddescription.- Create a function in
<App />with the signatureonNameChange(event: React.FormEvent<HTMLInputElement>): void - Bind the
thiskeyword to the function:- In the constructor add
this.onNameChange = this.onNameChange.bind(this);
- In the constructor add
- Set the
onChangeattribute in the product name input field toonNameChange- e.g.
<input name="newProductName" onChange={this.onNameChange} />
- e.g.
- Make corresponding changes for the new product description input field
- Create a function in
- Inside the event handler funcitons, update
<App />'s state with the corresponding data- e.g store the value of the new product name input field in
newProductNamein<App />'s state - You can access the value of the input field using
event.currentTarget.value
- e.g store the value of the new product name input field in
- Create a handler function for the
onClickevent of the submit button on the form- Create a function in
<App />with the signatureonSubmit(event: React.FormEvent<HTMLButtonElement>): void - On the first line write
event.preventDefault();to prevent the page from refreshing - Bind the
thiskeyword to the function:- In the constructor add
this.onSubmit = this.onSubmit.bind(this);
- In the constructor add
- Construct a new object of type
ProductusingnewProductNameandnewProductDescriptionin<App />'s state - Add the new
Productobject to the array ofProducts<App />'s state
- Create a function in
- Add a function to remove a product in
<App />- You might find the filter function available in arrays helpful
- In
ProductList.tsx, make the remove product function available to use in all<ProductItem>components and use it when the div with theremoveclass is clicked- You'll need to alter
ProductItemPropsto pass the function to remove an item - You can use the
Functiontype to do this
- You'll need to alter
- Test that you can add products and remove them from the app.