React:Redux简介

let nextState = todoApp(previousState, action);

// 设置监听函数

store.subscribe(listener);

  1. listener可以通过store.getState()得到当前状态。如果使用的是 React,这时可以触发重新渲染 View

<>3. redux的原则

  1. state 是只读的,唯一修改它的方式是 actions

  2. 更新的唯一方式:dispatch(action) -> reducer -> new state

  3. Reducer 函数必须是"纯"的 ------ 不能修改它的参数,也不能有副作用(不能对函数作用域之外的变量做任何更改。不要改变函数作用域以外的变量,不要调用其他会改变的函数,也不要dispatch actions等)

<>三、Redux改造TodoList


  1. 将分散在各个组件的状态统一放在store全局对象上,这样在每个组件需要状态时可以直接引入store取出状态,避免了状态之间的多层传递

  2. 对state的所有操作必须通过dispatch发出action给reducer,由reducer根据action的type对state进行操作并返回新的state,这使state的改变可容易追踪和可控

  3. 因为store是全局的,因此子组件不需要组件向父组件开放过多的接口(不需要传入那么多的变量给prop)

<>四、TodoList改造步骤


<>1. 目录结构规划

src |

-| commponents

-| store

App.js

index.css

index.js

store |

-| index.js

-| reducers.js

-| actionCreators

-| actionTypes

commponents |

-| Foot.jsx

-| Item.jsx

-| List.jsx

-| Top.jsx

<>2. store/index.js

  1. 说明:全局状态对象store创建和导出,它需要提供一个返回 state 的函数------reducers

import {createStore, applyMiddleware, compose} from 'redux'

import reducers from './reducers'

import ReduxThunk from 'redux-thunk'

// 处理redux-thunk的兼容问题

const composeEnhancers =

typeof window === 'object' &&

window.REDUX_DEVTOOLS_EXTENSION_COMPOSE ?

window.REDUX_DEVTOOLS_EXTENSION_COMPOSE({}) : compose;

const enhancer = composeEnhancers(

applyMiddleware(ReduxThunk)

);

//创建store状态管理对象

const store = createStore(reducers, enhancer);

export default store;

<>3. store/actionTypes

  1. 说明:

① Action对象描述你想做出的改变或者将触发的事件,它必须带有一个type 属性,为action提供type描述(说明),约定action在reducers中的匹配,这个文件就是将所有的type定义为常量,避免编写时出错

② actionCreators中定义的每个回调函数返回的action对象上都有一个type属性,用以标识每个action

③ 在界面通过store.dispatch(action)(这里的action都是从actionCreators中引入的)派发到renducers时,renducers函数是通过actions.type来判断传过来的是哪个action

export const GET_ALL_ITEM = 'get_all_item'; // 存TODO

export const CHANGE_TODO_ITEM = 'change_todo_item'; // 修改一条TODO的选中状态

export const DEL_TODO_ITEM = 'del_todo_item'; //删除一条TODO

export const ADD_TODO_ITEM = 'add_todo_item'; //添加一条TODO

export const DEL_FINISHED_TODO_ITEM = 'del_finished_todo_item'; //删除所有已经完成的TODO

export const IS_CHECKED_ALL_TODO_ITEM = 'is_checked_all_todo_item'; //删除所有已经完成的TODO

<>4. store/actionCreators.js

  1. 说明:

① 这里定义了所有的action,通过回调函数的形式返回action对象,使用回调函数的目的是为了让界面组件可以调用

② 每个action都有一个type属性,它定义了该action的类型;同时接受一个参数,是界面组件需要的状态,由action带给reducer

③ 界面上调用这里定义的回调函数(返回一个对象,对象的第一个属性type是,第二个参数是从界面接收的参数),通过store.dispatch(action)将action派发到reducers,reducres根据action.type属性对state进行相应处理

④ action充当了界面与reducers的桥梁,将界面中组件需要的数据和对数据进行的处理传给renducers,由reducers负责更新state

import {

GET_ALL_ITEM,

CHANGE_TODO_ITEM,

DEL_TODO_ITEM,

ADD_TODO_ITEM,

DEL_FINISHED_TODO_ITEM,

IS_CHECKED_ALL_TODO_ITEM

} from './actionTypes'

// 1. 存Todo

export const getAllItemAction = (todos)=>({

type: GET_ALL_ITEM,

todos

});

// 2. 单个TODO选中与否

export const getChangeItemFinishedAction = (todoId, flag)=>({

type: CHANGE_TODO_ITEM,

todoId,

flag

});

// 3. 单个TODO删除

export const getDelItemAction = (todoId)=>({

type: DEL_TODO_ITEM,

todoId

});

// 4. 添加一条记录

export const getAddItemAction = (todo)=>({

type: ADD_TODO_ITEM,

todo

});

// 5. 删除所有已经完成的记录

export const getRemoveFinishedItemAction = ()=>({

type: DEL_FINISHED_TODO_ITEM

});

// 6. 全选与非全选

export const getIsCheckedAllAction = (flag)=>({

type: IS_CHECKED_ALL_TODO_ITEM,

flag

});

<>5. store/reducers

  1. 说明:

① reducer 的职责是接收当前 state 和一个 action 然后返回新的 state

② 状态state以及操作状态的方法都在这里,符合了数据和操作数据的方法在同一个文件的原则

