33import React from 'react' ;
44import PropTypes from 'prop-types' ;
55import pickBy from 'lodash.pickby' ;
6+ import Perf from 'react-addons-perf' ;
7+
8+ if ( typeof window !== 'undefined' ) {
9+ window . Perf = Perf ;
10+ }
611
712import ArticleList from './ArticleList' ;
813import SearchBar from './SearchBar' ;
914import Timestamp from './Timestamp' ;
1015
11- class App extends React . Component {
16+ class App extends React . PureComponent {
1217
1318 //****** I need the following lines to expose the CONTEXT
1419 //to make the context api work we need to define the context type
@@ -22,25 +27,48 @@ class App extends React.Component {
2227 store : this . props . store
2328 } ;
2429 }
25-
2630 //********
2731
28- state = this . props . store . getState ( ) ;
32+
33+ //**** Subscribing to a portion of the state,
34+ //pretty much just returning what we really need as a state for this particular component
35+ appState = ( ) => {
36+ const { articles, searchTerm} = this . props . store . getState ( ) ;
37+ return { articles, searchTerm} ;
38+ } ;
39+
40+ state = this . appState ( ) ;
41+ //****
42+
2943
3044 onStoreChange = ( ) => {
31- this . setState ( this . props . store . getState ) ;
45+ //this.props.store.getState....we dont need to read directly from the props state but our own implementation
46+ this . setState ( this . appState ) ;
3247 }
3348
3449 componentDidMount ( ) {
3550 //we need to update the component state so the app component state is in sync to the store state
3651 this . subsciptionId = this . props . store . subscribe ( this . onStoreChange ) ;
3752 this . props . store . startClock ( ) ;
53+
54+
55+ //TODO: Leaving this purposely uncommented. Be aware that in production this has to be commented.
56+ //We need consistent measures that's why we need to add this lines so we messure the components at the same point
57+ //Those numbers need to be the same every time we refresh
58+ setImmediate ( ( ) => { Perf . start ( ) ; } ) ;
59+ setTimeout ( ( ) => { Perf . stop ( ) ;
60+ Perf . printWasted ( ) ; } , 5000 ) ;
3861 }
3962
4063 componentWillUnmount ( ) {
4164 this . props . store . unsubscribe ( this . subscriptionId ) ;
4265 }
4366
67+ componentWillUpdate ( nextProps , nextState )
68+ {
69+ console . log ( 'Updating TimeStamp' ) ;
70+ }
71+
4472 //In react we cannot return two elements unless
4573 //Option 1. is in an array so we need to pass a Key elements
4674 // render() {
@@ -68,6 +96,15 @@ class App extends React.Component {
6896 // we can make a StateApi an event emmiter (Flux)
6997 // or manage subscriptions in the same object
7098
99+
100+ //To solve rendering the articles if the properties have not changed
101+ //Just re-render if you need to
102+ //Downside is that we will have to update this method if we have another property
103+ // shouldComponentUpdate(nextProps, nextState){
104+ // return (nextState.articles !== this.state.articles ||
105+ // nextState.searchTerm !== this.state.searchTerm);
106+ // }
107+
71108 render ( ) {
72109 let { articles, searchTerm} = this . state ;
73110 //making the search case insensitive
0 commit comments