反应原生道具类型的对象

时间:2017-11-02 07:36:50

标签: reactjs react-native

我正在尝试为我的类的道具创建一个类型,但我不确定是否应该定义对象道具属性,或者我应该将对象定义为对象? 这是我的班级

class MyClass extends Component<MyProps> { ... }

这是myProps

type MyProps = {
  title: string,
  author: {
    id: number,
    username: string,
    email: string
  }
}

这样做是否正确?或者我应该将author定义为对象?

我的想法是,在类中我使用author的属性,而我从不使用对象author本身。所以我有点困惑。 谢谢!

1 个答案:

答案 0 :(得分:0)

如果您希望您的组件单独接收这些author属性,您可以这样做,但我会采用另一种方法将author定义为您想要的属性的形状它有。

看起来像这样:

static propTypes = {
  author: PropTypes.shapeOf({
    id: PropTypes.number,
    username: PropTypes.string,
    email: PropTypes.string
  })
};