ReactNative-綜合案例(03)

本文出自:我的個人博客:http://www.cenzhijun.top/
最近幾天學(xué)了幾個ReactNative組件,總覺得單純的學(xué)幾個組件進步慢,所以我打算做一些綜合性的小案例,練習(xí)下實戰(zhàn),我從網(wǎng)上找到一個小案例
,感覺挺好,也學(xué)習(xí)了很多,代碼內(nèi)容可能不太一樣,主要區(qū)別是:我把RN官方不推薦或者已經(jīng)放棄了的組件進行了替換,如果有需要的可以互相參考下

接著上篇案例開始寫,這篇文章將會講解如何怎樣利用WebView加載HTML文件。

WYNewsDetail.js文件寫如下代碼:

import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View,
    WebView
} from 'react-native';

import Request from '../Utils/WYRequest';

export default class WYNewsDetail extends Component {
  static navigationOptions = ({ navigation }) => ({
    title: navigation.state.params.title,
  });

  constructor(props){
    super(props);

    this.state = {
      docid:'',
    };
  }

  componentDidMount() {
    const {params} = this.props.navigation.state;

    this.setState({
      docid: params.docid,
      html: '',
    });

    var url_api = 'http://c.m.163.com/nc/article/' + params.docid + '/full.html';
    Request.get(url_api, (responseData) => {

      // 取出數(shù)據(jù)
      var allData = responseData[this.state.docid];

      var body = allData['body'];
      // 取出圖片
      var img = allData['img'];
      for (var i = 0; i < img.length; i++) {
        var item = img[i];
        var ref = item.ref;
        var src = item.src;
        var newImg = '![]('+ src +')';
        body = body.replace(ref, newImg);

        console.log('====================================');
        console.log(ref, src);
        console.log('====================================');
      }

      // 更新UI
      this.setState({
        html: body,
      });
    }, (error) => {
      alert(error);
    });
  }

  render() {
    return (
      <WebView
        automaticallyAdjustContentInsets={true}
        // source={{uri:'http://www.baidu.com'}}  加載網(wǎng)頁
        source={{html: this.state.html, baseUrl: ''}}
        javaScriptEnabled={true}
        domStorageEnabled={true}
        startInLoadingState={true}
      />
    );
  }
}

const styles = StyleSheet.create({
  background: {
    backgroundColor: 'red',
    flex: 1,
    justifyContent: 'center',
    alignItems:'center'
  },

  textStyle: {
    fontSize:30,
  }
});

主要知識點:

  • 界面?zhèn)髦?/li>
static navigationOptions = ({ navigation }) => ({
    title: navigation.state.params.title,
  });
  
componentDidMount() {
    const {params} = this.props.navigation.state;

    this.setState({
      docid: params.docid,
      html: '',
    });

還有一個bug,就是當(dāng)界面跳轉(zhuǎn)的時候,左上角返回按鈕和文字依舊是系統(tǒng)的藍(lán)色,如何修改顏色,就用到了自定義功能了

const StackOptions = ({navigation}) => {
    console.log(navigation);
    let {state,goBack} = navigation;

    // 用來判斷是否隱藏或顯示header
    const visible= state.params.isVisible;
    let header;
    if (visible === true){
        header = null;
    }
    const headerStyle = {backgroundColor:'#4ECBFC'};
    const headerTitle = state.params.title;
    const headerTitleStyle = {fontSize:FONT_SIZE(20),color:'white',fontWeight:'500'}
    const headerBackTitle = false;
    const headerLeft = (
        <Button
            isCustom={true}
            customView={
                            <Icon
                                name='ios-arrow-back'
                                size={30}
                                color='white'
                                style={{marginLeft:13}}
                            />
                        }
            onPress={()=>{goBack()}}
        />
    );
    return {headerStyle,headerTitle,headerTitleStyle,headerBackTitle,headerLeft,header}
};

通過下面的方式調(diào)用:

const MyApp = StackNavigator({
    MyTab: {
        screen: MyTab,
    },
    Detail: {
        screen: Detail,
        navigationOptions: ({navigation}) => StackOptions({navigation})
    },
)};

至此第一個小案例就做完了

效果圖

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,228評論 25 708
  • 這篇文章轉(zhuǎn)載自“折騰范兒の味精”,看完覺得寫得確實很精彩,值得準(zhǔn)備在項目中引入動態(tài)更新框架的同學(xué)參詳。我看完之后,...
    mercurygear閱讀 2,630評論 0 43
  • 文/安小左 手握繁華一季絕世獨立于你深情的眼 嫣紅的臉頰賦予這六月煙雨寂寞滂沱 我知你泣無聲交匯在我心口之上成潭 ...
    安小左閱讀 355評論 0 3
  • ——那些文章后面你不知道的事: 曾經(jīng)寫的故事,已經(jīng)不知道如何再下筆,曾經(jīng)結(jié)束了的東西,不知道如何能重新開始。 離開...
    南山1閱讀 518評論 0 0
  • 2011年,春節(jié)。 她說想我了,我何嘗不想她?于是,我叫她去我們家過春節(jié),她說去你家過年肯定不行,要去的話也得是年...
    sYnecs閱讀 208評論 0 2

友情鏈接更多精彩內(nèi)容