2016-07-03 24 views
0

我有一個問題,我無法解決嘗試使用redux-form。我正在嘗試Erikras樣板。我希望表單是一個組件,並且父級要調用handleSubmit(目前只有一個console.log來確認它是否有效)。這裏,兩個:如何調用handle從提交父母形式爲

import React, {Component, PropTypes} from 'react'; 
import Helmet from 'react-helmet'; 
import {initialize} from 'redux-form'; 
import {connect} from 'react-redux'; 
import * as membersActions from 'redux/modules/members'; 
import {isLoaded, loadMembers} from 'redux/modules/members'; 
import { DashboardList } from 'components'; 
import { DashboardHeader } from 'components'; 
import { DashboardAdding } from 'components'; 
import { asyncConnect } from 'redux-async-connect'; 

@asyncConnect([{ 
    deferred: true, 
    promise: ({store: {dispatch, getState}}) => { 
    if (!isLoaded(getState())) { 
     return dispatch(loadMembers()); 
    } 
    } 
}]) 
@connect(
    state => ({ 
    members: state.members.data, 
    error: state.members.error, 
    loading: state.members.loading 
    }), 
    {...membersActions, initialize }) 
export default class Dashboard extends Component { 

    static propTypes = { 
    initialize: PropTypes.func.isRequired, 
    members: PropTypes.array, 
    loadMembers: PropTypes.func.isRequired 
    } 

    handleSubmit = (data) => { 
    console.log(data); 
    this.props.initialize('dashAdding', {}); 
    } 

    handleInitialize =() => { 
    this.props.initialize('dashAdding', { 
     pseudo: 'Pibo', 
     email: '[email protected]' 
    }); 
    } 

    render() { 
    const {members} = this.props; 
    return (
     <div className="container"> 
     <h1>Dashboard</h1> 
     <Helmet title="Dashboard"/> 
     <DashboardHeader /> 
     <div> 
      <DashboardList members={members}/> 
      <h3>Ici commence le form</h3> 
      <div style={{textAlign: 'center', margin: 15}}> 
      <button className="btn btn-primary" onClick={this.handleInitialize}> 
      <i className="fa fa-pencil"/> Initialize Form 
      </button> 
      </div> 
     </div> 
     <DashboardAdding onSubmit={this.handleSubmit}/> 
     <p>Bleeeeah!!</p> 
     </div> 
    ); 
    } 
} 

和這裏的孩子:

import React, {Component, PropTypes} from 'react'; 
import {reduxForm} from 'redux-form'; 
import {connect} from 'react-redux'; 
import {bindActionCreators} from 'redux'; 
import memberValidation from './memberValidation'; 

@reduxForm({ 
form: 'dashAdding', 
fields: ['pseudo', 'email'], 
    validate: memberValidation 
}) 

export default class DashboardAdding extends Component { 
    static propTypes = { 
    fields: PropTypes.object.isRequired, 
    handleSubmit: PropTypes.func.isRequired, 
    resetForm: PropTypes.func.isRequired 
    } 

    render() { 
    const { 
     fields: { pseudo, email}, 
     handleSubmit, 
     resetForm 
    } = this.props; 
    const renderInput = (field, label) => 
    <div className={'form-group' + (field.error && field.touched ? ' has-error' : '')}> 
     <label htmlFor={field.name} className="col-sm-2">{label}</label> 
     <div className={'col-sm-8 '}> 
      <input type="text" className="form-control" id={field.name} {...field}/> 
     </div> 
     </div>; 

    return (
    <div> 
     <form className="form-horizontal" onSubmit={handleSubmit}> 
      {renderInput(pseudo, 'Full Name')} 
      {renderInput(email, 'Email', true)} 
      <div className="form-group"> 
      <div className="col-sm-offset-2 col-sm-10"> 
       <button className="btn btn-success" onClick={handleSubmit}> 
       <i className="fa fa-paper-plane"/> Submit 
       </button> 
       <button className="btn btn-warning" onClick={resetForm} style={{marginLeft: 15}}> 
       <i className="fa fa-undo"/> Reset 
       </button> 
      </div> 
      </div> 
     </form> 
     </div> 
    ); 
    } 
} 

所以......它不工作,我想我錯過了一些重要的知識。我認爲這是因爲表單組件是愚蠢的,它沒有調度功能。所以,我tryed到(以幾種不同的方式幾次)添加這個從特定的文件夾導入行動的創建者:

@connect(() => ({}), 
    dispatch => actionCreators(dispatch) 
) 

但我不還是得到我想要的東西。有什麼問題?

回答

0

