1

I have a table component that I am trying to populate from Firebase. I have 3 fields that need to populate: Name Date Comment

I need it to add a row for each entry, I have already added the pivot successfully.

When I attempt to populate the table, i get undefined instead of the actual entries....

index.js

import React, { Component, Fragment, useState } from "react";
import { render } from "react-dom";
import axios from "axios";
import firebase from "firebase";

import Header from "./components/Header";
import "./style.css";

const App = () => {
  const [comment, setComment] = useState("New Comments Please");
  const [date, setDate] = useState('');

  const handleClick = e => {
    console.log("Working");
    axios
      .post(`https://lq-time-tracking.firebaseio.com/user/${user()}.json`, {
        comment, date
      })
      .then(function(response) {
        console.log(response);
      })
      .catch(function(error) {
        console.log(error);
      });
  };
  const data = () => {
    axios
      .get("https://lq-time-tracking.firebaseio.com/user.json")
      .then(function(response) {
        console.log(response);
      })
      .catch(function(error) {
        console.log(error);
      });
  };

  const user = () => document.getElementById("theDropdown").value;

  return (
    <div>
      <Header
        comment={comment}
        setComment={setComment}
        date={date}
        setDate={setDate}
        handleClick={handleClick}
        data={data}
      />
    </div>
  );
};

render(<App />, document.getElementById("root"));

header.js

import React from "react";
import styled from "styled-components";

import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import Home from "./Home";

import "../style.css";

const Header = ({
  comment,
  handleClick,
  setComment,
  selectChanged,
  data,
  date,
  setDate
}) => {
  return (
    <Router>
      <nav className="navbar">
        <Link className="nav-item" to="/contact">
          Contact
        </Link>
        <Link className="nav-item" to="/about">
          Data
        </Link>
        <Link className="nav-item" to="/home">
          Home
        </Link>
      </nav>
      <Switch>
        <Route
          exact
          path="/home"
          render={(...props) => (
            <Home
              comment={comment}
              setComment={setComment}
              date={date}
              setDate={setDate}
              handleClick={handleClick}
              data={data}
            />
          )}
        />
      </Switch>
    </Router>
  );
};

export default Header;

home.js

import React, { Fragment } from "react";
import { Container, Row, Col } from "reactstrap";

import Form from "./components/Form";
import Table from "./components/Table";

const Home = ({ comment, setComment, handleClick, data, date, setDate }) => {
  return (
    <Container>
      <Row>
        <Form
          comment={comment}
          setComment={setComment}
          date={date}
          setDate={setDate}
          handleClick={handleClick}
          data={data}
        />
      </Row>
      <Row>
        <Table />
      </Row>
    </Container>
  );
};

export default Home;

table.js

import React from 'react';
import ReactTable from 'react-table'
import 'react-table/react-table.css'
import axios from 'axios';

const Table = (props) => {
  const getData = [axios.get("https://lq-time-tracking.firebaseio.com/user.json").then(function(response) {
        console.log(response);
      }).catch(function(error) {
        console.log(error);
      })]

const data = [{getData}];


  const columns = [{
    id: 'Name',
    Header: 'Name',
    accessor: data.user
  }, {
    Header: 'Date',
    accessor: 'Date',
  }, {
    Header: 'Comment',
    accessor:'Comment' 
  }]

  return <ReactTable
    data={...data}
    columns={columns}
    pivotBy={ ['Date', 'Name']}
  />
}


export default Table;

1 Answer 1

3

You have to modify the below code. API response is not returned to the variable.

import React, { useEffect, useState } from 'react';
import ReactTable from 'react-table'
import 'react-table/react-table.css'
import axios from 'axios';

const Table = (props) => {
  const [data, setData] = useState({});

  useEffect(() => {
    axios.get("https://lq-time-tracking.firebaseio.com/user.json")
      .then(function(response) {
        setData(response.data);
      }).catch(function(error) {
        console.log(error);
      })
  }, []);

  const columns = [{
    id: 'Name',
    Header: 'Name',
    accessor: data.user
  }, {
    Header: 'Date',
    accessor: 'Date',
  }, {
    Header: 'Comment',
    accessor:'Comment' 
  }]

  return <ReactTable
    data={...data}
    columns={columns}
    pivotBy={ ['Date', 'Name']}
  />
}

export default Table;

I have completed based on your data. You have to get the response and pass the response to function component props.

https://stackblitz.com/edit/react-dqnteu

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

Comments

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.