0

React newbie here; I have a table which displays the simple JSON response I get from my backend. But, I am not able to display some nested properties I have for each item.


What I Tried

I am not sure if I need to use .map function or run a loop, or use any accessor. I have a table filled with a JSON response, but it needs to have another row with the correct nested data being displayed for each row. The data is being sent just fine. I think I am not able to parse it.


My Code

const dispatchPlanColumns = [
  {
    dataField: "id",
    text: "ID",
    sort: true
  },
  {
    dataField: "name",
    text: "Truck",
    sort: true,
    isDummyField: true,
    formatter: (cell, row) => (
      <span>
        {row.truck_type} {row.truck_name}
      </span>
    )
  },
  {
    dataField: "total_trucks",
    text: "NO. of trucks",
    sort: true
  },
  {
    dataField: "weight",
    text: "Weight",
    sort: true
  },
  {
    dataField: "percent_filled",
    text: "Volume utilisation",
    sort: true
  },
  {
    dataField: "origin",
    text: "Route",
    isDummyField: true,
    formatter: (cell, row) => (
      <span>
        {row.origin} -> {row.destination}
      </span>
    )
  },
  {
    dataField: "distance",
    text: "Route Distance",
    sort: true
  },
  {
    dataField: "route_tat",
    text: "Date",
    sort: true
  },
  {
    dataField: "time",
    text: "Dispatch Time",
    sort: true
  },
  {
    dataField: "Action",
    text: "Action",
    sort: true,
    formatter: (row, cell) => <Button color="primary">Raise RFQ</Button>
  }
];

const dispatchListColumns = [
  {
    dataField: "id",
    text: "SKU ID",
    sort: true
  },
  {
    dataField: "name",
    text: "Name",
    sort: true
  },
  {
    dataField: "quantity",
    text: "Quantity",
    sort: true
  },
  {
    dataField: "weight",
    text: "Weight",
    sort: true
  }
];

const LoadingPlan = ({ row }) => {
  const [plan, setPlan] = useState([]);

  useEffect(() => {
    const getNetwork = async () => {
      const data = await getDispatchHistory();
      const plan = await getDispatchHistory();
      setPlan(data);
      setPlan(plan);
    };

    getNetwork();
  }, [setPlan]);

  return (
    <div
      className={"animated slideInDown lightSpeedIn"}
      style={{ marginBottom: 60, marginTop: 20 }}
    >
      <Row>
        <Col lg={7}>
          <DataTable columns={dispatchListColumns} data={plan} />
        </Col>
        <Col lg={4}>
          <DispatchMapWrapper />
        </Col>
      </Row>
    </div>
  );
};

const expandRow = row => <LoadingPlan row={row} />;

export default props => {
  const [data, setData] = useState([]);

  useEffect(() => {
    const getNetwork = async () => {
      const data = await getDispatchHistory();
      setData(data);
    };

    getNetwork();
  }, [setData]);

  return (
    <Row>
      <Col lg={12}>
        <Card>
          <CardHeader>
            <b>Dispatch Plan</b>
          </CardHeader>

          <CardBody>
            <DataTable
              columns={dispatchPlanColumns}
              data={data}
              expandRow={{ renderer: expandRow }}
            />
          </CardBody>
        </Card>
      </Col>
    </Row>
  );
};

This is the data

let data = [
  {
    id: 11,
    truck_name: "20 ft sxl 7 Ton",
    truck_type: "Container",
    items: [
      {
        id: "10",
        name: "Coupling",
        pid: 6,
        quantity: 2,
        length: 5.0,
        width: 4.0,
        height: 3.0,
        volume: 60.0,
        weight: 650.0,
        truck_type: "Container",
        origin: "Delhi",
        date: "2019-09-20T12:00:00",
        destination: "Gaya",
        time: "2019-08-01T17:09:06.106859",
        rtd: false,
        is_dispatched: false,
        uploaded_by: 1
      }
    ],
    comments: "Autogenerated",
    origin: "Delhi",
    destination: "Gaya",
    total_trucks: 1,
    material_type: "Non-Fragile",
    scheduled_date: "2019-09-20T12:00:00",
    offered_price: 0,
    weight: 6500,
    status: "Hold",
    created_on: "2019-08-13T10:06:40.761801",
    route_distance: 1088.821,
    route_tat: "19 Hours 27 Minutes",
    etd: "2019-08-15T14:39:32.261802",
    eta: "2019-09-20T12:00:00",
    route_link:
      "https://www.google.com/maps/embed/v1/directions?&key=AIzaSyBa6popp4h4-uNP98vV_-qhI9-GdHg1uQ8&origin=Delhi&destination=Gaya&mode=driving&language=en",
    percent_filled: "92.85714285714286",
    owner: 1
  },
  {
    id: 12,
    truck_name: "20 ft sxl 6 Ton",
    truck_type: "Container",
    items: [
      {
        id: "7",
        name: "Cover Shaft",
        pid: 3,
        quantity: 2,
        length: 3.0,
        width: 3.0,
        height: 4.0,
        volume: 36.0,
        weight: 500.0,
        truck_type: "Container",
        origin: "Delhi",
        date: "2019-08-10T12:00:00",
        destination: "Kalyan-Dombivli",
        time: "2019-08-01T17:09:05.898876",
        rtd: false,
        is_dispatched: false,
        uploaded_by: 1
      },
      {
        id: "7",
        name: "Cover Shaft",
        pid: 3,
        quantity: 2,
        length: 3.0,
        width: 3.0,
        height: 4.0,
        volume: 36.0,
        weight: 500.0,
        truck_type: "Container",
        origin: "Delhi",
        date: "2019-08-10T12:00:00",
        destination: "Kalyan-Dombivli",
        time: "2019-08-01T17:09:05.898876",
        rtd: false,
        is_dispatched: false,
        uploaded_by: 1
      }
    ]
  }
];

These are some routes info, and the nested item's info within each route. I am not able to display the item's info.

enter image description here

7
  • I see many people are viewing this question, but nobody wants to comment/attempt to asnwer ? Have I asked something too peculiar ? Commented Aug 19, 2019 at 11:44
  • I am trying to use the map function to map every route with the items it came with. I tried this but it is not working const plan = data.map((element) => element.items); Commented Aug 19, 2019 at 12:21
  • 1
    The problem with your question is that there is too much information. Instead of including all the code related to your current issue, you should build a minimal, reproducible example. Check the "How to create a Minimal, Reproducible Example" section of the Help Center for more information. It should be something that anyone can run easily to get a feel of your error. Commented Aug 19, 2019 at 14:24
  • Huh... definitely did not expect giving too much info could be bad. Well, I'll try to minimize my question. Commented Aug 19, 2019 at 14:26
  • You can have too much information, especially if you present that information through code. If you can't explain it with words, and you need to show the code, then creating a minimal, reproducible example is the way to go. That way, more people can help you. Commented Aug 19, 2019 at 14:52

1 Answer 1

1

Following your code, I have found this issue:

      const data = await getDispatchHistory();
      const plan = await getDispatchHistory();
      setPlan(data);
      setPlan(plan);

Are you fetching and setting the state twice? Could this be the problem?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. That was one of the things going wrong my side. I was able to solve this after passing the data after using a delay function. But yes, the error was setting the state twice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.