首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将参数传递给react组件

将参数传递给react组件
EN

Stack Overflow用户
提问于 2020-05-07 21:17:35
回答 1查看 21关注 0票数 1

考虑以下组件。

代码语言:javascript
复制
import React from "react"

const AlertBox = ({ type, content }) => {
    if (type === "tip") {
        return (
            <div className="alert is-tip">
                <p className="alert-title">Tip</p>
                <p>{content}</p>
            </div>
        )
    }

    if (type === "important") {
        return (
            <div className="alert is-important">
                <p className="alert-title">Important</p>
                <p>{content}</p>
            </div>
        )
    }
}

export default AlertBox

这允许我(通过使用gatsby-plugin-mdx)像这样使用它:

代码语言:javascript
复制
<AlertBox type="important" content="This is my very important note" />

好的很好。但我真正想要的是这样使用它:

代码语言:javascript
复制
<AlertBox type="important">
    This is my very important note
</AlertBox>

我该如何将其传递给组件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-07 21:23:57

你可以使用儿童道具。

代码语言:javascript
复制
import React from "react"

    const AlertBox = ({ type,children }) => {
        if (type === "tip") {
            return (
                <div className="alert is-tip">
                    <p className="alert-title">Tip</p>
                    <p>{children}</p>
                </div>
            )
        }

        if (type === "important") {
            return (
                <div className="alert is-important">
                    <p className="alert-title">Important</p>
                    <p>{children}</p>
                </div>
            )
        }
    }

    export default AlertBox
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61658790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档