Reputation: 121
I am using card from bootstrap react but it is not rendering anything on the screen, below is my code. Currently it is displaying it but not as intended, I have tried modifying it but it stops working.
I appreciate any help.
function Products(props) {
let params = useParams()
let [ product, setProduct ] = useState()
function loading() {
return <div className="w-25 text-center"><Spinner animation="border" /></div>
}
function Products(products) {
if (products === null) return;
return products.map((product) => (
<ListGroup.Item key={product.Id}>
<Link to={`/products/${product.name}`} key={product.id}> {product.name}
</Link>
</ListGroup.Item>
));
}
let { id, name, description, price, origin, imgUrl } = product;
return (
<>
<h1>Products</h1>
<Card className="align-self-start w-25">
<Card.Body>
<Card.Title>{origin}</Card.Title>
<Card.Text>
<strong>Description:</strong> <span>{description}</span>
<br/>
<ProductContext.Consumer>
{({ products }) => Products(products)}
</ProductContext.Consumer>
</Card.Text>
</Card.Body>
</Card>
</>
);
if (product === undefined) return loading()
return product.id !== parseInt(params.productId) ? loading() : Products()
}
export default Products;
Upvotes: 0
Views: 926
Reputation: 26
I feel like the logic in your code isn't sound. First of all, useState's product
doesn't seem to be used OR set (at least in this code snippet).
The products
is coming from the ProductContext.Consumer
, which we don't know the code for.
A couple things about your code to fix/look into:
const
with useState
useState
getter or setter in this code snippet.import BootstrapComponent as BSComponent from "bootstrap"
or pick a unique name for your component). You have two Products
nested. Whether the scope is sound, name them more purposefully like Products
and ProductsWrapper
or something.My guess, is either you have naming conflicts or the Consumer isn't working as expected. Gotta fix your logic and perhaps move the inner Products function out to simplify things for you.
Upvotes: 1
Reputation: 66
I'm not sure if this is related to your problem, but it seems that if the product is undefined, you do not display the loading()
part. This code is unreachable because it is after the return
statement of your component function.
function Products(props) {
let params = useParams();
let [product, setProduct] = useState();
function loading() {
return (
<div className='w-25 text-center'>
<Spinner animation='border' />
</div>
);
}
if (product === undefined) return loading();
let { id, name, description, price, origin, imgUrl } = product;
return (
<>
<h1>Products</h1>
<Card className='align-self-start w-25'>
<Card.Body>
<Card.Title>{origin}</Card.Title>
<Card.Text>
<strong>Description:</strong> <span>{description}</span>
</Card.Text>
</Card.Body>
</Card>
</>
);
}
export default Products;
Also, it seems that you have name that might conflicts: Try to avoid having a function Products()
inside a function that is already called Products
. My recommendation for this scenario is to create 2 different components and split their code into 2 different files ;-)
Upvotes: 1