版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、React Native Day5React Native Day5课前回顾课堂目标App导航框架设计仿主流APP设计一个导航框架欢迎页面设计App主页设计详情页设计安装 react navigation 与第三方图标库 react-native-vector-icons设计欢迎页进入主页导航App入口引用导航欢迎页面5秒后进入主页设计一个转场工具类 NavigationUtil.js欢迎页改造主页设计底部导航Index页面顶部导航设计Redux 与 React Navigation结合集成Redux流程 ,概念梳 概念第一步:安装redux,react-redux,react-navigat
2、ion-redux-helpers 第二步:配置Navigation第二步:配置Reducer 第三步:配置store第四步:在组件中应用案:使用react-navigaton+redux 修改状态栏颜色创建Actions创建 Actions/theme 创建Reducer/theme 在Reducer中聚合APIcombineReducers(reducers) createStore applyMiddleware如何做到从直接修改 state ?1.通过Objesign()创建对象拷贝, 而拷贝中会包含新创建或新过的属性值2. 通过通过ES7的新特性对象展开运算符(Object Spre
3、ad Operator)总结RN网络编程发起请求关于RN中Http请求问题版本项目中没有默认配置怎么办?以0.43版本为开课吧web全栈架构师课前回顾React NavigationReact Navigation概念与属性核心导航的学习与使用课堂目标掌握react navigation 导航框架设计解redux在RN项目(使用react navigation)中的集成方式掌握Fetch网络编程App导航框架设计仿主流APP设计一个导航框架欢迎页面设计开课吧web全栈架构师import React, Component from "react"import Platform
4、, StyleSheet, Text, View from "react-native" export default class WelcomePage extends Component render() return (<View style=styles.container><Text style=styles.welcome>Welcome to WelcomePage!</Text></View>);const styles = StyleSheet.create( container: flex: 1,justi
5、fyContent: "center", alignItems: "center", backgroundColor: "#F5FCFF",welcome: fontSize: 20,textAlign: "center", margin: 10);App主页设计importimportReact, Component from "react" Platform, StyleSheet, Text, View from "react-native"exportdefault
6、class HomePage extends Component constructor(props) super(props);console.disableYellowBox = true;render() return (<View style=styles.container><Text style=styles.welcome>Welcome to HomePage!</Text></View>);const styles = StyleSheet.create( container: flex: 1,justifyContent: &
7、quot;center", alignItems: "center", backgroundColor: "#F5FCFF",welcome: fontSize: 20,textAlign: "center", margin: 10);详情页设计开课吧web全栈架构师import React, Component from "react"import Platform, StyleSheet, Text, View from "react-native"export default c
8、lass DetailPage extends Component render() return (<View style=styles.container><Text style=styles.welcome>Welcome</View>);toDetailPage!</Text>const styles = StyleSheet.create( container: flex: 1,justifyContent: "center", alignItems: "center", backgroundCo
9、lor: "#F5FCFF",welcome: fontSize: 20,textAlign: "center", margin: 10);安装 react navigation 与第三方图标库 react-native-vector-iconsyarn add react-navigation # or with npm# npm install -save react-navigationyarn add react-native-gesture-handler # or with npm# npm install -save react-nativ
10、e-gesture-handlerreact-native link react-native-gesture-handleryarn add react-native-vector-iconsreact-native link react-native-vector-icons#记得关闭模拟,服务,重新启动项目开课吧web全栈架构师设计欢迎页进入主页导航# AppNavigator.jsimport createStackNavigator, createAppContainer,createSwitchNavigator from"react-navigation"im
11、portimport importHomePage from "./Pages/HomePage"WelcomePage from "./Pages/WelcomePage" DetailPage from "./Pages/DetailPage"/定义欢迎导航const AppInitNavigator = WelcomePage: screen: WelcomePage,createStackNavigator(navigationOptions: header: null);/定义主页导航const AppMainNavigat
12、or HomePage: screen: HomePage, navigationOptions:header: null,DetailPage: screen: DetailPage);=createStackNavigator(export default createAppContainer( createSwitchNavigator(Init: AppInitNavigator, Main: AppMainNavigator);开课吧web全栈架构师App入口引用导航import App from "./js/Navigator/AppNavigator"欢迎页面
13、5秒后进入主页componentDidMount() this.timer = setTimeout() => const navigation = ps; navigation.navigate("Main");, 1000);componentWillUnmount() this.timer && clearTimeout(this.timer);设计一个转场工具类 NavigationUtil.jsexport default class NavigationUtil /跳转到指定页面static goPage(props, pa
14、ge) const navigation = NavigationUtil.navigation; navigation.navigate(page, .props);/go Backstatic resetGoBack(props) const navigation = props; navigation.goBack();/回到主页static resetToHomePage(params) const navigation = params; navigation.navigate("Main");欢迎页改造开课吧web全栈架构师主页设计底部导航import Reac
15、t, Component from "react"import Platform, StyleSheet, Text, View from "react-native" import createAppContainer, createBottomTabNavigator, from "react-navigation"import IndexPage from "./IndexPage"import MyPage from "./MyPage"importPage from "./P
16、age"import FontAwesome from "react-native-vector-icons/FontAwesome"const TABS = IndexPage: screen: IndexPage, navigationOptions: tabBarLabel: "首页",tabBarIcon: ( tintColor, focused ) => (<FontAwesome name="home" size=26 style= color: tintColor />),Page: scr
17、een:Page,navigationOptions: tabBarLabel: "",tabBarIcon: ( tintColor, focused ) => (<FontAwesomename="-camera"开课吧web全栈架构师size=26style= color: tintColor />)import NavigationUtil from "./Navigator/navigationUtil"componentDidMount() this.timer = setTimeout() =>
18、navigationUtil.resetToHomePage(navigation: ps.navigation);, 1000);Index页面顶部导航设计开课吧web全栈架构师import React, Component from "react"import Button, Platform, StyleSheet, Text, View from "react-native" import createAppContainer, createMaterialTopTabNavigator,MyPage: screen: MyPag
19、e, navigationOptions: tabBarLabel: " ",tabBarIcon: ( tintColor, focused ) => (<FontAwesome name="user" size=26 style= color: tintColor />);export default class HomePage extends Component constructor(props) super(props); console.disableYellowBox = true;_TabNavigator() ret
20、urn createAppContainer(createBottomTabNavigator(TABS);render() const Tabs = this._TabNavigator(); return <Tabs />const styles = StyleSheet.create( container: flex: 1,justifyContent: "center", alignItems: "center", backgroundColor: "#F5FCFF",welcome: fontSize: 20,t
21、extAlign: "center", margin: 10);开课吧web全栈架构师 from "react-navigation"import navigationUtil from "./Navigator/navigationUtil" export default class IndexPage extends Component constructor(props) super(props); this.tabNames = "ios", "android", "nodeJ
22、s","Vue","React", "React Native"_genTabs() const tabs = ; this.tabNames.forEach(item, index) => tabstab$index = screen: props => <IndexTab .props tabName=item />, navigationOptions: title: item;);return tabs;render() const TabNavigator = createAppContai
23、ner( createMaterialTopTabNavigator(this._genTabs(), tabBarOptions: tabStyle: , upperCaseLabel: false, scrollEnabled: true, style: /选项卡背景色backgroundColor: "red",indicatorStyle: /指示的样式height: 2,backgroundColor: "#fff",labelStyle: /文字的样式fontSize: 16,marginTop: 6,marginBottom: 6开课吧we
24、b全栈架构师)/createMaterialTopTabNavigator(/IndexTab1: /screen: IndexTab,/navigationOptions: /title: "Tab1"/,/IndexTab2: /screen: IndexTab,/navigationOptions: /title: "Tab2"/);return (<View style= flex: 1, marginTop: 30 ><TabNavigator /></View>);class IndexTab extend
25、s Component render() const tabName = ps; return (<View style=styles.container><Text style=styles.welcome>Welcome to tabName</Text><Buttontitle="go to DetailPage" onPress=() => navigationUtil.goPage(ps, "DetailPage");/></View>);cons
26、t styles = StyleSheet.create( container: flex: 1,justifyContent: "center", alignItems: "center", backgroundColor: "#F5FCFF",welcome: Redux 与 React Navigation结合集成Redux + React Navigation有点复杂 因为Redux是自顶向下管一套状态,React Navigation也是自顶向下管一套状态甚至页面,这俩融合起来就有点Redux 是必须的要想撮合Redux和R
27、eact Navigation(为方描述,后面就直接称呼为导航),就得先各自解在使用 React Navigation 的项目中,想要集成 redux 就必须要引入 react-navigation-redux-helpers 这个库。ReduxReact或者说ReactNative原生使用state和props管UI状态,但是俩属性非常琐碎,稍微复杂一点的UI就没法应付,所以Redux应运而生,但是Redux又很乱,我觉得乱主要是以下:需要需要的东西多的地方多API方法名很莫名其妙流程 ,概念开课吧web全栈架构师fontSize: 20, textAlign: "center&q
28、uot;, margin: 10);用户(操作View)发出Action,发出方式就用到dispatch方法;然后,Store自动调用Reducer,并且传入两个参数(当前State和收到的Action),Reducer会返回新的State,如果有Middleware,Store会将当前State和收到的Action传递给Middleware, Middleware会调用Reducer 然后返回新的State;State一旦有变化,Store就会调用函数,来新View;梳redux是一个状态,响应动作(action)的地方,所以定义redux实现的叫storestore有一个初始状态(defa
29、ult state),还有响应某个动作(action)的处(reducer)然后UI视图将这个store及其状态(state)和方法(action) 件中取到这些状态和方法。到视图组件的props,这样就可以在组当用户点击某个操作等,会从props中拿到action并调用它,他会向store action的内容,如果store中有中间件,会先逐个调用中间件来完成预处然后再调用各个reducer,来完成状态的改变。状态改变以后,因为状态绑定UI组件的props,所以react会自动刷新UI。(dispatch)这个概念reducer:名字于Array的reduce方法,作者估计向表达的是遍历的意
30、义,但是这个名字实在是诡异,所以我给他起名叫做处,或者叫触发,作用就是UI发来action以后,它根据action的类型,对状态进修改。他是action的消费者,他是个函数(或者专业点叫纯函数),但是他有个缺点,就是需要立即返回,如果是网络请求等异步操作,他就没法胜任。中间件:中间件的作用就是完成异步请求,或者完成其他一些需要封装起来的预处,比如redux-logger,就是把action前后的状态打印出来的中间件,本质也是个函数,但是结构很诡异,诡异程度类似于C语言中的3级指针,这个指针还是函数指针。过这种诡异我们需要操心,只需要填写内容开课吧web全栈架构师第一步:安装redux,reac
31、t-redux,react-navigation-redux-helpersyarn add reduxyarn add react-redux /因为redux其实是可以独立运的js项目,所以把他使用在react项目中, 还需要使用react-reduxyarn add react-navigation-redux-helpers/在使用React Navigation 的项目中,想要集成 redux 就必须要引入 react-navigation-redux-helpers 这个库第二步:配置Navigation引入redux和react-navigation-redux-helpersi
32、mport connect from "react-redux"import createReactNavigationReduxMiddleware, createReduxContainer from "react-navigation-redux-helpers"使用createReduxContainer方法,将RootNavigator封装成高阶组件AppWithNavigationState,这个高阶组件完成navigation prop的替换,改成使用redux 的navigation/ 修改AppNavitor 为 RootNavigat
33、or 并再默认导出export const RootNavigator = createAppContainer( createSwitchNavigator(Init: AppInitNavigator, Main: AppMainNavigator);const AppWithNavigationState = createReduxContainer(RootNavigator, "root");开课吧web全栈架构师创建导航中间件:createReduxContainer把导航状态放到props只是能被各个组件到,但是React Navigation还能识别,所以还
34、需要最后一步创建一个中间件,把需要导航的组件与导航reducer连接起来然后使用Redux的connect函数再封装一个高阶组件,默认导出/State到Props的关系const mapStateToProps = state => return state: state.nav;/使用Redux的connect函数再封装一个高阶组件,连接 React 组件与 Redux store export default connect(mapStateToProps)(AppWithNavigationState);完整代码开课吧web全栈架构师import createStackNaviga
35、tor, createAppContainer, createSwitchNavigator from "react-navigation"import HomePage from "./Pages/HomePage" import WelcomePage from "./Pages/WelcomePage" import DetailPage from "./Pages/DetailPage"/引入reduximport connect from "react-redux"import cre
36、ateReactNavigationReduxMiddleware,/ reduxifyNavigator,react-navigation-redux-helpers3.0变,reduxifyNavigator被改名为createReduxContainer createReduxContainer from "react-navigation-redux-helpers" export const rootCom = "Init" /设置根由export const middleware = createReactNavigationReduxMid
37、dleware( state => state.nav,"root");开课吧web全栈架构师const AppInitNavigator = createStackNavigator( WelcomePage: screen: WelcomePage, navigationOptions: header: null);const AppMainNavigator = createStackNavigator( HomePage: screen: HomePage, navigationOptions: header: null,DetailPage: screen:
38、 DetailPage);export const RootNavigator = createAppContainer( createSwitchNavigator(Init: AppInitNavigator, Main: AppMainNavigator);/* 1.初始化react-navigation与redux的中间件,* 该方法的一个很大的作用就是为reduxifyNavigator的key设置actionSubscribers(为订阅者)*/react-navigation-redux-helpers3.0变,createReactNavigationReduxMiddlewa
39、re的参数顺序发生变化export const middleware = createReactNavigationReduxMiddleware( state => state.nav,"root");/* 2.将根导航组件传递给 reduxifyNavigator 函数,* 并返回一个将navigation state 和 dispatch 函数作为 props的新组件;* 使用createReduxContainer方法,将RootNavigator封装成高阶组件AppWithNavigationState* 这个高阶组件完成navigation prop的替换
40、,改成使用redux的navigation* */const AppWithNavigationState = createReduxContainer(RootNavigator, "root");第二步:配置Reducerimport combineReducers from "redux" import theme from "./theme"import rootCom, RootNavigator from "./Navigator/AppNavigator"/1.指定默认stateconst navSt
41、ate = RootNavigator.router.getStateForAction( RootNavigator.router.getActionForPathAndParams(rootCom);/*上面的代码创建一个导航action(表示打开rootCom),那么我们就可以通过action创建导航state,通过方*法getStateForAction(action, oldNavigationState)*俩参数,一个是新的action,一个是当前的导航state,返回新的状态,当没有办法执这个action的时候,就返回*null。*/* 2.创建自己的 navigation re
42、ducer,*/const navReducer = (state = navState, action) => const nextState = RootNavigator.router.getStateForAction(action, state);/ 如果nextState为null或未定义,只需返回原始state return nextState | state;/* 3.合并reducer* type Reducer<any> | Reducer<any, AnyAction>*/const index = combineReducers( nav:
43、 navReducer,开课吧web全栈架构师/State到Props的关系const mapStateToProps = state => return state: state.nav;/使用Redux的connect函数再封装一个高阶组件,连接 React 组件与 Redux store export default connect(mapStateToProps)(AppWithNavigationState);第三步:配置storeimportimport import applyMiddleware, createStore from "redux" re
44、ducers from "./Reducer" middleware from "./Navigator/AppNavigator"const middlewares = middleware;/* 创建store*/export default createStore(reducers, applyMiddleware(.middlewares);第四步:在组件中应用import import importimportReact, Component from 'react'Provider from 'react-redux&
45、#39;AppNavigator from './Navigator/AppNavigator' store from './Store'type Props = ;export default class App extends Component<Props> render() /* 将store传递给App框架*/return <Provider store=store><AppNavigator/></Provider>经过上述4步呢,我们已经完成react-navigaton+redux的集成,那么如何使
46、用它呢?开课吧web全栈架构师theme: theme);export default index;案:使用react-navigaton+redux 修改状态栏颜色创建Actions# Types.js export default THEM_CHANGE: "THEM_CHANGE", THEM_INIT: "THEM_INIT"创建 Actions/themeimport Types from "./Types"export function onThemeChange(theme) return type: Types.THE
47、M_CHANGE, theme: theme;创建Reducer/themeimport Types from "././Actions/Types"const defaultState = theme: "blue"export default function onAction(state = switch (action.type) case Types.THEM_CHANGE: return .state,theme: action.theme;default:return state;defaultState, action) 开课吧web全栈
48、架构师在Reducer中聚合1.订阅state开课吧web全栈架构师import React, Component from "react"import Button, Platform, StyleSheet, Text, View from "react-native" import createAppContainer, createMaterialTopTabNavigator from "react-navigation"import IndexTab from "./Pages/IndexTab" im
49、port connect from "react-redux"import onThemeChange from "./Actions/theme"import navigationUtil from "./Navigator/navigationUtil" class IndexPage extends Component constructor(props) super(props); this.tabNames = "ios", "android", "nodeJs",
50、"Vue","React", "React Native"_genTabs() const tabs = ; this.tabNames.forEach(item, index) => tabstab$index = screen: props => <IndexTab .props tabName=item />, navigationOptions: title: itemconst index = combineReducers( nav: navReducer,theme: theme);开课吧web全
51、栈架构师;);return tabs;render() const TabBackground = ps.theme; console.log(ps);const TabNavigator = createAppContainer( createMaterialTopTabNavigator(this._genTabs(), tabBarOptions: tabStyle: , upperCaseLabel: false, scrollEnabled: true, style: /选项卡背景色backgroundColor: TabBackground,indi
52、catorStyle: /指示的样式height: 2,backgroundColor: "#fff",labelStyle: /文字的样式fontSize: 16,marginTop: 6,marginBottom: 6)/createMaterialTopTabNavigator(/IndexTab1: /screen: IndexTab,/navigationOptions: /title: "Tab1"/,/IndexTab2: /screen: IndexTab,/navigationOptions: /title: "Tab2&qu
53、ot;/);return (<View style= flex: 1, marginTop: 30 ><TabNavigator />在上述代码中我们订阅store中的theme state,然后该组件就可以通过 ps.theme 获取到所订阅的theme state。2.触发action改变state开课吧web全栈架构师import React, Component from "react"import Button, Platform, StyleSheet, Text, View from "react-native
54、"import connect from "react-redux"import onThemeChange from "./Actions/theme"class IndexTab extends Component render() const tabName = ps; return (<View style=styles.container><Text style=styles.welcome>Welcome to tabName</Text><Buttontitle="
55、;go to DetailPage" onPress=() => navigationUtil.goPage(ps, "DetailPage");</View>);const styles = StyleSheet.create( container: flex: 1,justifyContent: "center", alignItems: "center", backgroundColor: "#F5FCFF",welcome: fontSize: 20,textAl
56、ign: "center", margin: 10);const mapStateToProps = state => ( theme: state.theme.theme);export default connect(mapStateToProps)(IndexPage);/><Buttontitle="改变tab背景色" onPress=() => ps.onThemeChange("#000");/ navigationUtil.goPage(ps,/></V
57、iew>);"DetailPage");const styles = StyleSheet.create( container: flex: 1,justifyContent: "center", alignItems: "center", backgroundColor: "#F5FCFF",welcome: fontSize: 20,textAlign: "center", margin: 10);const mapStateToProps = state => ();const mapDispatchToProps = dispatch => (onThemeChange: theme => dispatch
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年甘肃省嘉峪关市人民社区卫生服务中心招聘备考考试题库及答案解析
- 五四主题活动-方案策划(3篇)
- 2026天津市和平区教育系统事业单位面向社会招聘101人考试备考试题及答案解析
- 2026云南罗平锌电股份有限公司管理人员社会化招聘2人参考考试题库及答案解析
- 货币的英文介绍
- 2026广西壮族自治区考试录用人民法院法官助理工作360人考试参考题库及答案解析
- 奉化春天活动方案策划(3篇)
- 室内娱乐治安管理制度(3篇)
- 山西美容店管理制度(3篇)
- 2026中交天津航道局有限公司疏浚技术与装备研发中心招聘1人备考考试试题及答案解析
- 国家自然基金形式审查培训
- 2026马年卡通特色期末评语(45条)
- NCCN临床实践指南:肝细胞癌(2025.v1)
- 免租使用协议书
- 2025 AHA心肺复苏与心血管急救指南
- 2026年九江职业大学单招职业适应性测试题库带答案详解
- 危化品库区风险动态评估-洞察与解读
- 激光焊接技术规范
- 消防联动排烟天窗施工方案
- 2025年高考物理 微专题十 微元法(讲义)(解析版)
- 2025年国家能源投资集团有限责任公司校园招聘笔试备考题库含答案详解(新)
评论
0/150
提交评论