我想了解React和Redux是如何工作的,並且我從this example project看FuelSavingsPage.js。我得到actions
來自mapDispatchToProps
,但我不明白如何state
傳遞給mapStateToProps
或dispatch
如何傳遞給mapDispatchToProps
。這是怎麼發生的?Redux如何將狀態傳遞給mapStateToProps?
import React, {PropTypes} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as actions from '../actions/fuelSavingsActions';
import FuelSavingsForm from '../components/FuelSavingsForm';
export const FuelSavingsPage = (props) => {
return (
<FuelSavingsForm
saveFuelSavings={props.actions.saveFuelSavings}
calculateFuelSavings={props.actions.calculateFuelSavings}
fuelSavings={props.fuelSavings}
/>
);
};
FuelSavingsPage.propTypes = {
actions: PropTypes.object.isRequired,
fuelSavings: PropTypes.object.isRequired
};
function mapStateToProps(state) {
return {
fuelSavings: state.fuelSavings
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(actions, dispatch)
};
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(FuelSavingsPage);