这个项目好像就是记录了一个数据的流向,大体思想好像是这个

项目地址:https://github.com/Yangzhuren/rn_antd_dva_reactnavigation

先看效果

第一个页面会显示第二个页面点击的值



第二个页面



先来看代码

根index.js引用的不是app.js组件而是在自定义的组件里面

//index.js
/**
* @format
*/ import {AppRegistry} from 'react-native';
import App from './js';
import {name as appName} from './app.json'; AppRegistry.registerComponent(appName, () => App);

js中的项目

//js/index.js
import React, {Component} from 'react'
import {InputItem, List, Provider} from "@ant-design/react-native";
import Pages from './pages'
import dva from "dva";
import {registerModels} from "./models";
import createMemoryHistory from "history/createMemoryHistory"; class Root extends Component {
constructor(props) {
super(props);
} render() {
return (
<Provider>
<Pages/>
</Provider>
)
}
} const app = dva({
history:createMemoryHistory(),
initialState: {},
onError: function (e) {
console.log("dva onError", e)
}
}) registerModels(app) app.router(() => <Root/>) const App = app.start() export default App

上面的引用了pages组件

页面一进去,应该进入的是page1页面然后再进去page2页面

看代码

//js/pages/index.js
//根据代码执行顺序可知道先执行page1
import React, {Component} from 'react'
import {createStackNavigator, createAppContainer} from 'react-navigation'
import Page1 from "./page1";
import Page2 from "./page2"; const Pages = createStackNavigator({
page1: {
screen: Page1
},
page2: {
screen: Page2
}
}) export default createAppContainer(Pages)

page1页面组件



//js/pages/page1/index.js
import React, {Component} from 'react'
import {Button, Flex} from "@ant-design/react-native"
import Actions from './actions'
import {connect} from 'dva' class Page1 extends Component { constructor(props) {
super(props)
new Actions(this)
} render() {
const {clickCount} = this.props.userInfo
return (
<Flex align={"center"} justify={"center"} style={{flex: 1}}>
<Button onPress={this.clicked}>go page2 and click count:{clickCount}</Button>
</Flex>
)
}
} function mapStateToProps(state) {
return {userInfo: state.user}
} export default connect(mapStateToProps)(Page1)

这个有意思,定一个action组件,然后点击可以跳转第二个页面

//js/pages/page1/actions.js
import {BaseAction} from '../../common' export default class Actions extends BaseAction {
clicked() {
this.props.navigation.navigate('page2')
}
}
//js/pages/page2/index.js
import React, {Component} from 'react'
import {Button} from "@ant-design/react-native"
import Actions from './actions'
import {createAction} from "../../actions";
import {connect} from 'dva'; class Page2 extends Component {
constructor(props) {
super(props)
new Actions(this)
this.state = {
clickCount: 0
}
} render() {
const {clickCount} = this.state
return (
<Button onPress={this.clicked}>page2 click counts:{clickCount}</Button>
)
}
} function mapStateToProps(state) {
return {userInfo: state.user}
} export default connect(mapStateToProps)(Page2)
//js/pages/page2/actions.js
import {BaseAction} from '../../common'
import {createAction} from "../../actions"; export default class Actions extends BaseAction {
clicked() {
const {clickCount} = this.state
this.setState({
clickCount: clickCount + 1
},()=>{
const userAction = createAction('user/checkUser')({clickCount: this.state.clickCount})
this.props.dispatch(userAction)
})
}
}

关于models里面有一点不理解

//js/models/index.js
import User from './User'
import {DvaInstance} from "dva"; export function registerModels(app: DvaInstance) {
app.model(User)
}
//js/models/User.js
// 里面就是dva的一些基本方法吗
import {createAction} from '../actions' export default {
namespace: 'user',
state: {
name: '',
mobile: '',
clickCount: 0
},
reducers: {
getUserInfo(state, {payload}) {
return {...state, ...payload}
}
},
effects: {
* checkUser({payload}, {call, put}) {
yield put(
createAction('getUserInfo')({
...payload
})
)
}
}
}

