简体   繁体   English

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

[英]React.js pass multiple json values to one child parameter

I have a json object: 我有一个JSON对象:

"tags": ["salmons","kitchenaccident"] “标签”:[“鲑鱼”,“厨房事故”]

And I have a Component that parses through the JSON.. 而且我有一个通过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}/>
       );

However, it passes it through as one concatendated string: salmonskitchenaccident. 但是,它作为一个相互联系的字符串通过它:salmonskitchenaccident。

If I wanted it to render as '#salmons #kitchenaccident'.. what would be the best solution. 如果我希望它呈现为“ #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>
        );
    }
});

In this line: 在这一行:

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

you try to display an array straight away, which implicitly calls the toString() method of the tags array. 您尝试立即显示一个数组,该数组隐式调用了tags数组的toString()方法。 You need to map the items in your array to HTML nodes, like this: 您需要将数组中的项目map到HTML节点,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM