1

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>&nbsp; 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!

3
  • You could use (product.fields && product.fields.company && product.fields.company.fields && product.fields.company.fields.slug) === this.props.match.params.slug ... but then you'll have another problem because you're not doing anything with the result of the ternary operation. Commented Mar 11, 2020 at 15:23
  • 1
    Sometimes it's best not to use a ternary expression -- code becomes hard to read. I suggest just using an if statement or 2 to make sure of this Commented Mar 11, 2020 at 15:23
  • You can use the filter function this.state.products.filter(...).map(...) to remove all elems without a specific field. Commented Mar 11, 2020 at 15:34

3 Answers 3

2

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 ? .. : ..
Sign up to request clarification or add additional context in comments.

Comments

2

Use optional-chaining ( Babel plugin )

product.fields?.company?.fields?.slug

Or make use of the || operator :

(((product.fields || {}).company || {}).fields || {}).slug

And consider wrapping your compoennt in an error boundary so your app won't crash when there's this kind of errors.

Comments

2

In line 2 you can do: (product && product.fields && product.fields.company && product.fields.company.fields && product.fields.company.fields.slug && this.props && this.props.match && this.props.match.params && this.props.match.params.slug && product.fields.company.fields.slug === this.props.match.params.slug) ?

or use optional chaining.

2 Comments

You've got two product.fields.company in there.
Thank you, fixed it now and added checks for this, still I think if at all possible optional chaining is much more readable and preferred way of doing this.

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.