React-redux

简介: Redux的工作流程React Component 是页面上的组件 (借书的用户)Store 是存储数据的公共区域(相当于图书馆的管理员)Action Creators 数据传递的过程(当在图书馆借书的时候说的话做的事)Reduc...
Redux的工作流程

React Component 是页面上的组件 (借书的用户)
Store 是存储数据的公共区域(相当于图书馆的管理员)
Action Creators 数据传递的过程(当在图书馆借书的时候说的话做的事)
Reducers 记录本 (还书和借书的地方)

可以通过npm或者是 yarn
npm

npm install --save react-redux 

yarn

yarn add react-redux

因为使用的是redux, 所以所有的数据都存在store之中 在src的目录下创建一个名为store的文件夹 在创建一个index.js

在index.js 文件当中我们这样去写:

import { createStore } from 'redux';
import reducer from './reducer';

const store = createStore(reducer);

export default store;

这个这样理解 store是一个图书的管理员 它需要一个笔记本去帮忙管理这些图书 所以在store文件夹目录下面创建 reducer.js reducer是一个纯函数

//这个数据的初始值
import { CHANGE_VALUE, SUBMIT_TYPE, SPLICE_TYPE, INIT_LIST_ACTION} from '../ActionTypes';

const defaultState = {
    values: '',
    list: []
};
export default (state = defaultState , action) =>{
    if(action.type === CHANGE_VALUE){
        const newState = JSON.parse(JSON.stringify(state));
        newState.values = action.values;
        return newState;
    }

    if(action.type === SUBMIT_TYPE){
        const newState = JSON.parse(JSON.stringify(state));
        newState.list.push(newState.values);
        newState.values = '';
        return newState;
    }
    if(action.type === SPLICE_TYPE){
        const newState = JSON.parse(JSON.stringify(state));
        newState.list.splice(action.index, 1);
        return newState;
    }

    if(action.type === INIT_LIST_ACTION){
        const newState = JSON.parse(JSON.stringify(state));
        newState.list = action.data;
        return newState;
    }
    return state;
}

创建一个组件 test.js和store同级

import React, { Component } from 'react';
import {  Button, Input, List} from 'antd';
import store from './store';
// import { getInputChangeAction, getInputSubmitAction, getInputListAction} from './actionCreators';
import { getInputChangeAction, getInputSubmitAction, getInputListAction, getInitList} from './actionCreators';
import { CHANGE_VALUE, SUBMIT_TYPE, SPLICE_TYPE, GET_INIT_LIST} from './ActionTypes';

   class test extends Component{

      constructor(props){
        super(props);
        this.state= store.getState();
        this.submit = this.submit.bind(this);
        this.changes = this.changes.bind(this);
        this.handChange =this.handChange.bind(this);
        this.listClick =this.listClick.bind(this);
        store.subscribe(this.handChange);
      }

    componentDidMount(){
    }

    changes(e){
        
        let type = CHANGE_VALUE;
        let values = e.target.value;
        const action = getInputChangeAction(type, values);
        store.dispatch(action);
    }

    handChange(){
        this.setState(store.getState());
    }

    submit(){
        let type = SUBMIT_TYPE;
        const action = getInputSubmitAction(type);
        store.dispatch(action);
    }

    listClick(index){
        let type = SPLICE_TYPE;
        const action = getInputListAction(type, index);
        store.dispatch(action);
    //immutable: state 不允许我们做任何的改变 不能直接改state  做性能优化使用的
    // 父 -> 子  属性传值 this.props去接收  
    // 子 -> 父  属性传函数名并且改变this的指向 this.props去调用父组件的方法 this.props.zzz(this.props.dddd)  
    }

    render(){
        return (
            <div>
                <div>
                    <Input 
                    value={this.state.values}
                    placeholder= 'todo list' 
                    onChange ={this.changes}
                    style={{ width: '200px', height: '30px'}} />
                    <Button 
                    onClick ={this.submit}
                    type="primary"
                    >提交
                  </Button>
                </div>
                <List 
                    style={{ width: '200px'}}
                    bordered
                    dataSource={this.state.list}
                    renderItem={item => (<List.Item onClick={this.listClick}>{item}</List.Item>)}
                />
            </div>
        )
    }
}

    export default test;

和test.js同级创建actionCreators.js和ActionTypes.js ,ActionTypes.js是为了写一个声明变量的地方,方便代码管理和优化 写一个共同区域 调用时导入即可
actionCreators.js:


export const getInputChangeAction = (type, values) => ({
    type, 
    values
})

export const getInputSubmitAction = (type) =>({
    type
})

export const getInputListAction = (type, index) =>({
    type, 
    index
})

export const initListAction = (type , data) =>({
    type,
    data
})
export const getInitList = (type) =>({
    type,
})

ActionTypes.js

export const CHANGE_VALUE = 'change_value';
export const SUBMIT_TYPE = 'submit_type';
export const SPLICE_TYPE = 'splice_type';
export const INIT_LIST_ACTION = 'init_list_action';
export const GET_INIT_LIST = 'get_init_lst';
目录
相关文章
|
1月前
|
前端开发 JavaScript 测试技术
从零开始搭建react+typescript+antd+redux+less+vw自适应项目
从零开始搭建react+typescript+antd+redux+less+vw自适应项目
45 0
|
1月前
|
开发框架 前端开发 JavaScript
使用React、Redux和Bootstrap构建社交媒体应用
使用React、Redux和Bootstrap构建社交媒体应用
13 0
|
3月前
|
JavaScript 前端开发
React结合Redux实现Todolist
React结合Redux实现Todolist
17 0
|
9月前
|
存储 JavaScript 前端开发
【React】redux和React-redux
redux和React-redux
65 0
|
6月前
|
前端开发 JavaScript 容器
React的魅力: React-Router-集中式管理和Redux-核心概念
React的魅力: React-Router-集中式管理和Redux-核心概念
43 1
|
7月前
|
JavaScript 前端开发 中间件
React(六) —— redux
React(六) —— redux
|
7月前
|
JavaScript 前端开发 算法
|
7月前
|
Web App开发 JavaScript 前端开发
React | Redux的使用详解(二)
React | Redux的使用详解(二)
|
7月前
|
存储 缓存 JavaScript
React | Redux的使用详解(一)
React | Redux的使用详解(一)
|
7月前
|
JavaScript 前端开发 API
【React】React——redux
【React】React——redux
【React】React——redux