React Native JSON解析和辅助函数(2)

发布时间:2025-08-31 20:08:35 作者:益华网络 来源:undefined 浏览量(0) 点赞(0)
摘要:appBody.js

appBody.js 这里 super componentDidMount 是React内置函数。 使用 super, 您可以初始化 值,并且 componentDidMount 首先是调用。

react 导入   React,{Component}   react-native 导入   {  Text  }   native-base 导入   {Content,Card,CardItem,Body}   ./appBodyData 导入   AppBodyData   export   默认   类   AppBody  扩展了   Component {  constructor (){  super ()          this。 state  = {              data:[]          }      }      getData (){          return  fetch (  https://www.thewallscript.com/blogfeed/javascript/10  )       。 然后 ((response)=> response.json())       。 然后 ((responseJson)=> {         此。 的setState ({数据:responseJson.feed.entry});        })        。 捕捉 ((误差)=> {          控制台。 错误 (误差);        });      }       componentDidMount (){          this。 getData ();      }      render(){           return          <  AppBodyData data = {this。 state  .data}  />         );      }  模块  .EXPORT =  AppBody  ;

appBodyData.js 此组件呈现所有JSON数据标题。

react 导入   React,{Component}   react-native 导入   {  Text  }    从  native-base 导入  {Content,Card,CardItem,Body,Left}  export   default   class   AppBodyData  extends   Component {      render(){  let  articles = this。 道具  .data。 map function articleData ,index){                  return(                    <Card>                      <CardItem>                         <Body>                              <  Text  >                                   articleData  .title。$ t  }                             </  Text  >                          </ Body>                      </ CardItem>                  </ Card>                  )             });          return(             <Content>                   articles  }             </ Content>          );      }  模块  .EXPORT =   AppBodyData  ;

安装React包 这个JSON提要包含HTML和不正确的数据,我们需要安装包/插件来改进它。 Time Ago 此包有助于将时间戳转换为时间前的文本。

npm  install  react-native-timeago   --save

HTMLView将 HTML标记绑定到组件。

npm  install  react-native-htmlview  --save

适合图像 对于不同的移动尺寸,您需要以响应式风格制作图像。

npm  install  react-native-fit-image   --save

帮助程序功能 src 文件夹 下创建文件目录

helpers.js 这个文件包含两个函数, GetImage 是从HTML内容中过滤第一个图像源。 ContentSnippet 显示前50个单词。

导出   函数   GetImage (content){      var myRegexp = new RegExp(/<  img  .*?  src="(.  *?  )"/);      var match = myRegexp。 exec (内容);          if(match){               return  match [1];           }  导出   功能   ContentSnippet (内容){        回报 的内容。 拆分 (/ \ s + /)。 切片 (0,30)。 加入 (“”)+“......”;  }

视频教程 - React Native Helper / Injectable Functions appBodyData.js 包含所有实现的最终代码。

react 导入   React,{Component}   react-native 导入   {  Text  }     react-native-htmlview  导入 HTMLView  ;    react-native-timeago  导入 TimeAgo  ;    react-native-fit-image  导入 FitImage  ;  ../helpers/helpers 导入   {GetImage,ContentSnippet}   native-base 导入  {Content,Card,CardItem,Body,Left,Thumbnail,Button,Icon}   ; export   default   class   AppBodyData    extends   Component {      render(){           let    articles   = this。 道具  .data。 map function   ( articleData ,index){                  return(                     <Card>                          <CardItem>                              <Left>   <Thumbnail source = {require(../ img / SrinivasTamada.png)} />                                  <Body>                                       <  Text  >  articleData 。标题。$ t  }  </  文字                                 </ Body>                              </ Left>                          </ CardItem>                          <CardItem>  FitImage  source = {{uri: GetImage articleData  .content。$ t)}} />                         </ CardItem>                          <CardItem content>  HTMLView  value = { ContentSnippet( articleData  .content。$ t)} />                         </ CardItem>                          <CardItem>                              <Button transparent>                                  <Icon active name =“time”/>                                 <文本> TimeAgo  time = {  articleData  .published。$ t} />                                </ Text>                              </ Button>                              <Button transparent>                                  <Icon active name =“chatbubbles”/>                                  <Text>                                 {  articleData  .thr $ total。$ t}评论                                 </ Text>                              </ Button>                          </ CardItem>                      </ Card>                  )             });          返回(             <                  文章 }             </ Content>          );      }  模块  .EXPORT =   AppBodyData  ;

二维码

扫一扫,关注我们

声明:本文由【益华网络】编辑上传发布,转载此文章须经作者同意,并请附上出处【益华网络】及本页链接。如内容、图片有任何版权问题,请联系我们进行处理。

感兴趣吗?

欢迎联系我们,我们愿意为您解答任何有关网站疑难问题!

您身边的【网站建设专家】

搜索千万次不如咨询1次

主营项目:网站建设,手机网站,响应式网站,SEO优化,小程序开发,公众号系统,软件开发等

立即咨询 15368564009
在线客服
嘿,我来帮您!