1.项目目录

2.redux

(1)app/redux/action/action.js

/**
* 步骤一
* 行为 action
*/ // 定义行为名称
export const CHANGE_TEXT = 'CHANGE_TEXT'; // 初始化 CHANGE_TEXT 对象
export const changeText = (text) => { // 接收test参数
return {
type: CHANGE_TEXT, // 名称
text // 参数 默认值
}
};

(2)app/redux/reducer/reducer.js

/**
* 步骤二
* 操作
* 通过reducer操作action(根据action行为创建reducer文件)
*/ /**
* 引入 action
* CHANGE_TEXT 类型(行为名称)
* changeText 值
*/
import { CHANGE_TEXT, changeText } from '../action/action'; /**
* 创建 reducer
* 根据名称判断是哪一个行为
* state = changeText('welcome to React Native') 初始化state
*/
const mainReducer = (state = changeText('welcome to React Native'), action) => {
/**
* state 不能直接改变
* 定义newState 接收state的值
*/
const newState = state;
const text = action.text; // 判断 action 类型
switch (action.type) {
case CHANGE_TEXT:
return {
// 返回所有的newState
...newState,
text: '改变了' + text
}; default:
return {
...newState,
text:state.text
}
}
}; // 输出口
export default mainReducer;

(3)app/redux/store/store.js

/**
* 步骤三
* 初始化 store
*/
// 引入 reducer(操作)
import Reducer from '../reducer/reducer';
// 获取redux中的初始化方法 createStore
import { createStore } from 'redux'; // 输出
export default () => { // 根据 reducer 初始化 store
const store = createStore(Reducer); return store;
}

3.页面引用

(1)app/containers/Index.js

/**
* 容器组件
* 入口文件
*/
import React, { Component } from 'react'; // 引用外部文件
import { Provider } from 'react-redux';
import Main from './Main';
import configureStore from '../redux/store/store'; // 调用 store 文件中的 mainReducer常量中保存的方法
const store = configureStore();
// 定义根组件
export default class Root extends Component {
render() {
return(
// 第一层包装,为了让 main 能够拿到 store
<Provider store={store}>
<Main />
</Provider>
)
}
}

(2)app/containers/Main.js

/*主页面*/
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
TouchableOpacity,
} from 'react-native'; // 引入 测试组件
import TestText from '../components/TestText'
/**
* 获取 react-redux的 connect() 方法
* 注:使组件层级中的 connect() 方法能够得到 redux store
*/
import { connect } from 'react-redux';
// 获取 action行为的值
// import { CHANGE_TEXT } from '../redux/action/action';
import { changeText } from '../redux/action/action'; class Main extends Component {
render() {
// 通过 props 拿到保存的 onChangeText
const { onChangeText } = this.props; return (
<View style={styles.container}>
{/* 需要改变的组件 */}
{/* 将父组件(Main)的props,传递给子组件(TestText)*/}
<TestText {...this.props} /> {/* 按钮 */}
<TouchableOpacity
// 设置按钮点击事件
onPress={onChangeText}
>
<Text>改变文字按钮</Text>
</TouchableOpacity>
</View>
);
}
} const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
}); /************ 初始化 ************/
// 获取 state 变化
const mapStateToProps = (state) => {
return {
// 获取 state 变化
value: state.text,
}
}; // 发送行为
const mapDispatchToProps = (dispatch) => {
return {
// 发送行为
// onChangeText: () => dispatch({type: CHANGE_TEXT}),
onChangeText: () => dispatch(changeText('外部传值')),
}
}; /**
* 通过 connect() 方法 对Main组件进行第二层包装
* 进行第二层包装,生成的新组件拥有 接收和发送 数据的能力
* mapStateToProps 获取状态的函数
* mapDispatchToProps 发送行为的函数
*/
export default connect(mapStateToProps, mapDispatchToProps)(Main);

(3)app/components/TestText.js

/*测试组件*/
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native'; export default class TestText extends Component {
render() {
// 获取 props 中的 value
const { value } = this.props; return (
// 根据 value 改变内部文字
<Text>{value}</Text>
);
}
}

.