所以,最後我自己找到了答案。事實上,我決定不使用@connect,儘管它仍被用在樣板文件中(儘管它仍然用在樣板文件中),並且使用connect,就像在redux形式的文檔中的例子。唯一的變化與父母有關,但我會在他們兩人的情況下發布,以免我錯過了一些東西。下面的代碼很好。

下面是代碼:

import React, {Component, PropTypes} from 'react'; 
import Helmet from 'react-helmet'; 
import {initialize} from 'redux-form'; 
import {connect} from 'react-redux'; 
import * as membersActions from 'redux/modules/members'; 
import {isLoaded, loadMembers} from 'redux/modules/members'; 
import { DashboardList } from 'components'; 
import { DashboardHeader } from 'components'; 
import { DashboardAdding } from 'components'; 
import { asyncConnect } from 'redux-async-connect'; 

@asyncConnect([{ 
    deferred: true, 
    promise: ({store: {dispatch, getState}}) => { 
    if (!isLoaded(getState())) { 
     return dispatch(loadMembers()); 
    } 
    } 
}]) 

class Dashboard extends Component { 

    static propTypes = { 
    members: PropTypes.array, 
    error: PropTypes.string, 
    loading: PropTypes.bool, 
    addMember: PropTypes.func, 
    initialize: PropTypes.func.isRequired, 
    newMemberData: PropTypes.object 
    } 

    handleSubmit = (data, dispatch) => { 
    dispatch(addMember(JSON.stringify(data))); 
    this.props.initialize('dashboardForm', {}); 
    } 

    handleInitialize =() => { 
    this.props.initialize('dashboardForm', { 
     pseudo: 'Pibo', 
     email: '[email protected]' 
    }); 
    } 

    render() { 
    const {members} = this.props; 
    return (
     <div className="container"> 
     <h1>Dashboard</h1> 
     <Helmet title="Dashboard"/> 
     <DashboardHeader /> 
     <div> 
      <DashboardList members={members}/> 
      <h3>Ici commence le form</h3> 
      <div style={{textAlign: 'center', margin: 15}}> 
      <button className="btn btn-primary" onClick={this.handleInitialize}> 
       <i className="fa fa-pencil"/> Initialize Form 
      </button> 
      </div> 
     </div> 
     <DashboardAdding onSubmit={this.handleSubmit}/> 
     </div> 
    ); 
    } 
} 

function mapStateToProps(state) { 
    return { 
    members: state.members.data, 
    error: state.members.error, 
    loading: state.members.loading, 
    newMemberData: state.addSingleMember.data 
    }; 
} 

function matchDispatchToProps(dispatch) { 
    return bindActionCreators({ 
    addActions, 
    initialize: initialize 
    }, dispatch); 
} 

export default connect(mapStateToProps, matchDispatchToProps)(Dashboard); 

...和子組件:

import React, {Component, PropTypes} from 'react'; 
import {reduxForm} from 'redux-form'; 
import memberValidation from './memberValidation'; 

class DashboardAdding extends Component { 
    static propTypes = { 
    fields: PropTypes.object.isRequired, 
    handleSubmit: PropTypes.func.isRequired, 
    resetForm: PropTypes.func.isRequired 
    } 

    render() { 
    const { 
     fields: { pseudo, email}, 
     handleSubmit, 
     resetForm 
    } = this.props; 
    const renderInput = (field, label) => 
     <div className={'form-group' + (field.error && field.touched ? ' has-error' : '')}> 
     <label htmlFor={field.name} className="col-sm-2">{label}</label> 
     <div className={'col-sm-8 '}> 
      <input type="text" className="form-control" id={field.name} {...field}/> 
      {field.error && field.touched && <div className="text-danger">{field.error}</div>} 
     </div> 
     </div>; 

    return (
     <div> 
     <form className="form-horizontal" onSubmit={handleSubmit.bind(this)}> 
      {renderInput(pseudo, 'Pseudo')} 
      {renderInput(email, 'Email', true)} 
      <div className="form-group"> 
      <div className="col-sm-offset-2 col-sm-10"> 
       <button className="btn btn-success" onClick={handleSubmit}> 
       <i className="fa fa-paper-plane"/> Submit 
       </button> 
       <button className="btn btn-warning" onClick={resetForm} style={{marginLeft: 15}}> 
       <i className="fa fa-undo"/> Reset 
       </button> 
      </div> 
      </div> 
     </form> 
     </div> 
    ); 
    } 
} 
export default reduxForm({ 
    form: 'dashboardForm', 
    fields: ['pseudo', 'email'], 
    validate: memberValidation, 
    asyncBlurFields: ['email'] 
})(DashboardAdding); 

使用addMember功能明顯包含了一個承諾。我希望它能幫助某人:-)