-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ToCode Solution: תרגול מחזור חיים #35
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should certainly be in |
||
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')); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"}/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I would prefer to pass to |
||
</div>) | ||
|
||
} | ||
} | ||
} | ||
|
||
ReactDOM.render(React.createElement(Cps), document.querySelector('main')); |
There was a problem hiding this comment.
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)