In React JSX, I create many animal objects. Each animal object is always supposed to have the attirbute genus and color. Example:
const dog = {
genus: 'canis',
color: 'beige
}
What is the best way to do this in react, jsx?
Below is what I did. It works but i was wondedring if there a better way to do this?
export function Animal(attributes) {
const requiredAttributes = [
'genus',
'color',
]
requiredAttributes.map((attribute) => {
if (Object.keys(attributes).indexOf(attribute) === -1) {
throw `Required attribute ${attribute} was not found`
}
})
return attributes;
}
// GOOD
const dog = Animal({
genus: 'canis',
color: 'beige'
})
// ERROR - will throw error because no color attribute provided
const cat = Animal({
genus: 'felis',
})