私はリアクションには新しいので、ここでできるだけ多くのコードを表示して、うまくいけばそれを理解しようとしました!基本的には、別のAPIからフェッチしたオブジェクトのプロパティをフォームフィールドに入力したいだけです。オブジェクトはオートフィル減速機に保存されます。たとえば、autoFill.volumeInfo.titleを使用して入力を入力したい場合は、ユーザーが送信する前に値を変更することができます。Reactで編集可能なredux-formフィールドを自動入力するためにpropsを使用するにはどうすればよいですか?
autoFillアクション作成者からmapDispatchPropsを使用しましたが、this.props.autoFillはまだFillFormコンポーネントに未定義として表示されています。私はまた、フォームを提出するために小道具を再び使用する方法についても混乱しています。ありがとう!
マイ減速:
import { AUTO_FILL } from '../actions/index';
export default function(state = null, action) {
switch(action.type) {
case AUTO_FILL:
return action.payload;
}
return state;
}
アクション作成者:autoFillのアクションの作成者を呼び出し
export const AUTO_FILL = 'AUTO_FILL';
export function autoFill(data) {
return {
type: AUTO_FILL,
payload: data
}
}
:
:class SelectBook extends Component {
render() {
return (
....
<button
className="btn btn-primary"
onClick={() => this.props.autoFill(this.props.result)}>
Next
</button>
);
}
}
....
function mapDispatchToProps(dispatch) {
return bindActionCreators({ autoFill }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(SelectBook);
そして、ここでは問題が横たわる実際のフォームです
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { reduxForm } from 'redux-form';
import { createBook } from '../actions/index;
class FillForm extends Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
}
onSubmit(props) {
this.props.createBook(props)
}
handleChange(event) {
this.setState({value: event.target.value});
}
render() {
const { fields: { title }, handleSubmit } = this.props;
return (
<form {...initialValues} onSubmit={handleSubmit(this.onSubmit.bind(this))}>
<input type="text" className="form-control" name="title" {...title} />
<button type="submit">Submit</button>
</form>
)
}
export default reduxForm({
form: 'AutoForm',
fields: ['title']
},
state => ({
initialValues: {
title: state.autoFill.volumeInfo.title
}
}), {createBook})(FillForm)
「this.props.autoFill」はFillFormでは定義されていませんが、そこには使用されていません。 SelectBookで本当に意味がありましたか? –
@BrandonRoberts私は新しいコード/新しい問題でここに新しい質問を追加しました...多分それは少し明確です。ありがとう! http://stackoverflow.com/questions/42624225/how-to-export-redux-form-field-component – nattydodd