0

I have a functional react component that displays information after an api request. Initially the object it displays is blank, since the request hasn't been made. In the object is an array that looks like this:

['US', 'USA', 'United States of America']

When I simply display the array, after the api request, it displays it as a single string on the page with no spaces between ex:

USUSAUnited States of America

Naturally, I want to format it with .join(', ' ) to format the string like US, USA, United States of America, but after I add the .join(', '), it throws an error:

TypeError: props.modalCountry.alt_spellings is undefined

It seems like .join() is trying to run on first render before there is an actual modalCountry object. How do I get this method to not run until the object & array actually exists?

6
  • 1
    We will probably need a bit more of your code to figure this one out. We can't see any of what you are actually doing. Commented Nov 16, 2018 at 16:41
  • @CalebH. My code is literally a functional react component returning {props.modalCountry.alt_spellings} but breaking with {props.modalCountry.alt_spellings.join(', ')} Commented Nov 16, 2018 at 16:47
  • Yes, but how are you getting the alt_spellings loaded? Commented Nov 16, 2018 at 16:49
  • As Ori Drori explained, you can do the check within the functional component itself to ensure you're not calling functions on undefined objects, or you could do a little data sanitation on your response data in the parent component to ensure you're at least passing an array. You can also even still use PropTypes on functional components to also ensure you're not calling array functions (i.e. join) on non-array objects, which would fail equally as badly or worse. Commented Nov 16, 2018 at 16:53
  • is modalCountry from a redux store? why don't you set a default empty array Commented Nov 16, 2018 at 17:04

1 Answer 1

5

In React - Booleans, Null, and Undefined Are Ignored, so you can return null or undefined if the array doesn't exist. Another option is to supply a default array.

Option 1 - Use short circuit evaluation to skip the array.join(), if the array doesn't exist yet:

const Component = ({ arr = [] }) => (
    <div>
        {arr && arr.join()}
    </div>
);

Option 2 - Render null if no array:

const Component = ({ arr = [] }) => (
    <div>
        {arr ? arr.join() : null}
    </div>
);

Option 3 - Supply a default array as value:

const Component = ({ arr = [] }) => (
    <div>
        {arr.join()}
    </div>
);
Sign up to request clarification or add additional context in comments.

4 Comments

There isn't a more elegant solution? That seems rather hacky
These are simple solutions to a simple problem. However, the future is React suspense.
There is nothing hacky about it, peter. You have a variable that is not initialized. You somehow need to work around this.
@OriDrori short circuit evaluation is the best, but It's unfortunate because this component will never be accessed without the array.

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.