【发布时间】:2018-02-28 15:31:45
【问题描述】:
React Newbee Here 使用 react redux 和 redux-form
我想在Form.js 内部使用App.js 检索到的数据目前我正在将{this.props.data} 传递给<Form /> 我无法看到或访问App.js 内部的App.js 道具<Form /> 组件,相反我只能见 reduxForm Props ,如何在<Form /> 中访问this.props.data ?
我有一个要获取用户数据的组件:
App.js
import React, { Component } from "react";
import { connect } from "react-redux";
import * as actions from "../actions";
import Form from "./Form";
class Questions extends Component {
componentDidMount() {
this.props.fetchQuestion();
}
render() {
return <Form formData={this.props.data} />;
}
}
function mapStateToProps({ data }) {
return { data };
}
export default connect(mapStateToProps, actions)(Questions);
Form.js
import React, { Component } from "react";
import { connect } from "react-redux";
import { Field, FieldArray, reduxForm } from "redux-form";
import * as actions from "../actions";
class renderForm extends Component {
renderField({ input, label, type, meta: { touched, error } }) {
return (
<div>
<label>{label}</label>
<div>
<input {...input} type={type} placeholder={label} />
{touched && error && <span>{error}</span>}
</div>
</div>
);
}
render() {
const { handleSubmit, pristine, reset, submitting } = this.props;
return (
<form onSubmit={handleSubmit}>
<Field
name="clubName"
type="text"
component={this.renderField}
label="Club Name"
/>
<FieldArray name="questions" component={this.renderQuestion} />
<div>
</div>
</form>
);
}
}
function mapStateToProps({ data }) {
return { data };
}
export default reduxForm({
form: "fieldArrays"
})(connect(mapStateToProps, { actions })(FieldArraysForm));
【问题讨论】:
标签: javascript reactjs redux redux-form