【问题标题】:Conditional inside map JSX条件内部映射 JSX
【发布时间】:2017-10-26 00:44:50
【问题描述】:

我想在地图内创建条件渲染。数据取自具有以下结构的 JSON 文件:

export const products = [   
            {
                "scores": {
                    "quality": 8,
                    "relevancy": {
                        "design": 3,
                        "code": 9,
                        "business": 5
                    }
                },
                "title": "AdCall: Bringing WebRTC to display ads",
                "subtitle": "The first advertising banner with video calls",
                "content": [
                    {
                        "type": "text",
                        "body": "AdCall is a concept for a new kind of online advertising banner. Using WebRTC, it allows for a click to call action with the aim of increasing conversions. It's built with a Node backend and deployed on Heroku."
                    }
                ]
            }
]

所以我通过数组创建了一个地图,然后我想根据类型呈现内容部分。我为此使用了双映射,我需要在第二个映射中使用条件,但它不起作用:

    return (
                <div className="LongWork">
                    {products.map((product, i) => {
                        <div className="product-wrapper" key={i}>
                            <div className="subtitle"><span className="subtitle-span">{product.subtitle}</span></div>
                            <div className="title">{product.title}</div>
                            {product.content.map((content, l) => {
                                <div className="product-content" key={l}>
                                    {if (content.type==="text") {
                                        return (
                                            <div className="productText">
                                                {content.body}
                                            </div>
                                        )} 
                                    }}
                                </div>

                            })}
                        </div>
                    }
    )

【问题讨论】:

    标签: json reactjs jsx


    【解决方案1】:

    地图功能代码变更:

    return(
                    <div className="LongWork">
                        { products.map((product, i) => {
                            return <div className="product-wrapper" key={i}>
                                <div className="subtitle"><span className="subtitle-span">{product.subtitle}</span></div>
                                <div className="title">{product.title}</div>
                                {product.content.map((content, l) => {
                                    return <div className="product-content" key={l}>
                                        if(content.type==="text"){
                                            return (
                                                <div className="productText">
                                                    {content.body}
                                                </div>
                                            )} 
                                        }}
                                    </div>
    
                                })}
                            </div>
                        } )}
    

    在上面的文件中删除 if 语句之前的花括号

    希望对你有帮助。

    编码愉快!!

    【讨论】:

    【解决方案2】:

    使用ternary operator 进行条件渲染,因为if-else 我们不能在JSX 内部使用。

    另一个问题是,您没有在 map 正文中返回任何内容,因此也使用 return 来返回元素。

    为什么我们不能在 JSX 中使用 if-else?

    查看此答案以获得解释:if-else statement inside jsx: ReactJS

    这样写:

    <div className="LongWork">
        {products.map((product, i) => {
            return (
                <div className="product-wrapper" key={i}>
                    <div className="subtitle"><span className="subtitle-span">{product.subtitle}</span></div>
                    <div className="title">{product.title}</div>
                    {product.content.map((content, l) => {
                        return(
                            <div className="product-content" key={l}>
                                {content.type === "text" ?
                                    <div className="productText">
                                        {content.body}
                                    </div>
                                : null}
                            </div>
                        )
                    })}
                </div>
            )
        })}
    </div>
    

    更新:

    使用dangerouslySetInnerHTML 渲染html字符串。

    检查这个sn-p:

    let str = "<div>Hello Ocram</div>"
    
    let App = () => {
       return(
           <div>
              <div dangerouslySetInnerHTML={{ __html: str }}></div>
           </div>
       )
    }
    
    ReactDOM.render(<App/>, document.getElementById('app'))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    
    <div id='app'/>

    【讨论】:

    • 谢谢。 JSON 文件的元素之一是保存为字符串的 HTML 部分。如何在页面中将其呈现为 HTML?
    猜你喜欢
    • 2019-09-11
    • 1970-01-01
    • 2021-09-27
    • 2019-09-10
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多