2017-12-12 5 views
1

単一の入力値を取得してログに記録しようとしています。これは、既存の名前値propを持つ編集フォームです。redux-formとformValueSelectorで入力フォーム値を取得する方法

何らかの理由でフィールド入力の値で状態 '名前'を設定しません。私は自分の問題だと思う接続部分をどのように構造化するのか分かりません。特に、非フォーム状態とフォーム状態の両方を含むようにmapStateToPropsを書く方法については明確ではありません。

部分的に縮小コード:

import { Field, reduxForm, formValueSelector } from 'redux-form'; 
import { connect } from 'react-redux'; 
import { updateStable } from '../../actions/index'; 

const selector = formValueSelector('myEditForm'); 

class EditStuff extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     name: this.props.name 
    }; 
    } 

    componentDidMount() { 
    this.props.initialize({ 
     name: this.props.name || '' 
    }); 
    } 

    componentDidUpdate() { 
    // this.state.name is not getting set from input value 
    this.props.updateLocalActiveStuffData(this.state.name); 

    } 

    render() { 
    const { handleSubmit } = this.props; 
    return (
     <div> 

      <form onSubmit={handleSubmit(this.onSubmit.bind(this))}> 
      <Field 
       label="Name" 
       name="name" 
       class="name" 
       type="text" 
       component={renderField} 
      /> 

      <button type="submit" className="btn btn-primary"> 
       Submit 
      </button> 
      </form> 
     </div> 
     </div> 
    ); 
    } 
} 

function mapStateToProps(state) { 
    return { 
    displayEditForm: state.displayEditForm, //my own non-form related state 
    name: selector(state, 'name') //form input 'name' 
    }; 
} 

export default connect(mapStateToProps, { updateStuff })(
    reduxForm({ 
    form: 'myEditForm' 
    })(EditStuff) 
); 

答えて

0

あなたはreduxFormとformValueSelector名でフォームの名前が異なっているからだと値を取得できない場合、私は思う'StableEditForm' != 'myEditForm'
You have more info on selectors here


フォームを値で初期化する場合は、mapStateToPropsの状態からinitialValues小道具を設定する必要があります。

A great exemple here

function mapStateToProps(state) { 
    return { 
    initialValues: state.displayEditForm, // value of your form 
}; 
} 

私はこれがあなた

+0

フォーム名の違いは、コピー/ペーストタイプミス-I編集した質問だった役立つことを願って。私はあなたの推奨するinitialValuesの設定方法をチェックします。 –

関連する問題