Created
May 10, 2016 15:24
-
-
Save shotaK/909aadffea3fdaf8dcfd22ee19f972a6 to your computer and use it in GitHub Desktop.
React/Redux Container Explanation
This file contains hidden or 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
/** | |
* Created by shota on 5/10/16. | |
*/ | |
import React, {Component} from 'react'; | |
import {connect} from 'react-redux'; | |
import {selectBook} from '../actions/index'; | |
import {bindActionCreators} from 'redux'; | |
class BookList extends Component { | |
renderList() { | |
return this.props.books.map((book) => { | |
return ( | |
<li key={book.title} className="list-group-item">{book.title}</li> | |
) | |
}) | |
}; | |
render() { | |
return ( | |
<ul className="list-group col-sm-4"> | |
{this.renderList()} | |
</ul> | |
) | |
} | |
} | |
function mapStateToProps(state) { | |
// Whatever is returned will show up as props | |
// inside of BookList | |
return { | |
books: state.books | |
} | |
} | |
// Anything returned from this function will end up as props | |
// on the BookList container | |
function mapDispatchToProps(dispatch) { | |
// Whenever selectBook is called, the result should be passed | |
// to all of our reducers | |
return bindActionCreators({selectBook: selectBook}, dispatch); | |
} | |
// Promote BookList from a component to a container - it needs to know | |
// about this new dispatch method, selectBook. Make it available | |
// as a prop | |
export default connect(mapStateToProps, mapDispatchToProps)(BookList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment