React App Kata 3 Typescript
May 15, 2018 ยท View on GitHub
Code for Kata 3 Typescript is available in the app3-ts folder.
Learning aims
The idea here is to keep learning the concept of state, props, callbacks and React lifecycles in React.
Task
Write the TypeScript/React code to:
- Filter products by name
- Show/Hide product descriptions
Note: Remember you can run yarn lint as you develop to see all linting errors as you work
Filter products
- In
App.tsxAdd a<form>for the product name filter infilter-productsdiv. It should contain:labelfor product nameinputfor filtering by name
- Add
productNameToFiltertoAppStatenear the top ofApp.tsx- Remember to update the initial state object to initialise
productNameToFilter
- Remember to update the initial state object to initialise
- Add a handler function for the
onChangeevent of the input of the form.- Set
productNameToFilterinApp's state to the value from the input field - It might be helpful to look at the existing input fields as an example
- Set
- The input filed for setting the product name to filter must be synced with the component's state
- Set the product filter's input field value to
productNameToFilterinApp's state - e.g:
<input ... value={this.state.productNameFilter}/>
- Set the product filter's input field value to
- Filter products in the
rendermethod based on filter input.- You could do filter with Typescript array object
Show / Hide products
The idea is to have products be collapsible.
- Change
ProductComponentso that descriptions are not shown.- Do this by adding a
booleanflag toProductComponentState(for example:showDescription) - A very common pattern in React is conditional rendering, here are some examples:
{condition? <div>foo</div>: null}{condition? <div>foo</div>: <div>bar</div>}{condition && <div>hello</div>}
- Do this by adding a
- Add a
+or-component next to the product name and toggle it on click- It should show or hide the product description.
- Listen to
onClickon the component you just created and update your flag accordingly.