React Native-不变违反对象无效,不能作为React子对象

问题描述 投票:1回答:1

我尝试将我的函数称为其他组件,但我收到此消息:

不变违反:对象作为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> 
javascript reactjs react-native object react-component
1个回答
3
投票

您的函数只能返回一个字符串,您正在返回一个对象。 React不知道如何渲染它。

© www.soinside.com 2019 - 2024. All rights reserved.