Ashun's 技術駅 Ashun's 技術駅
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • Vue
  • 现代web布局
  • React
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 技术资源
  • 第一阶段

    • HTML
  • 第二阶段

    • JavaScript
  • 第三阶段

    • Vue
  • 第四阶段

    • 实战项目
  • 每周测试

    • 每周
  • 其他

    • Vue引入UI框架
    • Web前端面试
    • Vue3-resource
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 福利资源
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Ashun

前端界的小学生
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • Vue
  • 现代web布局
  • React
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 技术资源
  • 第一阶段

    • HTML
  • 第二阶段

    • JavaScript
  • 第三阶段

    • Vue
  • 第四阶段

    • 实战项目
  • 每周测试

    • 每周
  • 其他

    • Vue引入UI框架
    • Web前端面试
    • Vue3-resource
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 福利资源
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • vue

  • vue3

  • es6

  • JavaScript

  • css

  • webpack

  • http

  • NodeJS

  • React

    • 01React
    • 02Real DOM_Virtual DOM
    • 03life cycle
    • 04state_props
    • 05super()_super(props)
    • 06setState
    • 07SyntheticEvent
    • 08Binding events
    • 09Building components
    • 10communication
    • 11key
    • 12React refs
    • 13class_function component
    • 14controlled_Uncontrolled
    • 15High order components
    • 16React Hooks
    • 17import css
    • 18animation
    • 19redux
    • 20Redux Middleware
    • 21how to use redux
      • 面试官:你在React项目中是如何使用Redux的? 项目结构是如何划分的?
      • 一、背景
      • 二、如何做
        • Provider
        • connection
        • mapStateToProps
        • mapDispatchToProps
        • 小结
      • 三、项目结构
        • 按角色组织(MVC)
        • 按功能组织
      • 参考文献
    • 22React Router
    • 23React Router model
    • 24immutable
    • 25render
    • 26improve_render
    • 27diff
    • 28Fiber
    • 29JSX to DOM
    • 30Improve performance
    • 31capture error
    • 32server side rendering
    • 33summary
  • git

  • linux

  • typescript

  • algorithm

  • applet

  • design

  • 《Web前端面试》
  • React
xugaoyi
2022-03-25
目录

21how to use redux

# 面试官:你在React项目中是如何使用Redux的? 项目结构是如何划分的?

# 一、背景

在前面文章了解中,我们了解到redux是用于数据状态管理,而react是一个视图层面的库

如果将两者连接在一起,可以使用官方推荐react-redux库,其具有高效且灵活的特性

react-redux将组件分成:

  • 容器组件:存在逻辑处理
  • UI 组件:只负责现显示和交互,内部不处理逻辑,状态由外部控制

通过redux将整个应用状态存储到store中,组件可以派发dispatch行为action给store

其他组件通过订阅store中的状态state来更新自身的视图

# 二、如何做

使用react-redux分成了两大核心:

  • Provider
  • connection

# Provider

在redux中存在一个store用于存储state,如果将这个store存放在顶层元素中,其他组件都被包裹在顶层元素之上

那么所有的组件都能够受到redux的控制,都能够获取到redux中的数据

使用方式如下:

<Provider store = {store}>
    <App />
<Provider>
1
2
3

# connection

connect方法将store上的getState和 dispatch包装成组件的props

导入conect如下:

import { connect } from "react-redux";
1

用法如下:

connect(mapStateToProps, mapDispatchToProps)(MyComponent)
1

可以传递两个参数:

  • mapStateToProps

  • mapDispatchToProps

# mapStateToProps

把redux中的数据映射到react中的props中去

如下:

const mapStateToProps = (state) => {
    return {
        // prop : state.xxx  | 意思是将state中的某个数据映射到props中
        foo: state.bar
    }
}
1
2
3
4
5
6

组件内部就能够通过props获取到store中的数据

class Foo extends Component {
    constructor(props){
        super(props);
    }
    render(){
        return(
         // 这样子渲染的其实就是state.bar的数据了
            <div>this.props.foo</div>
        )
    }
}
Foo = connect()(Foo)
export default Foo
1
2
3
4
5
6
7
8
9
10
11
12
13

# mapDispatchToProps

将redux中的dispatch映射到组件内部的props中

const mapDispatchToProps = (dispatch) => { // 默认传递参数就是dispatch
  return {
    onClick: () => {
      dispatch({
        type: 'increatment'
      });
    }
  };
}

1
2
3
4
5
6
7
8
9
10
class Foo extends Component {
    constructor(props){
        super(props);
    }
    render(){
        return(
         
             <button onClick = {this.props.onClick}>点击increase</button>
        )
    }
}
Foo = connect()(Foo);
export default Foo;
1
2
3
4
5
6
7
8
9
10
11
12
13

# 小结

整体流程图大致如下所示:

# 三、项目结构

可以根据项目具体情况进行选择,以下列出两种常见的组织结构

# 按角色组织(MVC)

角色如下:

  • reducers
  • actions
  • components
  • containers

参考如下:

reducers/
  todoReducer.js
  filterReducer.js
actions/
  todoAction.js
  filterActions.js
components/
  todoList.js
  todoItem.js
  filter.js
containers/
  todoListContainer.js
  todoItemContainer.js
  filterContainer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 按功能组织

使用redux使用功能组织项目,也就是把完成同一应用功能的代码放在一个目录下,一个应用功能包含多个角色的代码

Redux中,不同的角色就是reducer、actions和视图,而应用功能对应的就是用户界面的交互模块

参考如下:

todoList/
  actions.js
  actionTypes.js
  index.js
  reducer.js
  views/
    components.js
    containers.js
filter/
  actions.js
  actionTypes.js
  index.js
  reducer.js
  views/
    components.js
    container.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

每个功能模块对应一个目录,每个目录下包含同样的角色文件:

  • actionTypes.js 定义action类型
  • actions.js 定义action构造函数
  • reducer.js 定义这个功能模块如果响应actions.js定义的动作
  • views 包含功能模块中所有的React组件,包括展示组件和容器组件
  • index.js 把所有的角色导入,统一导出

其中index模块用于导出对外的接口

import * as actions from './actions.js';
import reducer from './reducer.js';
import view from './views/container.js';

export { actions, reducer, view };
1
2
3
4
5

导入方法如下:

import { actions, reducer, view as TodoList } from './xxxx'
1

# 参考文献

  • https://www.redux.org.cn/docs/basics/UsageWithReact.html (opens new window)
  • https://segmentfault.com/a/1190000010384268 (opens new window)
编辑 (opens new window)
上次更新: 2023/08/06, 00:38:41
20Redux Middleware
22React Router

← 20Redux Middleware 22React Router→

最近更新
01
课件-react路由-V6
01-22
02
课件-国际化
01-22
03
课件-redux-toolkit
01-22
更多文章>
Theme by Vdoing | Copyright © 2019-2024 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式