javascript - React Native limit characters - Stack Overflow

I am working with the API.renderGridItem = (itemData) => {return(<Text>{itemData.item.descrip

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745635093a4637342.html

相关推荐

  • javascript - React Native limit characters - Stack Overflow

    I am working with the API.renderGridItem = (itemData) => {return(<Text>{itemData.item.descrip

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信