I am rendering a few data items in a row, separated by bullets, sometimes omitting these strings if they aren't available/applicable.
const items = [
age !== null && `age ${age}`,
nationality && countryName(nationality),
/* ... */
];
return <Text>{items.filter(x=>x).join(' • ')}</Text>;
I now want to make the age number bold, and so I've replaced the string in the array with a fragment:
[age !== null && (<Fragment>age <Text style={styles.ageNumber}>{age}</Text></Fragment>)]
Reassembling it, I can no longer do join
, as that only works on strings. Instead, I've interspersed it with string literals,
const intersperse = (xs, sep) => xs.flatMap((x, i) => (i > 0 ? [sep, x] : [x]));
return <Text>{intersperse(items.filter(x => x), ' • ')}</Text>;
Now that I am passing an array to React, rather than combining it in JavaScript, I get the warning that each child in my array needs a key. How do I handle this nicely, other than by raising more warnings by using the index as the key?