javascript - React.js 将多个 json 值传递给一个子参数

标签 javascript json reactjs

我有一个 json 对象:

"tags": ["salmons","kitchenaccident"]

我有一个通过 JSON 进行解析的组件..

var entryNodes = this.props.data.data.map(function(entry) {
       return (
         <EntryComponent
             id={entry.user.id}
             img={entry.images.thumbnail.url}
             likes={entry.likes.count}
             link={entry.link}
             username={entry.user.username}
             caption={entry.caption.text}
             tag={entry.tags}/>
       );

但是,它会将其作为一个串联字符串传递:salmonskitchenaccident。

如果我希望它呈现为“#salmons #kitchenaccident”..什么是最好的解决方案。

var EntryComponent = React.createClass({
render: function() {
    return(
            <div className="entry">
                <div className="entry-left">
                    <img className="img-rounded" src={this.props.img} />
                    <div className="like-section">
                        <img className="heart" src="./img/heart.png" />
                        <span className="likes">{this.props.likes}</span>
                    </div>
                </div>
                <div className="entry-right">
                    <h4><a href={this.props.link}>{this.props.username}</a>    </h4>
                    <p>{this.props.caption}</p>
                    <p className="hash">{this.props.tag}</p>
                </div>
            </div>
        );
    }
});

最佳答案

在这一行中:

<p className="hash">{this.props.tag}</p>

您尝试立即显示一个数组,这隐式调用 toString() tags的方法大批。您需要map数组中的项目到 HTML 节点,如下所示:

<p className="hash">{this.props.tag.map( tag => (<span>{ '#' + tag }</span>) }</p>

关于javascript - React.js 将多个 json 值传递给一个子参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36048863/

相关文章:

javascript - 捕获内容安全策略 (CSP) 错误

javascript - 如何让 Accordion 按钮的高度均匀地填充剩余空间

javascript - CSS或jQuery悬停效果增加固定框显示绝对位置放大版

javascript - 页面加载后如何检查RadioButton-Item是否被选中?

java - 如何使用 Jackson 解析对象数组?

javascript - Google Place API - 接收数据的 jQuery JSON 解析

php - DataTables 警告 : JSON data from server could not be parsed. 这是由 JSON 格式错误引起的

node.js - node js中的多对多关联

javascript - 如何只显示许多列表中的一些复选框项目?

javascript - 在 React 组件的文本框中复制文本的粘贴