Somewhat new to reactjs. Using Visual Studio Code to build my UI, in reactjs.
I need to pass my data to Application, is this done on this line?
<Application />
I know how to pass data to other components, but now sure how it is passed to a function.
import React, { Component } from 'react';
import { Form, FormText } from 'react-bootstrap';
import Application from './CreateTable';
import fetchQuestionBankData from './RetrieveData';
export class CreateQuestionBank extends Component {
static displayName = CreateQuestionBank.name;
constructor(props) {
super(props);
this.state = {
banks:[],
loading: true,
error: props.bError
}
}
componentDidMount() {
this.setState({ banks: fetchQuestionBankData(5), loading:false }, () => { console.log("Retrieved
data") });
}
static renderTable() {
return (
<Application />
);
}
render() {
let contents = this.state.loading
? <p><em>Loading...</em></p>
: CreateQuestionBank.renderTable();
return (
<div>
<Form.Group>
<h1>Question Bank Creator</h1>
<Form.Group>
<br />
<Form.Group className="col-md-12">
<FormText className="showError">{this.state.error}</FormText>
</Form.Group>
</Form.Group>
{contents}
</Form.Group >
</div>
);
}
}
import React from 'react';
import styled from 'styled-components'
import { useTable } from 'react-table';
const Styles = styled.div`
padding: 1rem;
table {
border-spacing: 0;
border: 1px solid black;
tr {
:last-child {
td {
border-bottom: 0;
}
}
}
th,
td {
margin: 0;
padding: 0.5rem;
border-bottom: 1px solid black;
border-right: 1px solid black;
:last-child {
border-right: 0;
}
}
}
`
This is my function Application that creates the react-table from the data, I want to pass into it.
function Table({ columns, data }) {
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable({ columns, data })
return (
// apply the table props
<table {...getTableProps()}>
<thead>
{// Loop over the header rows
headerGroups.map(headerGroup => (
// Apply the header row props
<tr {...headerGroup.getHeaderGroupProps()}>
{// Loop over the headers in each row
headerGroup.headers.map(column => (
// Apply the header cell props
<th {...column.getHeaderProps()}>
{// Render the header
column.render('Header')}
</th>
))}
</tr>
))}
</thead>
{/* Apply the table body props */}
<tbody {...getTableBodyProps()}>
{// Loop over the table rows
rows.map(row => {
// Prepare the row for display
prepareRow(row)
return (
// Apply the row props
<tr {...row.getRowProps()}>
{// Loop over the rows cells
row.cells.map(cell => {
// Apply the cell props
return (
<td {...cell.getCellProps()}>
{// Render the cell contents
cell.render('Cell')}
</td>
)
})}
</tr>
)
})}
</tbody>
</table>
)
}
function Application(data) {
const qColumns = React.useMemo(
() => [
{
Header: "Question",
columns: [
{
Header: "Type",
accessor: "QuestionTypeAbbrev",
width: 75
},
{
Header: "Points",
accessor: "PointsAwarded",
width: 75
},
{
Header: "Question",
accessor: "QuestionText",
minWidth: 225
}
]
}
],
[]
);
return (
<Styles>
<Table columns={qColumns} data={data} />
</Styles>
)
}
export default Application