美文网首页
第1.3章:Redux数据传递框架

第1.3章:Redux数据传递框架

作者: 赵羽珩 | 来源:发表于2019-06-20 11:56 被阅读0次
1、Redux简介

Redux的设计理念是把数据放到Store里,统一管理

image.png
2、Redux工作流程

蓝色React Components:借书的人(组件)
黄色Action Creators:我要借什么书?(请求)
橙色Store:图书馆(数据仓库)
紫色Reducers:图书馆管理员(查找,数据仓库记录册)

Redux工作流程顺序:蓝色 > 黄色 > 橙色 > 紫色 > 橙色 > 蓝色

图书馆例子
3、Redux安装
yarn add redux

4、创建橘色Store 数据仓库

index.js页面内容如下(默认写法CV即可)

image.png
// 引入redux框架 调用 createStore 方法
import { createStore } from 'redux';
import reducer from './reducer';

// 定义 store 用 createStore() 方法,创建了一个数据的公共存储仓库
  const store = createStore(
    reducer, /* preloadedState, */
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  );

 export default store
 

5、创建紫色Reducers 接收数据并改变,最后返回给橘色Store

Reducers 接收(state, action)
state = defaultState、是以前默认旧数据
action、是当前要执行的操作

reducer.js页面内容如下

image.png
// defaultState 默认数据
const defaultState = {
  inputValue: '',
  list: [
    'Racing car sprays burning fuel into crowd.',
    'Japanese princess to wed commoner.',
  ],
}

// Reducers 接收(state, action)
// state = defaultState、是以前默认旧数据
// action、当前要执行的操作
export default (state = defaultState, action) => {
  if (action.type === 'change_input_value') {
    const newState = JSON.parse(JSON.stringify(state)); // 定义 newState = 默认旧数据
    newState.inputValue = action.value; // 默认旧数据の.inputValue值 = action传过来の.新值
    return newState;  // 把新数据返回 stroe 替换旧数据
  }
  if (action.type === 'add_todo_Item') {
    const newState = JSON.parse(JSON.stringify(state));
    if (newState.inputValue !== '') {
      newState.list.push(newState.inputValue);
      newState.inputValue = '';
    }
    return newState;
  }
    if (action.type === 'del_todo_Item') {
      const newState = JSON.parse(JSON.stringify(state)); 
      newState.list.splice(action.index, 1);
      return newState; 
    }
  return state;
}


6、页面蓝色组件Components黄色Action部分

黄色Action修改数据,传递给橙色Store紫色Reducers,再返回给蓝色Components

image.png
import React, { Component } from 'react'
import 'antd/dist/antd.css'
import { Input, Button, List } from 'antd';
import store from './store';

export class TodoList extends Component {

  constructor(props) {
    super(props)

    this.state = store.getState(); // 获取 store 的所有数据内容
    this._StoreChange = this._StoreChange.bind(this);
    store.subscribe(this._StoreChange) // store 的数据发生改变,将新值传给组件
    
    this._onChangeInput = this._onChangeInput.bind(this);
    this._onClickBtn = this._onClickBtn.bind(this);
  }
  _StoreChange() { // 当数据改变是 执行该函数
    this.setState(store.getState()) // 获取 新数据内容
  }

    render() {
        return (
        <div style={{marginTop: '10px',marginLeft: '10px'}}>
            <h1>Hello World</h1>
            <Input 
                placeholder = "请输入"
                value={this.state.inputValue} 
                style={{width: '300px',marginRight: '10px'}}
                onChange={this._onChangeInput}
            />
            <Button type="primary" onClick={this._onClickBtn}>提交</Button>
            <List
                style={{marginTop: '10px', width: '300px'}}
                bordered
                dataSource={this.state.list}
                renderItem={(item, index) => 
                    <List.Item onClick={this._onClickListItemDel.bind(this, index)}>{item}</List.Item>
                }
            />
        </div>
        )
    }

    _onChangeInput(e) {
        const action = {
            type: "change_input_value",
            value: e.target.value
        }
        store.dispatch(action); // 发送 action 给 store
    }
  
    _onClickBtn() {
        const action = {
        type: "add_todo_Item",
        }
        store.dispatch(action);
    }

    _onClickListItemDel(index) {
        const action = {
        type: "del_todo_Item",
        index
        }
        store.dispatch(action);
    }
}

export default TodoList

相关文章

  • Redux

    一、什么是Redux Redux是一个数据层的框架,React可以借助Redux来实现数据的传递Redux等于Re...

  • flutter_redux框架的使用

    框架简介以及作用 flutter_redux是基于InheritedWidget封装的用于Widget树的数据传递...

  • 第1.3章:Redux数据传递框架

    1、Redux简介 Redux的设计理念是把数据放到Store里,统一管理 2、Redux工作流程 蓝色React...

  • Redux入门学习

    Redux是什么 Redux解决数据传递的一个框架。简单的说就是把组件中的数据放到一个公用的存储区域去存储。 Re...

  • Redux手把手教学,有这篇就够了

    Redux 是 React 框架下的一款状态管理工具,可以实现多个组件之间的数据共享和传递。学习和掌握 Redux...

  • 简述 Redux

    redux:解决数据传递问题,把所有数据放在store中传递。 1.基于react什么时候要用redux reac...

  • redux 超神篇

    为什么需要redux 解决组件间数据传递的问题 redux的简单流程原理图: redux 和 react-redu...

  • React之Flux简单总结

    React不适合来管理应用数据,而Redux就是管理应用状态的框架。Flux是单向数据流框架的始祖,而Redux是...

  • Redux-----React的好搭档!

    Redux 1.数据传递 1.props 2.回调 3.相邻兄弟元素传递数据 2....

  • React Redux原理和使用方法总结

    简介 React Redux框架可以用来对React Native进行数据流管理。Redux是一个用于UI布局框架...

网友评论

      本文标题:第1.3章:Redux数据传递框架

      本文链接:https://www.haomeiwen.com/subject/utjnzqtx.html