Leisure Moment

乱花渐欲迷人眼,浅草才能没马蹄。最爱湖东行不足,绿杨阴里白沙堤。


  • Home

  • Archives

  • Tags

  • About

  • Sitemap

redux同步使用学习

Posted on 2017-09-18

传统mvc 模型


model 与 view 之间的双向对话, 让数据和状态管理变的复杂

Redux (Flux + reducer)

基于Flux 单向数据流的状态管理框架

  1. 单向数据流
  2. 唯一数据源 (Store)
  3. 保持状态只读
  4. 数据改变只能通过纯函数完成(Reducer)

Action

  • 行为的抽象
  • 普通的JS 对象
  • 必须有一个type
  • 一般由方法生成
1
2
// Action
const increaseAction = { type: 'increase' }

Reducer

响应的抽象,只要传入参数相同,返回计算得到的下一个 state 就一定相同。没有特殊情况、没有副作用,没有 API 请求、没有变量修改,单纯执行计算。 (previousState, action) => newState

1
2
3
4
5
6
7
8
9
10
11
// Reducer
function counter(state = { count: 0 }, action) {
const count = state.count
switch (action.type) {
case 'increase':
return { count: count + 1 }
default:
return state
}
}
export default todos

Store

状态树, 状态的“数据库”,
Store 有以下职责:

  • 维持应用的 state;
  • 提供 getState() 方法获取 state;
  • 提供 dispatch(action) 方法更新 state;
  • 通过 subscribe(listener) 注册监听器;
  • 通过 subscribe(listener) 返回的函数注销监听器

1
2
// Store
const store = createStore(counter)

container 与 component 区分

component: UI组件

不含有状态,UI 组件又称为”纯组件”,即它纯函数一样,纯粹由参数决定它的值

container: 容器组建

  • 负责管理数据和业务逻辑,不负责 UI 的呈现
  • 带有内部状态
  • 使用 Redux 的 API

connect 用于从UI组件生成容器组件

connect方法接受两个参数:mapStateToProps和mapDispatchToProps

  • (1)输入逻辑:外部的数据(即state对象)如何转换为 UI 组件的参数
  • (2)输出逻辑:用户发出的动作如何变为 Action 对象,从 UI 组件传出去。

mapStateToProps()

mapStateToProps是一个函数。它的作用就是像它的名字那样,建立一个从(外部的)state对象到(UI 组件的)props对象的映射关系。


1
2
3
4
5
6
// Map Redux state to component props
function mapStateToProps(state) {
return {
value: state.count
}
}

connect方法可以省略mapStateToProps参数,那样的话,UI 组件就不会订阅Store,就是说 Store 的更新不会引起 UI 组件的更新。


mapDispatchToProps()

mapDispatchToProps是connect函数的第二个参数,用来建立 UI 组件的参数到store.dispatch方法的映射。也就是说,它定义了哪些用户的操作应该当作 Action,传给 Store。它可以是一个函数,也可以是一个对象。

1
2
3
4
5
6
// Map Redux actions to component props
function mapDispatchToProps(dispatch) {
return {
onIncreaseClick: () => dispatch(increaseAction)
}
}

provider 组件

connect方法生成容器组件以后,需要让容器组件拿到state对象,才能生成 UI 组件的参数。
一种解决方法是将state对象作为参数,传入容器组件。但是,这样做比较麻烦,尤其是容器组件可能在很深的层级,一级级将state传下去就很麻烦。React-Redux 提供Provider组件,可以让容器组件拿到state

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

实例


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import ReactDOM from 'react-dom'
import { createStore } from 'redux'
import { Provider, connect } from 'react-redux'

// React component
class Counter extends Component {
render() {
const { value, onIncreaseClick } = this.props
return (
<div>
<span>{value}</span>
<button onClick={onIncreaseClick}>Increase</button>
</div>
)
}
}

