本文出自:我的個(gè)人博客:http://www.cenzhijun.top/
最近幾天學(xué)了幾個(gè)ReactNative組件,總覺(jué)得單純的學(xué)幾個(gè)組件進(jìn)步慢,所以我打算做一些綜合性的小案例备徐,練習(xí)下實(shí)戰(zhàn)蔓腐,我從網(wǎng)上找到一個(gè)小案例
,感覺(jué)挺好璧函,也學(xué)習(xí)了很多,代碼內(nèi)容可能不太一樣周崭,主要區(qū)別是:我把RN官方不推薦或者已經(jīng)放棄了的組件進(jìn)行了替換柳譬,如果有需要的可以互相參考下
接著上篇案例開始寫,這篇文章將會(huì)講解如何怎樣利用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)頁(yè)
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,
}
});
主要知識(shí)點(diǎn):
- 界面?zhèn)髦?/li>
static navigationOptions = ({ navigation }) => ({
title: navigation.state.params.title,
});
componentDidMount() {
const {params} = this.props.navigation.state;
this.setState({
docid: params.docid,
html: '',
});
還有一個(gè)bug美澳,就是當(dāng)界面跳轉(zhuǎn)的時(shí)候,左上角返回按鈕和文字依舊是系統(tǒng)的藍(lán)色,如何修改顏色制跟,就用到了自定義功能了
const StackOptions = ({navigation}) => {
console.log(navigation);
let {state,goBack} = navigation;
// 用來(lái)判斷是否隱藏或顯示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}
};
通過(guò)下面的方式調(diào)用:
const MyApp = StackNavigator({
MyTab: {
screen: MyTab,
},
Detail: {
screen: Detail,
navigationOptions: ({navigation}) => StackOptions({navigation})
},
)};