【水滴石穿】rn_antd_dva_reactnavigation的更多相关文章

  1. iOS 开发笔记 -- 各种细枝末节的知识(水滴石穿)

    在此总结整理,遇到的各种的小问题: 1.通过从字典(数组)中取出的NSString的length==0 作为if的判断条件导致的carsh: 由于在字典中通过Key取出值之后直接做了length相关操 ...

  2. 【水滴石穿】react-native-book

    先推荐一个学习的地址:https://ke.qq.com/webcourse/index.html#cid=203313&term_id=100240778&taid=12778558 ...

  3. 【水滴石穿】rnTest

    其实就是一个小的demo,不过代码分的挺精巧的 先放地址:https://github.com/linchengzzz/rnTest 来看看效果 确实没有什么可以说的,不过代码部分还行 先入口文件 / ...

  4. 【水滴石穿】rn_statusbar

    先放项目地址https://github.com/hezhii/rn_statusbar 来看一下效果 咩有感觉很怎么样,看代码 根入口文件 //index.js //看代码我们知道入口是app.js ...

  5. 【水滴石穿】react-native-ble-demo

    项目的话,是想打开蓝牙,然后连接设备 点击已经连接的设备,我们会看到一些设备 不过我这边在开启蓝牙的时候报错了 先放作者的项目地址: https://github.com/hezhii/react-n ...

  6. 【水滴石穿】ReactNative-Redux-Thunk

    老实说,运行出来的项目让人失望,毕竟我想看各种有趣的demo啊- 先放上源码地址:https://github.com/ludejun/ReactNative-Redux-Thunk 我们来一起看看代 ...

  7. 【水滴石穿】mobx-todos

    我觉得代码在有些程序员手里,就好像是画笔,可以创造很多东西 不要觉得创意少就叫没有创意,每天进步一点点,世界更美好 首先源码地址为:https://github.com/byk04712/mobx-t ...

  8. 【水滴石穿】ReactNativeMobxFrame

    项目地址如下:https://github.com/FTD-ZF/ReactNativeMobxFrame 应该可以说的是,项目也只是一个花架子,不过底部的tab稍微改变了 我们一起来看代码 //in ...

  9. 【水滴石穿】react-native-aze

    说个题外话,早上打开电脑的时候,电脑变成彩色的了,锅是我曾经安装的一个chrome扩展,没有经过我的同意开启了 (也许是昨天迷迷糊糊开启了) 上午运行项目都不成功,还以为被黑客攻击了---然后下午就排 ...

随机推荐

  1. [转]C#中用NamedPipe进程间通信

    转自:http://blog.csdn.net/jinjazz/archive/2009/02/03/3861143.aspx 本文只是一个测试例子,核心代码是kernel32.dll中的一组wind ...

  2. MySQL系列(十一)--外键约束foreign key的基本使用

    有些时候,为了保证数据的完整性,我们会选择的使用外键约束,例如教师对应的表和课程表中老师的id,这种时候就要使用外键约束了. PS:这里不考虑表结构设计,三范式与反范式等设计问题,基于MySQL8.0 ...

  3. light oj 1105 规律

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  4. DP学习之路(1) 01背包

    动态规划是算法中一门很重要的思想,其通过对每一步的假设规划,不停的寻找最优最有利的解决方案,然后一步一步求解出来. 而01背包是其中最基本的一种dp思想,其题目一般为给定一个容量为V的背包,然后有n件 ...

  5. Python configparser的使用 读取配置文件

    configparser是Python自带用于读取配置文件的工具包,它的使用非常简单 配置文件 配置文件[]中为section命名,section的命名可以包含空格,每个section下面以键值对的方 ...

  6. 初探.NET CORE WEB API(RESTful风格)

    前面有4篇系列博客 (一)Asp.net web api中的坑-[找不到与请求 URI匹配的 HTTP 资源] (二)Asp.net web api中的坑-[http get请求中的参数] (三)As ...

  7. Django项目:CRM(客户关系管理系统)--48--39PerfectCRM实现登录+验证码+过期时间+页面保留账号

    # gbacc_urls.py # ————————38PerfectCRM实现全局账号登录注销———————— from django.conf.urls import url from gbacc ...

  8. xampp中tomcat服务器无法启动

    xampp中的Tomcat服务器无法启动的问题... 我的Java中自己安装了Tomcat服务器,webstorm中还有一个php服务器,,,xampp中的能不能用我就不需要去理会了...反正tomc ...

  9. [转]js的键盘事件

    类型 键盘事件用来描述键盘行为,主要有keydown.keypress.keyup三个事件 keydown 当用户按下键盘上的任意键时触发,如果按住不放的话,会重复触发该事件 <div id=& ...

  10. css3动画性能优化

    css3的动画简单好用,但是性能方面存在一些问题,很多时候一不留神cpu就已经满了. 现在记下一些常用的技巧,去优化我们的css3的动画. 1. translate3d进行gpu加速 写动画的时候写个 ...