最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to append a string and html tag in ternary operator condition? - Stack Overflow

programmeradmin3浏览0评论

I have a ternary condition in React

return <span>
    {
        data.length > 136
            ? this.trimStringLength(data, 136) + (<span>see more...</span>) 
            : data
    }
</span>;

Here, this.trimStringLength provides a trim string. The result should be "some data here see more..." but I am geeting "some data here[object Object]"

How can I concatenate to get the required result?

I have a ternary condition in React

return <span>
    {
        data.length > 136
            ? this.trimStringLength(data, 136) + (<span>see more...</span>) 
            : data
    }
</span>;

Here, this.trimStringLength provides a trim string. The result should be "some data here see more..." but I am geeting "some data here[object Object]"

How can I concatenate to get the required result?

Share Improve this question edited Sep 1, 2021 at 10:26 dumbass 27.3k4 gold badges38 silver badges74 bronze badges asked Sep 1, 2021 at 10:15 TalesTales 3031 gold badge5 silver badges16 bronze badges 4
  • What do you have in your data? And what do you want to print? – Ryan Le Commented Sep 1, 2021 at 10:16
  • @RyanLe data is string of characters like "some data here". I already mentioned the required result. – Tales Commented Sep 1, 2021 at 10:17
  • Just remove the inner span: this.trimStringLength(data, 136) + 'see more...' – jabaa Commented Sep 1, 2021 at 10:18
  • No I want to provide some css to see more text. – Tales Commented Sep 1, 2021 at 10:19
Add a ment  | 

3 Answers 3

Reset to default 8

Use a Fragment:

E.g.:

<span>
  {data.length > 136
    ? <>{this.trimStringLength(data, 136)} <span>see more...</span></>
    : data}
</span>

You can use it like this, no need to use the + sign

<span>
{
 data.length > 136
  ? 
   (<>{this.trimStringLength(data, 136)} <span> see more...</span></>)
  : 
   data
}
</span>

Seems like you are concatenating a string and an object instead of two strings.

Have you tried replacing the span element with a string, like so ?

<span>
{
 data.length > 136
  ? 
   this.trimStringLength(data, 136) + "see more..." 
  : 
   data
}
</span>
发布评论

评论列表(0)

  1. 暂无评论