bindActionCreators返回空对象 - Redux
问题描述:
我正在使用bindActionCreators来分派操作,但我得到的是空对象。 我有以下代码bindActionCreators返回空对象 - Redux
主要成分
import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import {Row, Form, FormGroup, FormControl, Button, Col} from 'react- bootstrap';
import * as RegisterActions from 'actions/betaPageActions';
export default class LoginForm extends React.Component {
constructor(props) {
super(props);
let { register, dispatch } = this.props;
this.state = register;
this.actions = bindActionCreators(RegisterActions, dispatch);
}
handleChange(item, e) {
const change = this.props.register;
change[item] = e.target.value;
console.log("change", change);
this.actions.handlechanges(change);
}
render() {
return (
<Row>
<Col md={12}>
<Form inline className="login-beta">
<FormGroup controlId="formControlsSelect">
<FormControl componentClass="select" placeholder="I am.."
onChange={this.handleChange.bind(this, 'type')}>
<option value="">I am..</option>
<option value="startup">Startup</option>
<option value="investment_company">Investment Company/Group</option>
<option value="surfer">Individual Investor/Surfer</option>
</FormControl>
</FormGroup>
{' '}
<FormGroup controlId="formInlineEmail">
{' '}
<FormControl type="email" placeholder="Email address"
onChange={this.handleChange.bind(this, 'email')}/>
</FormGroup>
{' '}
<Button type="submit">
I'M IN!
</Button>
</Form>
</Col>
</Row>
);
}
}
function mapStateToProps(state) {
const { register } = state;
return {
register
};
}
LoginForm.propTypes = {
register: React.PropTypes.object,
dispatch: React.PropTypes.func.isRequired
};
export default connect(mapStateToProps)(LoginForm);
下面一个是操作文件
操作
import * as types from 'constants';
import api from 'utils/api/beta';
export function handleChanges(changes) {
return {
type: types.HANDLEREGISTERATTR,
payload: {
changes
}
}
}
当我登录RegisterActions时,我可以看到handleChanges函数,但是当我使用bindActionCreators(RegisterActions,dispatch)时,我得到了空对象。我错过了什么?
答
需要声明另一个mapDispatchToProps
,将您的行动创造者,并与调度
mapDispatchToProps = (dispatch) => (bindActionCreators ({
RegisterActions : RegisterActions
},dispatch)
//connect should be changed to
connect(mapStateToProps, mapDispatchToProps)(LoginForm);
然后在约束他们的props
你将有一个功能RegisterActions
,将派遣一个终极版动作
this.props.RegisterActions()
你不需要绑定动作来自己发送。只需传递'RegisterActions',就像:connect(mapStateToProps,RegisterActions)(LoginForm)'。然后使用'this.props.somebindedAction' –