Skip to content

Instantly share code, notes, and snippets.

@victoraugust
Created September 16, 2016 04:03
Show Gist options
  • Save victoraugust/4e01b5535e228a6f6ebdbcff347d9bbb to your computer and use it in GitHub Desktop.
Save victoraugust/4e01b5535e228a6f6ebdbcff347d9bbb to your computer and use it in GitHub Desktop.
React: counter
import React from "react";
import Message from "./Message";
import Button from "./Button";
class Appblah extends React.Component {
constructor(props) {
super(props);
this.state = { changeNumber: 1 }
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
let changeNumber = this.state.changeNumber;
changeNumber += 1;
this.setState({changeNumber:changeNumber});
}
render() {
return (
<div>
<Message text={this.state.changeNumber} />
<Button text="Add One" onClick={this.handleClick} />
</div>
);
}
}
import React from "react";
const Button = ({text, onClick}) => (
<button onClick={onClick}>{text}</button>
);
Button.propTypes = {
text: React.PropTypes.string.isRequired,
onClick: React.PropTypes.func.isRequired
};
export default Button;
import React, { Component } from 'react';
import Appblah from "./components/";
class Main extends Component {
render() {
return (
<div className="App">
<Appblah />
</div>
);
}
}
export default Main;
import React from "react";
const Message = ({text}) => (
<div>{text}</div>
);
Message.propTypes = {
text: React.PropTypes.number.isRequired
};
export default Message;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment