Created
December 9, 2015 21:39
-
-
Save ansonkao/f3866250ac5239e2fc94 to your computer and use it in GitHub Desktop.
Super simple example of React smart vs dumb components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.