2016-07-03 10 views
0

私はredux-formを使用しようとすると解決できません。私はErikrasの定型文を試しています。私はフォームをhandleSubmit(現時点ではconsole.logが動作することを確かめるために)と呼ぶコンポーネントと親にします。ここでは、2:redux-formでparentからhandleSubmitを呼び出す方法

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> 
    ); 
    } 
} 

だから... ...それは私が、私はいくつかの重要な知識が欠けていると思う動作しません。その理由は、フォームコンポーネントがダムであり、ディスパッチ機能を持たないためです。だから、私は特定のフォルダからアクションの作成者をインポートする(いくつかの異なる方法で何回か)これを追加するためにしようと試み:

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

しかし、私はまだ私が欲しいものを得ることはありません。どうしたの?

答えて

0

最後に私は自分で答えを見つけました。実際には、@connectを使わないことに決めました。(これは定型文で使用されているにもかかわらず)廃止されました。また、redux形式のドキュメントの例のようにconnectを使用しました。唯一の変更は親に関係していましたが、私は何かを逃した場合に備えて両方を投稿します。次のコードはうまくいきます。ここで

コードです:

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機能は明らかに約束が含まれています。 私はそれが誰かを助けることを望む:-)

関連する問題