Skip to content

Instantly share code, notes, and snippets.

@ansonkao
Created December 9, 2015 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ansonkao/f3866250ac5239e2fc94 to your computer and use it in GitHub Desktop.
Save ansonkao/f3866250ac5239e2fc94 to your computer and use it in GitHub Desktop.
Super simple example of React smart vs dumb components
import React, { Component } from 'react';
export default class Fruits extends Component {
render() {
var fruits = this.props.fruits.map(function(fruit){
return (
<li>
<button onClick={this.props.handleFavourite.bind(this, fruit)}>
<span>{fruit} </span>
<span>Fav!</span>
</button>
</li>
);
}.bind(this));
return (
<ul>
{fruits}
</ul>
);
}
}
import React, { Component } from 'react';
import Fruits from './Fruits.js';
export default class FruitSearch extends Component {
constructor() {
super();
this.state = {
fruits: [
"Apple",
"Orange"
],
favourite: null
};
this.handleFavourite = this.handleFavourite.bind(this);
}
handleFavourite(favourite) {
this.setState({
favourite: favourite
});
}
render() {
return (
<div>
<h3>
<span>Favourite Fruit: </span>
{this.state.favourite}
</h3>
<Fruits fruits={this.state.fruits} handleFavourite={this.handleFavourite} />
</div>
);
}
}
@aligajani
Copy link

aligajani commented Dec 4, 2016

In Fruits.js (line 15) you don't have to apply bind(this) if you had used the new ES6 arrow syntax for the function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment