我正在渲染帖子列表。对于每个帖子,我想呈现一个带有帖子ID的锚标记,作为href字符串的一部分。
render: function(){ return ( <ul> { this.props.posts.map(function(post){ return <li key={post.id}><a href='/posts/'{post.id}>{post.title}</a></li> }) } </ul> );
我该如何做,以便每个帖子都具有href的/posts/1,/posts/2等等?
/posts/1
/posts/2
使用字符串串联:
href={'/posts/' + post.id}
JSX语法允许使用字符串或表达式({...})作为值。您不能将两者混用。顾名思义,您可以在表达式内部使用任何JavaScript表达式来计算值。
({...})