Vova :
I have working code
const products = this.state.products.map((product, i) =>
product.fields.company.fields.slug === this.props.match.params.slug ?
<Suspense key={i} fallback={<div>Loading...</div>}>
<ProductListItem id={i} key={i} product={product} />
</Suspense>
: null)
return(
<div className="partner-details" style={partnerD}>
<div className="container-xl">
<Button type="button" className="btn btn-secondary" onClick={this.props.history.goBack}>
<i className="fa fa-arrow-left"></i> Get back
</Button>
<ul>
<div className="product-item">
{products}
</div>
</ul>
</div>
</div>
)
But the problem is if product.fields.company.fields.slug
(company.fields.slug) does not exist my code crashes.
How can I add extra ternary operator to check if it product.fields.company
exist before execute this product.fields.company.fields.slug === this.props.match.params.slug
Thanks!
Karim :
if your environment has support for optional chaining you can do this
product?.fields?.company?.fields?.slug === this.props.match.params.slug ? .. : ..
otherwise you need to check that each field is truthy
product && product.fields && product.fields.company && product.fields.company.fields && product.fields.company.fields.slug === this.props.match.params.slug ? .. : ..
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=220841&siteId=1