Skip to content

Instantly share code, notes, and snippets.

@empeje
Last active December 17, 2017 11:11
Show Gist options
  • Select an option

  • Save empeje/1afdf36763279236bc637af9d7e3dae6 to your computer and use it in GitHub Desktop.

Select an option

Save empeje/1afdf36763279236bc637af9d7e3dae6 to your computer and use it in GitHub Desktop.
[SNIPPET-13] Samer Buna - ReactJs Getting Started
class Button extends React.Component {
// constructor (props) {
// super(props);
// this.state = { counter: 0 };
// }
state = { counter: 0 }
handleClick = () => {
// this.state.counter++
this.setState((prevState) => ({
counter: prevState.counter + 1
}));
}
render() {
return (
<button onClick={this.handleClick}>
{this.state.counter}
</button>
);
}
}
ReactDOM.render(<Button />, mountNode);
class Button extends React.Component {
// handleClick = () => {
// this.setState((prevState) => ({
// counter: prevState.counter + 1
// }));
// }
render() {
return (
<button
onClick={() => this.props.onClickFunction(this.props.incrementValue)}>
+{this.props.incrementValue}
</button>
);
}
}
const Result = (props) => {
return (
<div>{props.counter}</div>
);
}
class App extends React.Component {
state = { counter: 0 }
incrementCounter = (incrementValue) => {
this.setState((prevState) => ({
counter: prevState.counter + incrementValue
}));
}
render() {
return (
<div>
<Button incrementValue = {1} onClickFunction={this.incrementCounter}/>
<Button incrementValue = {5} onClickFunction={this.incrementCounter}/>
<Button incrementValue = {10} onClickFunction={this.incrementCounter}/>
<Button incrementValue = {100} onClickFunction={this.incrementCounter}/>
<Result counter={this.state.counter}/>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
class Button extends React.Component {
handleClick = () => {
this.props.onClickFunction(this.props.incrementValue)
};
render() {
return (
<button onClick = {this.handleClick}>
+{this.props.incrementValue}
</button>
);
}
}
const Result = (props) => {
return (
<div>{props.counter}</div>
);
}
class App extends React.Component {
state = { counter: 0 }
incrementCounter = (incrementValue) => {
this.setState((prevState) => ({
counter: prevState.counter + incrementValue
}));
}
render() {
return (
<div>
<Button incrementValue = {1} onClickFunction={this.incrementCounter}/>
<Button incrementValue = {5} onClickFunction={this.incrementCounter}/>
<Button incrementValue = {10} onClickFunction={this.incrementCounter}/>
<Button incrementValue = {100} onClickFunction={this.incrementCounter}/>
<Result counter={this.state.counter}/>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
const Card = (props) => {
return (
<div style={{margin: '1em'}}>
<img width="75" src={props.avatar_url} />
<div style={{display: 'inline-block', marginLeft: 10}}>
<div style={{fontSize: '1.25em', fontWeight: 'bold'}}>
{props.name}
</div>
<div>{props.company}</div>
</div>
</div>
);
}
let data = [
{
avatar_url: "https://avatars2.githubusercontent.com/u/11813607?v=3",
name: "Abdurrachman Mappuji",
company: "@ole-vi @open-learning-exchange "
},
{
avatar_url: "https://avatars2.githubusercontent.com/u/13684059?v=3",
name: "dogi",
company: "@ole-vi @open-learning-exchange "
}
];
const CardList = (props) => {
return (
<div>
{props.cards.map(card => <Card {...card} />)}
</div>
);
}
ReactDOM.render(<CardList cards={data}/>, mountNode);
const Card = (props) => {
return (
<div style={{margin: '1em'}}>
<img width="75" src={props.avatar_url} />
<div style={{display: 'inline-block', marginLeft: 10}}>
<div style={{fontSize: '1.25em', fontWeight: 'bold'}}>
{props.name}
</div>
<div>{props.company}</div>
</div>
</div>
);
}
const CardList = (props) => {
return (
<div>
{props.cards.map(card => <Card key={card.id} {...card} />)}
</div>
);
}
class Form extends React.Component {
state = { userName: '' }
handleSubmit = (event) => {
event.preventDefault();
axios.get(`https://api.github.com/users/${this.state.userName}`)
.then(resp => {
this.props.onSubmit(resp.data);
this.setState({ userName: '' })
});
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<input type="text"
value = {this.state.userName}
onChange = {(event) => this.setState({ userName: event.target.value })}
placeholder="Github username" required />
<button type="submit">Add card</button>
</form>
);
}
}
class App extends React.Component {
state = {
cards: []
};
addNewCard = (cardInfo) => {
this.setState(prevState => ({
cards: prevState.cards.concat(cardInfo)
}))
};
render() {
return (
<div>
<Form onSubmit={this.addNewCard}/>
<CardList cards={this.state.cards} />
</div>
)
}
}
ReactDOM.render(<App />, mountNode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment