#Default values for Component props with Array type

3 messages · Page 1 of 1 (latest)

boreal lark
#

Let's say I export a prop persons that is array of Person type

//components/Person.astro

type Person = {
  name: string;  
  isActive?: boolean;
};

export interface Props {
  persons: Array<Person>;
}

const { persons } = Astro.props;

Now I want isActive to be true by default for each person when I import Astro.props. What is correct syntax for it?

noble hazel
#

Hello, since it's an array it's slightly more complicated, normally you'd set a default value while deconstructing, like such:

const { isActive = true } = Astro.props;

In your case, what you probably want to do is assume that undefined if also true, so when checking for isActive, you could do if (isActive || isActive === undefined)

boreal lark
#

Though not ideal, I think it will do... My actual code has a few dozen types with many properties each so it will be a monstrous component with all those ifs