Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Sorting a table in React error

user2030942:

I have an application that loads up sizes and stock quantities for particular items, based on user input. Right now, I have two tables loading for each product, one for out of stock items and their sizes and one for in stock items and their sizes. I would like to be able to sort the In Stock Table ascending/descending based on the quantity each size has.

So this picture below kinda shows the output as I have it right now. Two tables, one of them should have sorting capabilities.

My approach to this was to create an InStockTable component, where the sorting logic occurs. I'm using this codepen for reference. The difference between that one and mine is that I'm passing the table data as props to the component, rather than having it hardcoded in the component state.

This is the code for the component that handles each product:


class Product extends React.Component {
constructor(props) {
super(props);
}

renderRow(row) { // this function is for the rows of the out of stock table. These need not to be sorted.
return (

{row.quantity}
{row.size_text}

);
}

renderSortableRow(row) { // this is for the rows that need to be sorted
var row = {
"quantity": row.quantity,
"size": row.size_text
}

return row;
}

renderProductDetails(product) {
const inStockProdStr = [];
const outStockProdStr = [];

if (product.length) { // product is an array
product.forEach(p => {
if (p.quantity == 0) {
outStockProdStr.push(this.renderRow(p))
}
else {
inStockProdStr.push(this.renderSortableRow(p))
}
});
}
else { // product is an object
if (typeof product == 'object') {
Object.keys(product).forEach(id => {
if (product[id].quantity == '0') {
outStockProdStr.push(this.renderRow(product[id]))
}
else {
inStockProdStr.push(this.renderSortableRow(product[id]))
}
}
);
}
}

return (



Out Of stock









{outStockProdStr.map(p => p)}

QtySize



);
}

render() {
return (


{this.props.productId}



{
this.renderProductDetails(this.props.prodStock)
}



);
}

}

This is the code for the In Stock table component:


class InStockTable extends React.Component {
constructor(props) {
super(props);
}

render() {
return (

In stock









{
this.props.stockArr.map((s) => (




))
}

QtySize
{s.quantity}{s.size_text}


);
}

}

If all this worked, I should be able to create a sorting function inside the inStock table component. However, I'm getting the following error:


Error: Objects are not valid as a React child (found: object with keys {quantity, size}). If you meant to render a collection of children, use an array instead.
in tbody (created by Product)
in table (created by Product)
in div (created by Product)
in div (created by Product)
in div (created by Product)
in div (created by Product)
in div (created by Product)
in Product (created by ProductList)
in ProductList (created by RequestForm)
in div (created by RequestForm)
in div (created by RequestForm)
in RequestForm
react-dom.development.js:59:15
The above error occurred in the component:
in tbody (created by Product)
in table (created by Product)
in div (created by Product)
in div (created by Product)
in div (created by Product)
in div (created by Product)
in div (created by Product)
in Product (created by ProductList)
in ProductList (created by RequestForm)
in div (created by RequestForm)
in div (created by RequestForm)
in RequestForm

Any ideas? Is this the right way to sort?



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Sorting a table in React error

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×