如何在react render函数中创建动态href?

JavaScript

AEva伽罗

2020-03-12

我正在渲染帖子列表。对于每个帖子,我想呈现一个带有帖子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等等?

第1125篇《如何在react render函数中创建动态href?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
Sam梅小胖 2020.03.12

除了Felix的答案,

href={`/posts/${posts.id}`}

也会很好。很好,因为它全都在一个字符串中。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android