File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ import request from 'superagent'
2
+ import { rootPath , errHandler } from './config'
3
+
4
+ const xhr = ( { url, body = null , method = 'get' } ) => {
5
+ // P.S: 此处引入了ES6的Promise实现
6
+ return new Promise ( ( resolve , reject ) => {
7
+ request [ method . toLowerCase ( ) ] ( rootPath + url )
8
+ . send ( body )
9
+ // .withCredentials()
10
+ . end ( ( err , re ) => {
11
+ if ( err )
12
+ return errHandler ( err )
13
+
14
+ if ( ! re . body )
15
+ return resolve ( null )
16
+
17
+ if ( re . body . _code )
18
+ return errHandler ( re . body . _msg )
19
+
20
+ resolve ( re . body )
21
+ } )
22
+ } )
23
+ }
24
+
25
+ export default xhr
Original file line number Diff line number Diff line change
1
+ import React , { Component } from 'react'
2
+
3
+ /* 高阶组件 High Order Component 例子 */
4
+ const ExampleHoC = WrappedComponent => class extends Component {
5
+ componentWillMount ( ) {
6
+ console . info ( '[HoC] componentWillMount' )
7
+ }
8
+
9
+ componentDidMount ( ) {
10
+ console . info ( '[HoC] componentDidMount' )
11
+ }
12
+
13
+ render ( ) {
14
+ return < WrappedComponent { ...this . props } />
15
+ }
16
+ }
17
+
18
+ export default ExampleHoC
19
+
20
+ /**
21
+ *【拓展】
22
+ * React Router 2.4 新增 withRouter 这个 HoC
23
+ * 这是除 context 外另一种获取 router 的推荐方式
24
+ * https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.4.0.md
25
+ *
26
+ * 本项目中的 Redirect 组件用到了该高阶组件
27
+ */
You can’t perform that action at this time.
0 commit comments