③ 根据在界面组件中通过store.dispatch(action)派发过来的action.type,匹配到相应的操作状态的方法,并根据组件传入的 参数,对state进行相应操作,将当前state直接进行替换,并合并旧的state返回给store

④ reducer是直接用新的state替换旧的state,而不是更新旧的state,就是说旧的state还是保留的;返回最新的state给界面组件,同时保留了新state和旧state存入store,使state可以回溯和方便进行时间旅行调试

import {

GET_ALL_ITEM,

CHANGE_TODO_ITEM,

DEL_TODO_ITEM,

ADD_TODO_ITEM,

DEL_FINISHED_TODO_ITEM,

IS_CHECKED_ALL_TODO_ITEM

} from './actionTypes'

// 初始状态数据

const defaultState = {

todos: [],

finishedCount: 0

};

//根据action的type,对state进行相应操作,并返回新的state

export default (state = defaultState, action)=>{

console.log(state, action);

// 1. 存所有的Todo

if(action.type === GET_ALL_ITEM){

const newState = JSON.parse(JSON.stringify(state));

newState.todos = action.todos;

return newState;

}

// 2. 选中与取消选中

if(action.type === CHANGE_TODO_ITEM){

const newState = JSON.parse(JSON.stringify(state));

// 1. 遍历

let tempFinishedCount = 0;

newState.todos.forEach((todo, index)=>{

if(action.todoId === todo.id){

todo.finished = action.flag;

}

if(todo.finished){

tempFinishedCount += 1;

}

});

// 2. 返回一个新的状态

newState.finishedCount = tempFinishedCount;

return newState;

}

// 3. 单个TODO删除

if(action.type === DEL_TODO_ITEM){

const newState = JSON.parse(JSON.stringify(state));

// 1. 遍历

let tempFinishedCount = 0;

newState.todos.forEach((todo, index)=>{

if(action.todoId === todo.id){

newState.todos.splice(index, 1);

}

});

// 处理选中的

newState.todos.forEach((todo, index)=>{

if(todo.finished){

tempFinishedCount += 1;

}

});

// 2. 返回新状态

newState.finishedCount = tempFinishedCount;

return newState;

}

// 4. 添加一条记录

if(action.type === ADD_TODO_ITEM){

console.log(action);

const newState = JSON.parse(JSON.stringify(state));

newState.todos.push(action.todo);

return newState;

}

// 5. 删除所有已经完成的记录

if(action.type === DEL_FINISHED_TODO_ITEM){

const newState = JSON.parse(JSON.stringify(state));

let tempArr = [];

newState.todos.forEach((todo, index)=>{

if(!todo.finished){ // 没有完成的任务

tempArr.push(todo);

}

});

// 2. 返回新状态

newState.finishedCount = 0;

newState.todos = tempArr;

return newState;

}

// 6. 全选与非全选

if(action.type === IS_CHECKED_ALL_TODO_ITEM){

const newState = JSON.parse(JSON.stringify(state));

// 6.1. 遍历

let tempFinishedCount = 0;

newState.todos.forEach((todo, index)=>{

todo.finished = action.flag;

});

// 处理选中的

newState.todos.forEach((todo, index)=>{

if(todo.finished){

tempFinishedCount += 1;

}

});

// 6.2. 返回新状态

newState.finishedCount = tempFinishedCount;

return newState;

}

return state;

}

<>Redux中间件

===================================================================

<>一、redux-thunk


  1. 概念

① redux-thunk是一个中间件,需要配合redux提供的applyMiddleware一起使用

② 主要是将常规的对象类型的action扩展为可接受函数类型的action

③ 它可以让原本只支持同步方式的redux扩展为支持异步的方式

④ 操作流程

  1. 将需要修改的state都存入到store里
总结一下

面试前要精心做好准备,简历上写的知识点和原理都需要准备好,项目上多想想难点和亮点,这是面试时能和别人不一样的地方。

还有就是表现出自己的谦虚好学,以及对于未来持续进阶的规划,企业招人更偏爱稳定的人。

万事开头难,但是程序员这一条路坚持几年后发展空间还是非常大的,一切重在坚持。

为了帮助大家更好更高效的准备面试,特别整理了《前端工程师面试手册》电子稿文件。

前端面试题汇总

相关推荐
(⊙o⊙)~哦42 分钟前
JavaScript substring() 方法
前端
无心使然云中漫步1 小时前
GIS OGC之WMTS地图服务,通过Capabilities XML描述文档,获取matrixIds,origin,计算resolutions
前端·javascript
Bug缔造者1 小时前
Element-ui el-table 全局表格排序
前端·javascript·vue.js
xnian_2 小时前
解决ruoyi-vue-pro-master框架引入报错,启动报错问题
前端·javascript·vue.js
麒麟而非淇淋3 小时前
AJAX 入门 day1
前端·javascript·ajax
2401_858120533 小时前
深入理解MATLAB中的事件处理机制
前端·javascript·matlab
阿树梢3 小时前
【Vue】VueRouter路由
前端·javascript·vue.js
随笔写4 小时前
vue使用关于speak-tss插件的详细介绍
前端·javascript·vue.js
史努比.4 小时前
redis群集三种模式:主从复制、哨兵、集群
前端·bootstrap·html
快乐牌刀片884 小时前
web - JavaScript
开发语言·前端·javascript