2017-03-03 145 views
1

我正在使用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)時,我得到了空對象。我錯過了什麼?

+1

你不需要綁定動作來自己發送。只需傳遞'RegisterActions',就像:connect(mapStateToProps,RegisterActions)(LoginForm)'。然後使用'this.props.somebindedAction' –

回答

0

需要聲明另一個mapDispatchToProps,將您的行動創造者,並與調度

mapDispatchToProps = (dispatch) => (bindActionCreators ({ 
    RegisterActions : RegisterActions 
},dispatch) 

//connect should be changed to 
connect(mapStateToProps, mapDispatchToProps)(LoginForm); 

然後在約束他們的props你將有一個功能RegisterActions,將派遣一個終極版動作

this.props.RegisterActions()