@@ -825,4 +825,120 @@ class PropsDisplayer extends React.Component {
825825 }
826826 Button . defaultProps = { text : 'I am a button' } ;
827827 ReactDOM . render ( < Button text = "heya" /> , document . getElementById ( 'app' ) ) ;
828+
829+
830+
831+ // THIS.STATE
832+
833+
834+ // setting and accessing a state
835+ import React from 'react' ;
836+ import ReactDOM from 'react-dom' ;
837+ class App extends React . Component {
838+ constructor ( props ) {
839+ super ( props ) ;
840+ this . state = { title : 'Best App' } ;
841+ }
842+ // alternative to constructor
843+ // state = {
844+ // title: 'Best App'
845+ // };
846+ render ( ) {
847+ return (
848+ < h1 >
849+ { this . state . title }
850+ </ h1 >
851+ ) ;
852+ }
853+ }
854+ ReactDOM . render ( < App /> , document . getElementById ( 'app' ) ) ;
855+
856+
857+ // update state; don't forget to bind 'this' !; setState automatically calls render !
858+ import React from 'react' ;
859+ import ReactDOM from 'react-dom' ;
860+ const green = '#39D1B4' ;
861+ const yellow = '#FFD712' ;
862+ class Toggle extends React . Component {
863+ constructor ( props ) {
864+ super ( props ) ;
865+ this . state = { color : green } ;
866+ this . changeColor = this . changeColor . bind ( this ) ;
867+ }
868+ changeColor ( ) {
869+ const newColor = this . state . color == green ? yellow : green ;
870+ this . setState ( { color : newColor } ) ;
871+ }
872+ render ( ) {
873+ return (
874+ < div style = { { background : this . state . color } } >
875+ < h1 > Change my color</ h1 >
876+ < button onClick = { this . changeColor } > Change color</ button >
877+ </ div >
878+ ) ;
879+ }
880+ }
881+ ReactDOM . render ( < Toggle /> , document . getElementById ( 'app' ) ) ;
882+
883+
884+ // react forms (a form is uncontrolled when react doesn't manage it, meaning real DOM is in charge)
885+ class ControlledInput extends React . Component {
886+ constructor ( props ) {
887+ super ( props ) ;
888+ this . state = { input : '' } ;
889+ this . handleChange = this . handleChange . bind ( this ) ;
890+ }
891+ handleChange ( event ) {
892+ this . setState ( { input : event . target . value } ) ;
893+ }
894+ // to avoid binding we can make handleChange an arrow function
895+ // handleChange = (event) => { this.setState({ input: event.target.value }) }
896+ render ( ) {
897+ return (
898+ < div >
899+ < input value = { this . state . input } onChange = { this . handleChange } />
900+ < h4 > Controlled Input:</ h4 >
901+ < p > { this . state . input } </ p >
902+ </ div >
903+ ) ;
904+ }
905+ } ;
906+
907+
908+ // component lifecycle methods
909+ import React from 'react' ;
910+ export class Clock extends React . Component {
911+ constructor ( props ) {
912+ super ( props ) ;
913+ this . state = { date : new Date ( ) } ;
914+ }
915+ startInterval ( ) {
916+ let delay = this . props . isPrecise ? 100 : 1000 ;
917+ this . intervalID = setInterval ( ( ) => {
918+ this . setState ( { date : new Date ( ) } ) ;
919+ } , delay ) ;
920+ }
921+ render ( ) {
922+ return (
923+ < div >
924+ { this . props . isPrecise
925+ ? this . state . date . toISOString ( )
926+ : this . state . date . toLocaleTimeString ( ) }
927+ </ div >
928+ ) ;
929+ }
930+ componentDidMount ( ) {
931+ this . startInterval ( ) ;
932+ }
933+ componentDidUpdate ( prevProps ) {
934+ if ( this . props . isPrecise === prevProps . isPrecise ) {
935+ return ;
936+ }
937+ clearInterval ( this . intervalID ) ;
938+ this . startInterval ( ) ;
939+ }
940+ componentWillUnmount ( ) {
941+ clearInterval ( this . intervalID ) ;
942+ }
943+ }
828944
0 commit comments