|
1 | 1 | import React, { PropTypes } from 'react';
|
2 |
| -import HelloWorldWidget from '../components/HelloWorldWidget'; |
3 | 2 | import { connect } from 'react-redux';
|
4 |
| -import { bindActionCreators } from 'redux'; |
5 |
| -import Immutable from 'immutable'; |
6 |
| -import * as helloWorldActionCreators from '../actions/helloWorldActionCreators'; |
| 3 | +import HelloWorldWidget from '../components/HelloWorldWidget'; |
| 4 | +import * as actions from '../actions/helloWorldActionCreators'; |
7 | 5 |
|
8 |
| -function select(state) { |
9 |
| - // Which part of the Redux global state does our component want to receive as props? |
10 |
| - // Note the use of `$$` to prefix the property name because the value is of type Immutable.js |
11 |
| - return { $$helloWorldStore: state.$$helloWorldStore }; |
12 |
| -} |
| 6 | +// Which part of the Redux global state does our component want to receive as props? |
| 7 | +const mapStateToProps = (state) => ({ name: state.name }); |
13 | 8 |
|
14 | 9 | // Simple example of a React "smart" component
|
15 | 10 | const HelloWorld = (props) => {
|
16 |
| - const { dispatch, $$helloWorldStore } = props; |
17 |
| - const actions = bindActionCreators(helloWorldActionCreators, dispatch); |
18 |
| - const { updateName } = actions; |
19 |
| - const name = $$helloWorldStore.get('name'); |
| 11 | + const { name, updateName } = props; |
20 | 12 |
|
21 | 13 | // This uses the ES2015 spread operator to pass properties as it is more DRY
|
22 | 14 | // This is equivalent to:
|
23 |
| - // <HelloWorldWidget $$helloWorldStore={$$helloWorldStore} actions={actions} /> |
24 |
| - return ( |
25 |
| - <HelloWorldWidget {...{ updateName, name }} /> |
26 |
| - ); |
| 15 | + // <HelloWorldWidget name={name} updateName={updateName} /> |
| 16 | + return <HelloWorldWidget {...{ name, updateName }} />; |
27 | 17 | };
|
28 | 18 |
|
29 | 19 | HelloWorld.propTypes = {
|
30 |
| - dispatch: PropTypes.func.isRequired, |
31 |
| - |
32 |
| - // This corresponds to the value used in function select above. |
33 |
| - // We prefix all property and variable names pointing to Immutable.js objects with '$$'. |
34 |
| - // This allows us to immediately know we don't call $$helloWorldStore['someProperty'], but |
35 |
| - // instead use the Immutable.js `get` API for Immutable.Map |
36 |
| - $$helloWorldStore: PropTypes.instanceOf(Immutable.Map).isRequired, |
| 20 | + name: PropTypes.string.isRequired, |
| 21 | + updateName: PropTypes.func.isRequired, |
37 | 22 | };
|
38 | 23 |
|
39 | 24 | // Don't forget to actually use connect!
|
40 | 25 | // Note that we don't export HelloWorld, but the redux "connected" version of it.
|
41 | 26 | // See https://github.com/reactjs/react-redux/blob/master/docs/api.md#examples
|
42 |
| -export default connect(select)(HelloWorld); |
| 27 | +export default connect(mapStateToProps, actions)(HelloWorld); |
0 commit comments