I try to call my function an other component but i have this message :
Invariant Violation: Objects are not valid as a React child (found: object with keys {years, months, days})
My function age :
export const age = date => {
const result = { years: 0, months: 0, days: 0 };
const now = new Date();
let age = parseISO(date);
const years = differenceInYears(now, age);
if (years > 0) {
result.years = years;
age = addYears(age, years);
}
const months = differenceInMonths(now, age);
if (months > 0) {
result.months = months;
age = addMonths(age, months);
}
const days = differenceInDays(now, age);
if (days > 0) {
result.days = days;
}
return result;
};
That not possible to call this function ? Like that :
<Caption>
{age(//key)}
</Caption>
Your function can only return a string, you are returning an object. React doesn't know how to render it.
What you should probably do is this:
export const age = date => {
const result = { years: 0, months: 0, days: 0 };
const now = new Date();
let age = parseISO(date);
const years = differenceInYears(now, age);
if (years > 0) {
result.years = years;
age = addYears(age, years);
}
const months = differenceInMonths(now, age);
if (months > 0) {
result.months = months;
age = addMonths(age, months);
}
const days = differenceInDays(now, age);
if (days > 0) {
result.days = days;
}
return `${result.years} years ${result.months} ${result.days}`
};