Skip to content

Instantly share code, notes, and snippets.

@Ami777
Created March 3, 2017 11:06
Show Gist options
  • Save Ami777/bca3bffbbb22b9b1be1ab53f9c2aef41 to your computer and use it in GitHub Desktop.
Save Ami777/bca3bffbbb22b9b1be1ab53f9c2aef41 to your computer and use it in GitHub Desktop.
import React from 'react';
import ReactDOM from 'react-dom';
document.addEventListener('DOMContentLoaded', function(){
class Buttons extends React.Component {
constructor(props){
super(props);
let generatedTexts = [];
for( let i = 0; i < 15; i++ ) {
generatedTexts.push( 'Klik!' );
}
this.state = {
texts : generatedTexts
};
}
handleButtonClick = (e, index) => {
const textsCopy = this.state.texts.slice();
textsCopy[index] = 'Kliknięty.';
this.setState({
texts : textsCopy,
});
};
render(){
let buttons = [];
for( let i = 0; i < 15; i++ ){
const newButton = <button
onClick={e => this.handleButtonClick(e, i)}
>
{ this.state.texts[i] }
</button>;
buttons.push( newButton );
}
return <div>
{ buttons }
</div>;
}
}
class App extends React.Component {
render(){
return <Buttons />;
}
}
ReactDOM.render(
<App />,
document.getElementById('app')
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment