Code for Kata 2 Typescript is available in the app2-ts folder.
The idea here is understand the concept of state
and callbacks in a Typescript React component.
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.tsx
create aninterface
to define the state of<App />
:interface AppState { products: Product[]; //imported from `/Models/Product.ts newProductName: string; newProductDescription: string; }
- Alter the signature of
<App />
to useAppState
class App extends Component<{}, AppState>
- Currently
products
is 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
newProductName
adnnewProductDescription
to empty strings
- Create a
<form>
to add new products within theadd-product
div. It should contain:label
for the new product nameinput
for the new product namelabel
for the new descriptioninput
for the new descriptionbutton
for form submission
- Make the input fields display their corresponding values stored in
<App />
's state.- Use the
value
attribute, e.g.<input name="newProductName" value={this.state.newProductName} />
- Use the
- We're going to use an event handler to respond to changes in
name
anddescription
.- Create a function in
<App />
with the signatureonNameChange(event: React.FormEvent<HTMLInputElement>): void
- Bind the
this
keyword to the function:- In the constructor add
this.onNameChange = this.onNameChange.bind(this);
- In the constructor add
- Set the
onChange
attribute 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
newProductName
in<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
onClick
event 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
this
keyword to the function:- In the constructor add
this.onSubmit = this.onSubmit.bind(this);
- In the constructor add
- Construct a new object of type
Product
usingnewProductName
andnewProductDescription
in<App />
's state - Add the new
Product
object to the array ofProduct
s<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 theremove
class is clicked- You'll need to alter
ProductItemProps
to pass the function to remove an item - You can use the
Function
type to do this
- You'll need to alter
- Test that you can add products and remove them from the app.