我也是React和Redux的新手。我正在努力找到它如何工作,没有任何线索。 这是我的商店创建功能:
import { createStore } from 'redux';
import rootReducer from '../reducers/reducers';
export default function configureStore(initialState) {
const store = createStore(rootReducer, initialState);
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers/reducers', () => {
const nextReducer = rootReducer;
store.replaceReducer(nextReducer);
});
}
return store;
}
这是我的index.js与提供者:
const store = configureStore();
ReactDOM.render(
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={Main}>
<IndexRoute component={Intents}/>
<Route path="/intents" component={Intents}/>
<Route path="/intents/:intentId" component={Intent}/>
<Route path="/entities" component={Entities}>
</Route>
</Route>
</Router>
</Provider>,
document.getElementById('root')
);
在主要组件中调用connect():
export class Main extends Component {
render() {
return (
<MuiThemeProvider muiTheme={getMuiTheme()}>
<div>
<div style={styles.container}>
<Header/>
<main style={styles.main}>
{this.props.children}
</main>
<Footer/>
</div>
</div>
</MuiThemeProvider>
);
}
}
Main.propTypes = {
children: React.PropTypes.node
};
function mapStateToProps(state) {
console.log(state);
return {
todos: state.todos
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(TodoActions, dispatch)
};
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Main);
但无论我尝试什么,都不会调用mapStateToProps和mapDispatchToProps。