Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions 33-lifecycle-lab_v16/src/01-cps.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use componentWillMount. Ever. Everything you want to write here is better written in the constructor or in componentDidMount.
(If it uses the DOM it should be in componentDidMount. Otherwise in constructor)

this.timer = setInterval(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should certainly be in componentDidMount

this.setState({ rate: this.state.count , count : 0});
}, 1000);
}

componentWillUnmount = () => {
clearInterval(this.timer);
}

count = () => {

this.setState(oldState => ({count : oldState.count + 1 }));
}

render() {
return (
<div>
<button>Click Fast</button>
<p>CPS rate: {0}</p>
</div>
);
return (<div>
<button onClick={this.count} >Click Fast</button>
<p>CPS rate: {this.state.rate}</p>
<p>{this.state.rate > 4 ? "not so fast..." : "faster"}</p>
</div>);

}
}

ReactDOM.render(React.createElement(Cps), document.querySelector('main'));
50 changes: 39 additions & 11 deletions 33-lifecycle-lab_v16/src/02-cps-with-indicator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div style={rectStyle}></div>
}

export default class CpsWithIndicator extends React.Component {
render() {
return (
<div>
<button>Click Fast</button>
<p>CPS rate: {0}</p>
<Indicator />
</div>
);

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 (<div>
<button onClick={this.count} >Click Fast</button>
<p>CPS rate: {this.state.rate}</p>
<p>{this.state.rate > 4 ? "not so fast..." : "faster"}</p>
<Rect color={this.state.rate > 4 ? "green" : "red"}/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would prefer to pass to Rect the current rate and let it decide on the color. But that may be a matter of taste.

</div>)

}
}
}

ReactDOM.render(React.createElement(Cps), document.querySelector('main'));