Skip to content

Commit cf9b118

Browse files
committed
modify by dk
1 parent b276e95 commit cf9b118

File tree

19 files changed

+318
-30
lines changed

19 files changed

+318
-30
lines changed

component/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ ReactDOM.render(<Component1 name="Tom" age="18"/>, document.getElementById('div1
7171
```
7272
### 默认属性(DefaultProps)
7373
组件的属性除了可以通过调用的时候以 DOM 节点属性的方式传值,也可以设置默认的属性值,如果调用的时候没有传对应的属性值,则会用默认的属性值。
74+
`getDefalutProps` 这个方法只会被调用一次。
7475
```javascript
7576
//es5
7677
var React = require('react');
@@ -231,6 +232,7 @@ ReactDOM.render(<Component1/>, document.getElementById('div1'));
231232
state 可以理解成 props,不一样的在于 props 是只读的,而 state 是可读写。当 state 发生改变的时候会重新执行 render 方法去渲染整颗 DOM 树,在渲染的过程中会有 diff 算法去计算哪些 DOM 有更新,从而提升性能。
232233
在使用 state 前要先初始化 `getInitialState`
233234
更改 state 一定要用 `setState`
235+
`getInitialState` 该方法在每次 render 时都会被调用一次。
234236
```javascript
235237
//es5
236238
var StateComponent = React.createClass({

component/src/lifecycle/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//https://segmentfault.com/a/1190000004168886
2+
# 生命周期
3+
React 是一个由虚拟 DOM 渲染成真实 DOM 的过程,这个过程称为组件的生命周期。React 把这个周期划分为三个阶段,每个阶段都提供了 will 和 did 两种处理方式,will 是指发生前,did 是指发生后。
4+
- Mounting:已插入真实 DOM
5+
- componentWillMount() 在组件挂载之前调用一次
6+
- componentDidMount() 在组件挂载之后调用一次。这个时候,子主键也都挂载好了,可以在这里使用refs。
7+
- Updating:正在被重新渲染
8+
- componentWillUpdate(object nextProps, object nextState) 在组件接收到新的props或者state但还没有render时被调用。在初始化时不会被调用。
9+
- componentDidUpdate(object prevProps, object prevState) 在组件完成更新后立即调用。在初始化时不会被调用。
10+
- Unmounting:已移出真实 DOM
11+
- componentWillUnmount() 在组件从 DOM 中移除的时候立刻被调用。
12+
- 这个阶段没有对应的 did 方法
13+
- componentWillReceiveProps(nextProps) 在组件接收到一个新的prop时被调用。这个方法在初始化render时不会被调用。
14+
- shouldComponentUpdate(nextProps, nextState) 组件挂载之后,每次调用setState后都会调用shouldComponentUpdate判断是否需要重新渲染组件。默认返回true,需要重新render。在比较复杂的应用里,有一些数据的改变并不影响界面展示,可以在这里做判断,优化渲染效率。
15+
16+
## React 实例化的顺序
17+
1. getDefaultProps
18+
2. getInitialState
19+
3. componentWillMount
20+
4. render
21+
5. componentDidMount
22+
23+
## componentWillMount
24+
该方法在首次渲染之前调用,也是再 render 方法调用之前。修改 state 的最后一次机会。
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>React-Component-生命周期</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<!--React 核心库-->
8+
<script src="../../../js/react.js"></script>
9+
<!--React 跟 Dom 相关的功能库-->
10+
<script src="../../../js/react-dom.js"></script>
11+
<!--jsx 转换 js 的框架 babel-->
12+
<script src="../../../js/browser.min.js"></script>
13+
</head>
14+
<body>
15+
<fieldset>
16+
<legend><h3>componentWillMount</h3></legend>
17+
<div id="div1"></div>
18+
</fieldset>
19+
20+
<script type="text/babel">
21+
var Button = React.createClass({
22+
getInitialState: function() {
23+
return {
24+
data:0
25+
};
26+
},
27+
setNewNumber: function() {
28+
//reset state
29+
//当 state 发生改变的时候,state 对应的组件会重新挂载
30+
//将 state 绑定到对应的组件
31+
this.setState({data: this.state.data + 1})
32+
},
33+
render: function () {
34+
var content;
35+
if(this.state.data % 2 == 0){
36+
content = <Content myNumber = {this.state.data}></Content>;
37+
} else {
38+
content = <h3>{this.state.data}</h3>;
39+
}
40+
return (
41+
<div>
42+
<button onClick = {this.setNewNumber}>INCREMENT</button>
43+
{content}
44+
</div>
45+
);
46+
}
47+
})
48+
49+
50+
var Content = React.createClass({
51+
getInitialState: function(){
52+
console.log('state');
53+
return {
54+
};
55+
},
56+
getDefaultProps: function(){
57+
console.log('props');
58+
return {};
59+
},
60+
componentWillMount:function() {
61+
console.log('Component WILL MOUNT!')
62+
},
63+
componentDidMount:function() {
64+
console.log('Component DID MOUNT!')
65+
},
66+
componentWillReceiveProps:function(newProps) {
67+
console.log('Component WILL RECEIVE PROPS!')
68+
},
69+
shouldComponentUpdate:function(newProps, newState) {
70+
return true;
71+
},
72+
componentWillUpdate:function(nextProps, nextState) {
73+
console.log('准备更新组件');
74+
},
75+
componentDidUpdate:function(prevProps, prevState) {
76+
console.log('组件已经更新')
77+
},
78+
componentWillUnmount:function() {
79+
console.log('Component WILL UNMOUNT!')
80+
},
81+
render: function () {
82+
console.log('render');
83+
return (
84+
<h3>{this.props.myNumber}</h3>
85+
);
86+
}
87+
});
88+
89+
ReactDOM.render(<Button />, document.getElementById('div1'));
90+
</script>
91+
</body>
92+
</html>

reactERP/api/route/Account.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ module.exports = {
2121
response.send({state: true});
2222
})
2323

24-
app.post("/register", function(request, response){})
24+
app.post("/register", function(request, response){
25+
console.log(request.body);
26+
response.send('ok');
27+
})
2528

26-
app.get('/setSession', function(request, response){
29+
app.post('/setSession', function(request, response){
2730
request.session.username = 'dk';
2831
response.send(request.session.username)
2932
})
3033

31-
app.get('/getSession', function(request, response){
34+
app.post('/getSession', function(request, response){
3235
response.send(request.session.username);
3336
})
3437

reactERP/api/route/Index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
var Account = require('./Account');
2+
var Product = require('./Product')
23

34
module.exports = {
45
Register: function(express){
56
var app = express();
6-
7+
app.all('*', function(req, res, next) {
8+
res.header("Access-Control-Allow-Origin", "*");
9+
res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
10+
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
11+
if(req.method=="OPTIONS") {
12+
res.send(200);/*让options请求快速返回*/
13+
} else{
14+
next();
15+
}
16+
});
717
Account.Register(app);
18+
Product.Register(app);
819

920
app.listen(888);
1021
}

reactERP/api/route/Product.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var bodyParser = require('body-parser');
2+
3+
var urlencodeParser = bodyParser.urlencoded();
4+
module.exports = {
5+
Register: function(app){
6+
app.get('/pro/fetchAllProducts', function(request, response){
7+
response.send({name: '娃哈哈1号 15ml', price: '10.22'});
8+
//请求数据库 根据商品条形码找到对应商品
9+
})
10+
}
11+
}

reactERP/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"node-sass": "^4.5.0",
2424
"progress-bar-webpack-plugin": "^1.9.3",
2525
"react": "^15.5.4",
26+
"react-addons-update": "^15.6.0",
2627
"react-dom": "^15.5.4",
2728
"react-redux": "^5.0.5",
2829
"react-router": "^3.0.2",

reactERP/web/src/app.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,13 @@ import './libs/font-awesome/css/font-awesome.min.css'
44
import React from 'react';
55
import ReactDOM from 'react-dom';
66
import LoginComponent from './modules/login/LoginComponent';
7-
import {Provider} from 'react-redux'
8-
import {createStore, applyMiddleware} from 'redux';
9-
import thunkMiddleware from 'redux-thunk'
10-
import LoginReducer from './modules/login/LoginReducer';
11-
import {ajaxMiddleware} from './utils/ajaxMiddleware';
12-
import { routerMiddleware } from 'react-router-redux';
13-
import {Router, Route, hashHistory, browserHistory} from 'react-router'
14-
15-
// const middleware = applyMiddleware(thunk, ajaxMiddleware);
16-
// let middleware = applyMiddleware(thunk, ajaxMiddleware, routerMiddleware(browserHistory))
17-
18-
const store = createStore(LoginReducer, {}, applyMiddleware(thunkMiddleware, ajaxMiddleware))
19-
// const store = createStore(LoginReducer);
7+
import OrderComponent from './modules/order/orderComponent';
8+
import {Provider} from 'react-redux';
9+
import store from './redux/configStore';
2010

2111
ReactDOM.render(
2212
<Provider store={store}>
23-
<LoginComponent/>
13+
<OrderComponent/>
2414
</Provider>,
2515
document.getElementById('app')
2616
);
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
import * as LoginConstants from './LoginConstants';
2+
13
export function login(username, password){
24
return {
35
types: ['a', 'b', 'c'],
4-
path: 'login',
5-
method: 'post',
6+
path: 'pro/fetchAllProducts',
7+
method: 'get',
68
query: {username, password}
79
}
10+
}
11+
12+
export function getCode(){
13+
return {
14+
types: ['getCode1', 'getCode2', 'getCode3']
15+
}
816
}

reactERP/web/src/modules/login/LoginComponent.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import React, {Component} from 'react';
2-
import loginActions from './LoginAction';
2+
import * as loginActions from './LoginAction';
33
import {connect} from 'react-redux'
44
// import {Input} from 'element-react';
55
// import 'element-theme-default';
66

77
class LoginComponent extends Component{
8+
loginHandler(){
9+
this.props.login();
10+
}
811
render(){
912
return (
1013
<div className="col-md-12">
@@ -24,14 +27,23 @@ class LoginComponent extends Component{
2427
<input type="text" placeholder="输入密码" id="form-field-1" className="form-control"/>
2528
</div>
2629
</div>
27-
<button type="button" className="btn btn-primary">登录</button>
30+
<button type="button" className="btn btn-primary" onClick={this.loginHandler.bind(this)}>登录</button>
2831
<i className="fa fa-fire"></i>
32+
<h1>{this.props.a}</h1>
2933
</div>
3034
)
3135
}
3236
}
3337

34-
const mapStateToProps = state => ({
35-
loading: false,
36-
})
38+
// const mapStateToProps = state => ({
39+
// loading: false,
40+
// })
41+
42+
const mapStateToProps = (state) => {
43+
console.log(state);
44+
return {
45+
loading: false,
46+
a: state.login.login.a
47+
}
48+
}
3749
export default connect(mapStateToProps, loginActions)(LoginComponent)

0 commit comments

Comments
 (0)