我尝试将我的函数称为其他组件,但我收到此消息:
不变违反:对象作为React子对象无效(找到:具有键{年,月,日}的对象)
我的功能年龄:
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;
};
无法调用此函数?像这样:
<Caption>
{age(//key)}
</Caption>
您的函数只能返回一个字符串,您正在返回一个对象。 React不知道如何渲染它。