Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save etienne-dldc/33adc1999123e5361f7c33462e0f3b2b to your computer and use it in GitHub Desktop.
Save etienne-dldc/33adc1999123e5361f7c33462e0f3b2b to your computer and use it in GitHub Desktop.
07 - Multiple Counters v2 - React JSX Clean
var body = document.body;
// State
var counters = [0, 0, 0];
// Render
function render() {
return (
<div>
<button
onClick={function() {
counters.push(0);
renderWithReact();
}}
>
Add counter
</button>
<br />
<div>
{counters.map(function(value, index) {
return (
<div>
<span>{value}</span>
{value > 0 &&
<button
onClick={function() {
counters[index] = value - 1;
renderWithReact();
}}
>
-
</button>}
{value < 10 &&
<button
onClick={function() {
counters[index] = value + 1;
renderWithReact();
}}
>
+
</button>}
</div>
);
})}
</div>
</div>
);
}
function renderWithReact() {
ReactDOM.render(render(), body);
}
// Initial render
renderWithReact();
<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