@@ -3,9 +3,10 @@ import * as React from 'react';
33import Header from './header' ;
44import Button from './button' ;
55import Main from './main' ;
6+ import Menu from './menu' ;
67
78interface AppState {
8- items : string [ ] ,
9+ listItems : string [ ] ,
910 disabled : boolean ;
1011}
1112
@@ -14,51 +15,58 @@ export default class App extends React.Component<AppProps, AppState> {
1415 super ( props ) ;
1516 // We initialise its state by using the `props` that were passed in when it
1617 // was first rendered. We also want the button to be disabled until the
17- // component has fully mounted on the DOM
18- this . state = { items : this . props . items , disabled : true } ;
18+ // App component has fully mounted on the DOM
19+ this . state = { listItems : this . props . listItems , disabled : true } ;
1920 }
2021
21- // Once the component has been mounted, we can enable the button
22+ // Once the App component has been mounted, we can enable the button
2223 componentDidMount ( ) {
23- this . setState ( { disabled : false } ) ;
24+ this . setState ( { disabled : false } ) ;
2425 }
2526
2627 // Update the state whenever its clicked by adding a new item to
2728 // the list - imagine this being updated with the results of AJAX calls, etc
2829 handleAdd = ( ) => {
29- this . setState ( {
30- items : this . state . items . concat ( 'Item # ' + this . state . items . length )
31- } ) ;
30+ this . setState ( prevState => ( {
31+ listItems : prevState . listItems . concat ( 'Item ' + prevState . listItems . length )
32+ } ) ) ;
3233 }
3334
3435 handleSort = ( ) => {
35- this . setState ( {
36- items : this . state . items . sort ( )
37- } ) ;
36+ this . setState ( prevState => ( {
37+ listItems : prevState . listItems . sort ( )
38+ } ) ) ;
3839 }
3940
4041 render ( ) {
41- const { items } = this . state ;
42+ const { menuItems } = this . props ;
43+ const { listItems, disabled } = this . state ;
4244
43- return ( < div className = "container" >
44- < Header />
45+ return ( < div >
46+ < Menu items = { menuItems } />
4547 < Main >
48+ < Header
49+ title = "Hello React"
50+ sub = "This is an example using React & TypeScript"
51+ />
4652 < ul >
47- { items . map ( ( item , i ) =>
53+ { listItems . map ( ( item , i ) =>
4854 < li key = { i } > { item } </ li >
4955 ) }
5056 </ ul >
5157 < Button
5258 onClick = { this . handleAdd }
53- disabled = { this . state . disabled }
59+ disabled = { disabled }
5460 type = "primary"
55- value = "Add Item" />
56- < br />
61+ text = "Add Item"
62+ />
63+ < span > </ span >
5764 < Button
5865 onClick = { this . handleSort }
59- disabled = { this . state . disabled }
66+ disabled = { disabled }
6067 type = "warning"
61- value = "Sort Items" />
68+ text = "Sort Items"
69+ />
6270 </ Main >
6371 </ div > ) ;
6472 }
0 commit comments