Counter.propTypes = {
value: PropTypes.number.isRequired,
onIncreaseClick: PropTypes.func.isRequired
}

// Action
const increaseAction = { type: 'increase' }

// Reducer
function counter(state = { count: 0 }, action) {
const count = state.count
switch (action.type) {
case 'increase':
return { count: count + 1 }
default:
return state
}
}

// Store
const store = createStore(counter)

// Map Redux state to component props
function mapStateToProps(state) {
return {
value: state.count
}
}

// Map Redux actions to component props
function mapDispatchToProps(dispatch) {
return {
onIncreaseClick: () => dispatch(increaseAction)
}
}

// Connected Component
const App = connect(
mapStateToProps,
mapDispatchToProps
)(Counter)

ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)

Maven在eclipse的配置(解决Plugin execution not covered by lifecycle configuration\:org.apache.maven.pl)

Posted on 2017-09-18

Maven在eclipse的配置(解决Plugin execution not covered by lifecycle configuration: org.apache.maven.pl)

  1. 下载Maven binary
  2. 设置MVN_HOME 环境变量, 添加Bin下路径到Path环境变量
  3. 修改conf文件下setting.xml,设置为阿里源

    1
    2
    3
    4
    5
    6
    7
    8
    <mirrors>
    <mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
    <mirrorOf>central</mirrorOf>
    </mirror>
    </mirrors>
  4. 打开eclipse->preference->Maven

  5. 修改Installation,自己所装maven路径
  6. 修改User setting, 制定为设置阿里源的setting.xml
  7. 右键maven工程 -> Maven-> Update Project

如何使用webpack构建单页面React应用

Posted on 2017-06-14

entry

打包的入口文件,有几个入口文件就会生成几个bundle文件

output

打包后的输出设置

1
2
3
output = {
fileName : [XXX].js // 打包后的文件名
}

loader

打包之前的预处理器,处理各种资源文件

css loader& style loader

  • css loader 负责解析css文件
  • style loader 负责将css style插入到html中

如何在webpack打包中包括css打包?

  1. loaders:[ { test: /\.css$/, loader: 'style-loader!css-loader' },
    ]

  2. 以及在root.js中import/require
    require('../css/app.css');

语法中的!(感叹号)用来连接link 不同的loader

  • url loader 预处理图片资源
    1
    2
    3
    loaders:[
    { test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192' }
    ]

? 用来向loader传入参数,这里表示8192以下用base64转码

Common chunk

CommonsChunkPlugin
将脚本中相同chunk块打包成一个单独的文件的插件

Webpack配置文件中用到的特殊符号

! : loader的连接符号 比如 ‘style-loader!css-loader’

& : 向loader传递参数

解决:Cannot find module ‘webpack/lib/node/NodeTemplatePlugin

1
2
3
npm remove webpack -g
npm i webpack --save-dev
npm run ./node_modules/.bin/webpack

一个参考模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var webpack = require("webpack");
var path = require("path");
var HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
context: path.join(__dirname),
entry: "./src/js/root.js",
module: {
loaders: [
{
test: /\.js?$/,
exclude: /(node_modules)/,
loader: "babel-loader",
query: {
presets: ["react", "es2015", "stage-0"],
plugins: ["react-html-attrs"]
}
},
{ test: /\.css$/, loader: "style-loader!css-loader" },
{
test: /\.(png|jpg|jpeg)$/,
loader: "url-loader?limit=8192",
use: ["file-loader"]
}
]
},
entry: {
app: path.resolve(__dirname, "./src/js/root.js")
},
output: {
path: path.resolve(__dirname, "./dist"),
filename: "./src/bundle.js"
},
plugins: [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
new HtmlWebpackPlugin({
title: "resume-system-front",
filename: "app.html",
template: "./index.html" //Load a custom template
})
]
};
1…456…8
zhong-wei

zhong-wei

23 posts
3 categories
7 tags
RSS
© 2016 - 2019 zhong-wei