1
votes

React Newbee Here using react redux and redux-form

I want to use data retrieved by App.js inside Form.js Currently I am passing {this.props.data} to <Form /> I can't see or access the App.js props inside <Form /> component , Instead I can just see reduxForm Props , How can I access this.props.data inside <Form /> ?

I have a component where I am fetching the user 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));
2

2 Answers

2
votes

In your case you should only use the reduxForm HOC without connecting to redux (connect() wrapper) :

export default reduxForm({
  form: "fieldArrays"
})(renderForm);

In your form you can access the data from the parent using this.props.formData;

0
votes

You don't need to connect your child component (form.js) to redux in this case as you are passing down the props from the parent component.

When you mapSateToProps() you are overriding the value of this.props.data.

Instead of

export default reduxForm({
  form: "fieldArrays"
})(connect(mapStateToProps, { actions })(FieldArraysForm));

Try

export default renderForm

EDIT:

Also, I think you should be exporting renderForm not reduxForm