0

Hi I have one component in which I am rendering another component. I am not able to pass data from Agmt component to AgmtTable component. What I want to do is pass details to AgmtTable component, form a table there and display that table in parent Agmt component. I am not able to understand it how can I access it in AgmtTable. Code in Agmt component is as follows.

import React, { Component } from "react";
import { connect } from "react-redux";
import AgmtTable from "./AgmtTable";
import * as AgmtAction from "../actions/AgmtAction";

class AgmtContainer extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  //fetch Agmt details. This is giving me a data in mapStateToProps function below
  componentDidMount() {
    this.props.dispatch(
      AgmtAction.getAgmtsFor(id)
    );
  }

  render() {
    const data = this.props.Agmt;//here I have a data as object
    return (
      <React.Fragment>
        <div>
          <AgmtTable Data={data} />
        </div>
      </React.Fragment>
    );
  }
}

function mapStateToProps(state) {
  return {
    Agmt: state.AgmtsDetails.AgmtsData,// here I have a data
  };
}

export default connect(mapStateToProps)(AgmtContainer);

Code in AgmtTable component as follows.

import React, { Component } from "react";

class AgmtTable extends Component {
  constructor(props) {
    super(props);
    this.state = {
    };
  }

  getHeaader = () => {
    console.log("details", this.state.details);//here I am not getting a data from props
    var tableHeadings = [
      "Agmt ID",
      "Start Date",
      "End Date",
    ];
    return tableHeadings.map((key) => {
      return <th key={key}> {key.toUpperCase()}</th>;
    });
  };

  getRowsData = (e) => {
    return this.props.Data.map((value) => {//in this line I am getting error
      const {
        Agmt_ID,
        Agmt_START_DATE,
        END_DATE,
      } = value;
      return (
        <tr key={Agmt_ID} className="clickable-row active">
          <td> {Agmt_ID} </td>
          <td> {Agmt_START_DATE} </td>
          <td> {END_DATE} </td>
        </tr>
      );
    });
  };

  render() {
    return (
      <div>
        <table
          id="display-table"
          className="table table-bordered table-hover table-responsive table-condensed table-striped table-sm"
        >
          <tbody>
            <tr>{this.getHeaader()}</tr>
            {this.getRowsData()}
          </tbody>
        </table>
      </div>
    );
  }
}

export default AgmtTable;
Nilesh
  • 518
  • 1
  • 9
  • 26
  • What error are you getting in the line ```this.props.Data.map((value) => ... ``` ? – Maniraj Murugan Apr 21 '20 at 15:24
  • @ManirajMurugan I am getting error as - TypeError: this.props.Data.map is not a function – Nilesh Apr 21 '20 at 15:32
  • Can you check whether you are receiving props first by doing a console before the return statement?? Like ```getRowsData = (e) => { console.log(this.props) return ...}``` – Maniraj Murugan Apr 21 '20 at 15:44
  • @ManirajMurugan Nope, I am not getting, Thats the question I have, How will I get it? :( – Nilesh Apr 21 '20 at 15:46
  • I can understand your question, for that we need to debug step by step.. You could atleast tell what data you are getting when doing console.. Unless there is a reproducable example it is hard to tell where exactly the issue is.. If possible create codesandbox to reproduce your issue.. – Maniraj Murugan Apr 21 '20 at 15:50
  • @ManirajMurugan I have changes my code now and having diff issue. Link for new question is https://stackoverflow.com/questions/61348542/how-can-i-set-the-reducers-return-value-to-state-object-in-component-in-react-j Any help please – Nilesh Apr 21 '20 at 16:39

0 Answers0