从父包装器到子容器反应传递道具

时间:2020-02-13 09:38:17

标签: javascript reactjs styled-components

我需要将道具从包装器传递到子组件

具有这样的结构:

CabinetBlockContainer.jsx

smallChild.jsx

现在,我的 smallChild.jsx 如下:

struct DataModel: Codable {
    let meta: Meta?
    let data: [Datum]?
}

struct Datum: Codable {
    let type, subType, name, detailedName: String?
    let id: String?
    let datumSelf: SelfClass?
    let iataCode: String?
    let address: Address?

    enum CodingKeys: String, CodingKey {
        case type, subType, name, detailedName, id
        case datumSelf = "self"
        case iataCode, address
    }
}

struct Address: Codable {
    let cityName, countryName: String?
}

struct SelfClass: Codable {
    let href: String?
    let methods: [String]?
}

struct Meta: Codable {
    let count: Int?
    let links: Links?
}

struct Links: Codable {
    let linksSelf: String?

    enum CodingKeys: String, CodingKey {
        case linksSelf = "self"
    }
}

和我的 容器如下:

return (
    <CabinetBlockContainer>
      <div className={props.className}>
        <Typography variant="h6" color="primary">
          {props.title}
        </Typography>
        <Typography variant="caption">{props.subtitle}</Typography>
      </div>
      <BalanceSmallBlock balance="1254.51" needIcon={props.needIcon} />
    </CabinetBlockContainer>
  );

我的积木样式很多,这就是为什么我创建了包装器( CabinetBlockContainer )。我需要将道具从CabinetBlockContainer传递到其子组件。我真的不知道这是最佳做法,但看起来很正常。

我试图在文档中找到如何传递像这样的道具,但是有任何解决方案。

2 个答案:

答案 0 :(得分:1)

在包装器中使用{props.children},看来您想做类似this的事情。

答案 1 :(得分:0)

您可以将道具传递给孩子,同时使用新道具克隆

为更好地理解道具和状态的运作方式,请参阅this

这是先前在SO

上回答相同问题的示例