2017-08-18 3 views
0

redux-form v6.7.0を使用して非同期にデータをロードしようとしましたが、動作しません。私はこれをlinkと呼んだ。以下は私のサンプルコードです。どんな助けもありがとう。前もって感謝します。データをロードするredux-formを使用して非同期に動作しません

/* reducers should always maintain immutability */ 
 

 
var personInfo = { 
 
    name: "", 
 
    age: "" 
 
}; 
 

 
function PersonInfoReducer(state = personInfo, action) { 
 
    switch (action.type) { 
 
    case "SAVE_PERSON_DATA": 
 
     return Object.assign({}, state, action.payload); 
 
default: 
 
     return state; 
 
    } 
 
} 
 

 
/* save person data action */ 
 
var savePersonData = (data) => { 
 
    return { 
 
    type: "SAVE_PERSON_DATA", 
 
    payload: data 
 
    }; 
 
}; 
 

 
/* form sample component */ 
 
class FormSample extends React.Component { 
 
    componentDidMount() { 
 
    const { initialize, savePersonData } = this.props; 
 

 
    setTimeout(() => { 
 
     var personInfo = { 
 
     name: "Abraham", 
 
     age: 21 
 
     }; 
 
     savePersonData(personInfo); 
 
    }, 3000); 
 

 
    } 
 

 
    componentWillReceiveProps(nextProps) { 
 
    //console.log(nextProps); 
 
    //debugger; 
 
    //this.props.change("personName", nextProps.personInfo.name); 
 
    //this.props.change("personAge", nextProps.personInfo.age); 
 
    } 
 

 
    render() { 
 
    return (
 
     <form> 
 
     <ReduxForm.Field name={"personName"} component="input" type="text" /> 
 
     <ReduxForm.Field name={"personAge"} component="input" type="text" /> 
 
     <h4>Values: </h4>{JSON.stringify(this.props.personInfo)} 
 
     </form> 
 
    ); 
 
    } 
 
} 
 

 
FormSample = ReduxForm.reduxForm({ 
 
    form: 'FormSample', // a unique identifier for this form 
 
})(FormSample); 
 

 
function mapStateToProps(state) { 
 
    const { personInfo } = state; 
 

 
    return { 
 
    initialValues: personInfo, 
 
    personInfo: personInfo 
 
    }; 
 
} 
 

 
function mapDispatchToProps(dispatch) { 
 
    return Redux.bindActionCreators({ 
 
    savePersonData 
 
    }, dispatch); 
 
} 
 

 
FormSample = ReactRedux.connect(mapStateToProps, mapDispatchToProps)(FormSample); 
 

 

 
const allReducers = Redux.combineReducers({ 
 
    personInfo: PersonInfoReducer, 
 
    form: ReduxForm.reducer 
 
}); 
 

 
const store = Redux.createStore(allReducers); 
 

 
ReactDOM.render(<ReactRedux.Provider store={store}><FormSample /></ReactRedux.Provider>, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.7.2/redux.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/5.0.6/react-redux.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux-form/6.7.0/redux-form.min.js"></script> 
 

 
<div id="root"></div>

+0

エラーメッセージは何ですか?そこに何かエラーがありますか? – Kyon

+0

エラーはありません。しかし、値を 'Field'に更新しません。 –

+0

フィールド値を更新するにはredux-formの状態を更新する必要があります。動的に行うことができます。この回答を見る、https://stackoverflow.com/questions/45230531/how-to-programatically-set-redux-form-field-value/45231071#45231071 値をcomponentWillReceivePropsに設定します。 –

答えて

1

それはReduxの状態から新しい小道具を受信したとき、それは初期値を更新するようにあなたは、Reduxのフォームより高次のコンポーネントを呼び出すときtrueenableReinitializeを設定する必要があります。

FormSample = ReduxForm.reduxForm({ 
    form: 'FormSample', 
    enableReinitialize: true 
})(FormSample); 

http://redux-form.com/6.0.2/examples/initializeFromState/

+0

ありがとう、完璧にうまくいった!私はあなたに別の質問があります。フォームの値を編集/再初期化した後、フォームのすべての値を 'redux'ストアと同期させたいと思いました。参考までに、私は別の[質問](https://stackoverflow.com/questions/45779128/edit-field-not-working-managing-form-values-using-redux-store-redux-form)を掲載しました。これで私を助けることができますか?前もって感謝します。 –

+0

あなたの他の質問から、あなたはすでに解決していることが分かります。 –

+0

私はその質問を投稿したときに私はほとんど混乱していませんでした。その後、私はこの記事の中であなたから与えられた解決策を試してみました。あなたの時間のおかげで男!あなたは私の時間を救った。 –

関連する問題