react-native redux 操作的更多相关文章

  1. react native redux 草稿

    Provider > Provider > 使组件层级中的 方法都能够获得 Redux store.正常情况下,你的根组件应该嵌套在 Provider 中才能使用 方法. 如果你真的不想把 ...

  2. react native redux saga增加日志功能

    redux-logger地址:https://github.com/evgenyrodionov/redux-logger 目前Reac native项目中已经使用redux功能,异步中间件使用red ...

  3. react native redux

    redux可以解决, 程序中所有组件的状态统一管理, 从而使我们可以更加动态的,灵活的控制程序 React:数据管理使用props.stateRedux的主要思想:提供一个数据存储中心,可以供外部访问 ...

  4. React Native & debug & debugger

    React Native & debug & debugger http://localhost:8081/debugger-ui/ react-devtools # yarn: $ ...

  5. 从React Native到微服务,落地一个全栈解决方案

    Poplar是一个社交主题的内容社区,但自身并不做社区,旨在提供可快速二次开发的开源基础套件.前端基于React Native与Redux构建,后端由Spring Boot.Dubbo.Zookeep ...

  6. 在 React Native 中使用 Redux 架构

    前言 Redux 架构是 Flux 架构的一个变形,相对于 Flux,Redux 的复杂性相对较低,而且最为巧妙的是 React 应用可以看成由一个根组件连接着许多大大小小的组件的应用,Redux 也 ...

  7. React Native集成Redux框架讲解与应用

    学过React Native的都知道,RN的UI是根据相应组件的state进行render的,而页面又是由大大小小的组件构成,导致每个组件都必须维护自身的一套状态,因此当页面复杂化的时候,管理stat ...

  8. React Native 开发豆瓣评分(三)集成 Redux

    什么是 redux redux 是一个用于管理 js 应用状态(state)的容器.比如组件 A 发生了变化,组件 B 要同时做出响应.常见的应用场景就是用户的登录退出操作:未登录状态,个人中心显示登 ...

  9. react native 中的redux 理解

    redux 中主要分为三大块,分别是Action Reducer 与Store. 1.Action是js的一个普通对象,是store数据的唯一来源.通过store.dispath()讲action传到 ...

  10. [RN] React Native 使用 Redux 比较详细和深刻的教程

    React Native 使用 Redux 比较详细和深刻的教程 React Native 使用 Redux https://www.jianshu.com/p/06fc18cef56a http:/ ...

随机推荐

  1. jquery 获取日期 date 对象、 判断闰年

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. jQuery 超过字符截取部分用星号表示

    $(function(){ var str = $('#num').text(); if (str.length >15) { var strend = str.substring(4,str. ...

  3. badblocks - 查询设备的坏区块

    语法(SYNPSIS) badblocks [ -svwnf ] [ -b block-size ] [ -c blocks_at_once ] [ -i input_file ] [ -o outp ...

  4. error while loading shared libraries: libclntsh.so.11.1

    解决这个问题有两种方法 1.在当前用户下,添加链接库所在路径 LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH; export LD_LIBRARY_ ...

  5. apt-get update 报错 W: Unknown Multi-Arch type 'no' for package 'compiz-core'

    源 #deb包 deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb http:// ...

  6. 【VScode】使用VScode 来写markdown时序图

    准备工作 在VScode中下载插件Markdown Preview Enhanced插件 创建一个.md文件 在VScode中打开文件,界面内点击右键可以看到Open preview to the s ...

  7. 【传智播客】Libevent学习笔记(一):简介和安装

    目录 00. 目录 01. libevent简介 02. Libevent的好处 03. Libevent的安装和测试 04. Libevent成功案例 00. 目录 @ 01. libevent简介 ...

  8. 洛谷——P1731 [NOI1999]生日蛋糕

    P1731 [NOI1999]生日蛋糕 搜索+剪枝 常见的剪枝: 若当前状态+后面所要搜索的最差的状态$>$或是$<$最后的状态,就返回 预处理最差的状态 #include<iost ...

  9. <Redis> 入门四 Jedis操作Redis

    pom依赖 <dependencies> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> < ...

  10. Sax解析xml文档

    测试的xml数据: <?xml version="1.0" encoding="utf-8" ?> <note> <to>G ...