有条件的内部地图 JSX

Conditional inside map JSX

我想在地图中创建条件渲染。数据取自具有以下结构的 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>
                    }
    )

地图功能代码更改:

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 语句前的花括号

希望对您有所帮助。

编码愉快!!

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

另一个问题是,您没有在 map 体内 return 任何东西,所以对 return 元素也使用 return

Why we can't use if-else inside JSX?

查看此答案以获取解释:

这样写:

<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 个字符串。

检查这个片段:

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'/>