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

javascript - React Native limit characters - Stack Overflow

programmeradmin7浏览0评论

I am working with the API.

renderGridItem = (itemData) => {
 return(
  <Text>
   {itemData.item.description}
  </Text>
 );
};

I want to show only the first 50 characters of the description. How can I do that?

I am working with the API.

renderGridItem = (itemData) => {
 return(
  <Text>
   {itemData.item.description}
  </Text>
 );
};

I want to show only the first 50 characters of the description. How can I do that?

Share Improve this question edited Jul 13, 2020 at 17:36 monamona 1,2462 gold badges21 silver badges46 bronze badges asked Jul 13, 2020 at 15:44 NaiNai 551 silver badge9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Wele @Nai to StackOverflow,

You may use the pure JavaScript functions. slice or maybe substring.

Use of Slice:-

renderGridItem = (itemData) => {
 return(
  <Text>
   {itemData.item.description.slice(0, 50)}
  </Text>
 );
};

You can just use regular javascript functions to do that. In this case substring

renderGridItem = (itemData) => {
 return(
  <Text>
   {itemData.item.description.substring(0, 50)}
  </Text>
 );
};

Not exactly 50 characters, but you could achieve this with a bination of:

  • Declare numberOfLines prop for text and set it to 1 / 2 (depending on your needs).
  • Declare ellipsizeMode with whatever you want it to be.
  • Set width of the container / text itself to the width you need and want to display.

Link to docs on Text in React Native.

This way you're making sure that your text is going to show correctly and no word is going to be cut in an ugly way.

发布评论

评论列表(0)

  1. 暂无评论