diff --git a/33-lifecycle-lab_v16/src/01-cps.jsx b/33-lifecycle-lab_v16/src/01-cps.jsx index 67ae14b..2a6902c 100644 --- a/33-lifecycle-lab_v16/src/01-cps.jsx +++ b/33-lifecycle-lab_v16/src/01-cps.jsx @@ -9,12 +9,35 @@ import React from 'react'; export default class Cps extends React.Component{ + constructor(props){ + super(props); + this.state = { rate :0 , count : 0 }; + } + + + componentWillMount = () => { + this.timer = setInterval(() => { + this.setState({ rate: this.state.count , count : 0}); + }, 1000); + } + + componentWillUnmount = () => { + clearInterval(this.timer); + } + + count = () => { + + this.setState(oldState => ({count : oldState.count + 1 })); + } + render() { - return ( -
- -

CPS rate: {0}

-
- ); + return (
+ +

CPS rate: {this.state.rate}

+

{this.state.rate > 4 ? "not so fast..." : "faster"}

+
); + } } + +ReactDOM.render(React.createElement(Cps), document.querySelector('main')); diff --git a/33-lifecycle-lab_v16/src/02-cps-with-indicator.jsx b/33-lifecycle-lab_v16/src/02-cps-with-indicator.jsx index ca43dd4..4c0a793 100644 --- a/33-lifecycle-lab_v16/src/02-cps-with-indicator.jsx +++ b/33-lifecycle-lab_v16/src/02-cps-with-indicator.jsx @@ -11,18 +11,46 @@ import React from 'react'; -class Indicator extends React.Component { +function Rect(props){ + const rectStyle = { width: '20px', + height: '20px', + background: props.color}; + return
} -export default class CpsWithIndicator extends React.Component { - render() { - return ( -
- -

CPS rate: {0}

- -
- ); + +export default class Cps extends React.Component{ + + constructor(props){ + super(props); + this.state = { rate :0 , count : 0 }; + } + + + componentWillMount = () => { + this.timer = setInterval(() => { + this.setState({ rate: this.state.count , count : 0}); + }, 1000); + } + + componentWillUnmount = () => { + clearInterval(this.timer); + } + + count = () => { + + this.setState(oldState => ({count : oldState.count + 1 })); + } + + render() { + return (
+ +

CPS rate: {this.state.rate}

+

{this.state.rate > 4 ? "not so fast..." : "faster"}

+ 4 ? "green" : "red"}/> +
) + + } } -} + ReactDOM.render(React.createElement(Cps), document.querySelector('main')); \ No newline at end of file