33 < head >
44 < meta charset ="utf-8 ">
55 < title > React Tutorial with cats</ title >
6- <!-- Not present in the tutorial. Just for basic styling. -->
76 < link rel ="stylesheet " href ="css/bootstrap.min.css " />
87 < script src ="scripts/react.js "> </ script >
98 < script src ="scripts/react-dom.js "> </ script >
2120class CatApp extends React . Component {
2221 constructor ( props ) {
2322 super ( props ) ;
24- this . handleSubmit = this . handleSubmit . bind ( this ) ;
23+ this . handleSubmit = this . handleSubmit . bind ( this ) ; // Add context
24+ this . handleRemove = this . handleRemove . bind ( this ) ;
2525 this . state = { items : [ ] , text : '' } ;
2626 }
2727
28+ /**
29+ * Handle picture removal.
30+ *
31+ * Passed to CatList component through props,
32+ * so called in CatList but executed in this component.
33+ */
2834 handleRemove ( id ) {
2935 var items = this . state . items ;
3036
4854 return (
4955 < div className = "container" >
5056 < h1 > Cat + React = Recat</ h1 >
51- < CatList items = { this . state . items } handleRemove = { this . handleRemove . bind ( this ) } />
57+ < CatList items = { this . state . items } handleRemove = { this . handleRemove } />
5258 < div className = "row" >
5359 < div className = "cold-md-12 text-center" >
5460 < form onSubmit = { this . handleSubmit } >
@@ -60,6 +66,15 @@ <h1>Cat + React = Recat</h1>
6066 ) ;
6167 }
6268
69+ /**
70+ * Handle adding a new picture.
71+ *
72+ * This will:
73+ * - Add a new record in the list of items
74+ * - Call render() on this component because we update the state
75+ * - Call render() on CatList component because we render its owner
76+ * - React with JSX is smart to know that only one (new) component needs to be rendered in the DOM
77+ */
6378 handleSubmit ( e ) {
6479 e . preventDefault ( ) ;
6580
0 commit comments