A Pen by Etienne Deladonchamps on CodePen.
Created
September 10, 2017 09:39
-
-
Save etienne-dldc/0487b627b018c0a2321465be8e5b23d3 to your computer and use it in GitHub Desktop.
14 - Multiple Counters - SCU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="app"></div> | |
<div id="logger" style="position: fixed; right: 10px; top: 0px"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Logger | |
var loggerEl = document.getElementById('logger'); | |
function log(text) { | |
const line = document.createElement("span"); | |
line.innerText = text; | |
loggerEl.appendChild(line); | |
loggerEl.appendChild(document.createElement("br")); | |
} | |
/////// | |
class Counter extends React.Component { | |
shouldComponentUpdate(nextProps, nextState) { | |
return ( | |
this.props.value !== nextProps.value || | |
this.props.hideIncrement !== nextProps.hideIncrement | |
) | |
} | |
render() { | |
log(`Render Counter number ${this.props.counterIndex} with value ${this.props.value}` ); | |
return ( | |
<div> | |
<span>{this.props.value}</span> | |
{this.props.value > 0 && | |
<button | |
onClick={() => { | |
this.props.onDecrementClicked(); | |
}} | |
> | |
- | |
</button>} | |
{(!this.props.hideIncrement && this.props.value < 10) && | |
<button | |
onClick={() => { | |
this.props.onIncrementClicked(); | |
}} | |
> | |
+ | |
</button>} | |
</div> | |
); | |
} | |
} | |
class Counters extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
counters: [0, 0, 0] | |
}; | |
} | |
increment(counterIndex) { | |
this.setState(prevState => { | |
const copiedCounters = [...prevState.counters]; | |
copiedCounters[counterIndex] += 1; | |
return { counters: copiedCounters }; | |
}); | |
} | |
decrement(counterIndex) { | |
this.setState(prevState => { | |
const copiedCounters = [...prevState.counters]; | |
copiedCounters[counterIndex] -= 1; | |
return { counters: copiedCounters }; | |
}); | |
} | |
render() { | |
const counterSum = this.state.counters.reduce((acc, val) => acc + val, 0); | |
return ( | |
<div> | |
<span>Sum: {counterSum}</span> | |
<br /> | |
<button | |
onClick={() => { | |
// like push be we create a new array | |
this.setState(prevState => ({ | |
counters: [...prevState.counters, 0] | |
})); | |
}} | |
> | |
Add counter | |
</button> | |
<br /> | |
<div> | |
{this.state.counters.map((value, index) => | |
<Counter | |
value={value} | |
onIncrementClicked={() => this.increment(index)} | |
onDecrementClicked={() => this.decrement(index)} | |
hideIncrement={counterSum >= 20} | |
counterIndex={/*used for loggin*/index} | |
/> | |
)} | |
</div> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render(<Counters />, document.getElementById('app')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment