目錄
JavaScript 語言
變量聲明
const 和 let
不要用 var,而是用 const 和 let,分別表示常量和變量。不同于 var 的函數(shù)作用域,const 和 let 都是塊級作用域。
const DELAY = 1000;
let count = 0;
count = count + 1;
模板字符串
模板字符串提供了另一種做字符串組合的方法。
const user = 'world';
console.log(`hello ${user}`); // hello world
// 多行
const content = `
Hello ${firstName},
Thanks for ordering ${qty} tickets to ${event}.
`;
默認(rèn)參數(shù)
function logActivity(activity = 'skiing') {
console.log(activity);
}
logActivity(); // skiing
箭頭函數(shù)
函數(shù)的快捷寫法,不需要通過 function 關(guān)鍵字創(chuàng)建函數(shù),并且還可以省略 return 關(guān)鍵字。
同時(shí),箭頭函數(shù)還會繼承當(dāng)前上下文的 this 關(guān)鍵字。
比如:
[1, 2, 3].map(x => x + 1); // [2, 3, 4]
等同于:
[1, 2, 3].map((function(x) {
return x + 1;
}).bind(this));
模塊的 Import 和 Export
import 用于引入模塊,export 用于導(dǎo)出模塊。
比如:
// 引入全部
import dva from 'dva';
// 引入部分
import { connect } from 'dva';
import { Link, Route } from 'dva/router';
// 引入全部并作為 github 對象
import * as github from './services/github';
// 導(dǎo)出默認(rèn)
export default App;
// 部分導(dǎo)出,需 import { App } from './file'; 引入
export class App extend Component {};
ES6 對象和數(shù)組
析構(gòu)賦值
析構(gòu)賦值讓我們從 Object 或 Array 里取部分?jǐn)?shù)據(jù)存為變量。
// 對象
const user = { name: 'guanguan', age: 2 };
const { name, age } = user;
console.log(`${name} : ${age}`); // guanguan : 2
// 數(shù)組
const arr = [1, 2];
const [foo, bar] = arr;
console.log(foo); // 1
我們也可以析構(gòu)傳入的函數(shù)參數(shù)。
const add = (state, { payload }) => {
return state.concat(payload);
};
析構(gòu)時(shí)還可以配 alias,讓代碼更具有語義。
const add = (state, { payload: todo }) => {
return state.concat(todo);
};
對象字面量改進(jìn)
這是析構(gòu)的反向操作,用于重新組織一個(gè) Object 。
const name = 'duoduo';
const age = 8;
const user = { name, age }; // { name: 'duoduo', age: 8 }
定義對象方法時(shí),還可以省去 function 關(guān)鍵字。
app.model({
reducers: {
add() {} // 等同于 add: function() {}
},
effects: {
*addRemote() {} // 等同于 addRemote: function*() {}
},
});
Spread Operator
Spread Operator 即 3 個(gè)點(diǎn) ...,有幾種不同的使用方法。
可用于組裝數(shù)組。
const todos = ['Learn dva'];
[...todos, 'Learn antd']; // ['Learn dva', 'Learn antd']
也可用于獲取數(shù)組的部分項(xiàng)。
const arr = ['a', 'b', 'c'];
const [first, ...rest] = arr;
rest; // ['b', 'c']
// With ignore
const [first, , ...rest] = arr;
rest; // ['c']
還可收集函數(shù)參數(shù)為數(shù)組。
function directions(first, ...rest) {
console.log(rest);
}
directions('a', 'b', 'c'); // ['b', 'c'];
代替 apply。
function foo(x, y, z) {}
const args = [1,2,3];
// 下面兩句效果相同
foo.apply(null, args);
foo(...args);
對于 Object 而言,用于組合成新的 Object 。(ES2017 stage-2 proposal)
const foo = {
a: 1,
b: 2,
};
const bar = {
b: 3,
c: 2,
};
const d = 4;
const ret = { ...foo, ...bar, d }; // { a:1, b:3, c:2, d:4 }
此外,在 JSX 中 Spread Operator 還可用于擴(kuò)展 props,詳見 Spread Attributes。
Promises
Promise 用于更優(yōu)雅地處理異步請求。比如發(fā)起異步請求:
fetch('/api/todos')
.then(res => res.json())
.then(data => ({ data }))
.catch(err => ({ err }));
定義 Promise 。
const delay = (timeout) => {
return new Promise(resolve => {
setTimeout(resolve, timeout);
});
};
delay(1000).then(_ => {
console.log('executed');
});
Generators
dva 的 effects 是通過 generator 組織的。Generator 返回的是迭代器,通過 yield 關(guān)鍵字實(shí)現(xiàn)暫停功能。
這是一個(gè)典型的 dva effect,通過 yield 把異步邏輯通過同步的方式組織起來。
app.model({
namespace: 'todos',
effects: {
*addRemote({ payload: todo }, { put, call }) {
yield call(addTodo, todo);
yield put({ type: 'add', payload: todo });
},
},
});
React Component
Stateless Functional Components
React Component 有 3 種定義方式,分別是 React.createClass, class 和 Stateless Functional Component。推薦盡量使用最后一種,保持簡潔和無狀態(tài)。這是函數(shù),不是 Object,沒有 this 作用域,是 pure function。
比如定義 App Component 。
function App(props) {
function handleClick() {
props.dispatch({ type: 'app/create' });
}
return <div onClick={handleClick}>${props.name}</div>
}
等同于:
class App extends React.Component {
handleClick() {
this.props.dispatch({ type: 'app/create' });
}
render() {
return <div onClick={this.handleClick.bind(this)}>${this.props.name}</div>
}
}
JSX
Component 嵌套
類似 HTML,JSX 里可以給組件添加子組件。
<App>
<Header />
<MainContent />
<Footer />
</App>
className
class 是保留詞,所以添加樣式時(shí),需用 className 代替 class 。
<h1 className="fancy">Hello dva</h1>
JavaScript 表達(dá)式
JavaScript 表達(dá)式需要用 {} 括起來,會執(zhí)行并返回結(jié)果。
比如:
<h1>{ this.props.title }</h1>
Mapping Arrays to JSX
可以把數(shù)組映射為 JSX 元素列表。
<ul>
{ this.props.todos.map((todo, i) => <li key={i}>{todo}</li>) }
</ul>
注釋
盡量別用 // 做單行注釋。
<h1>
{/* multiline comment */}
{/*
multi
line
comment
*/}
{
// single line
}
Hello
</h1>
Spread Attributes
這是 JSX 從 ECMAScript6 借鑒過來的很有用的特性,用于擴(kuò)充組件 props 。
比如:
const attrs = {
href: 'http://example.org',
target: '_blank',
};
<a {...attrs}>Hello</a>
等同于
const attrs = {
href: 'http://example.org',
target: '_blank',
};
<a href={attrs.href} target={attrs.target}>Hello</a>
Props
數(shù)據(jù)處理在 React 中是非常重要的概念之一,分別可以通過 props, state 和 context 來處理數(shù)據(jù)。而在 dva 應(yīng)用里,你只需關(guān)心 props 。
propTypes
JavaScript 是弱類型語言,所以請盡量聲明 propTypes 對 props 進(jìn)行校驗(yàn),以減少不必要的問題。
function App(props) {
return <div>{props.name}</div>;
}
App.propTypes = {
name: React.PropTypes.string.isRequired,
};
內(nèi)置的 prop type 有:
- PropTypes.array
- PropTypes.bool
- PropTypes.func
- PropTypes.number
- PropTypes.object
- PropTypes.string
往下傳數(shù)據(jù)
往上傳數(shù)據(jù)
CSS Modules
理解 CSS Modules
一張圖理解 CSS Modules 的工作原理:
button class 在構(gòu)建之后會被重命名為 ProductList_button_1FU0u 。button 是 local name,而 ProductList_button_1FU0u是 global name 。你可以用簡短的描述性名字,而不需要關(guān)心命名沖突問題。
然后你要做的全部事情就是在 css/less 文件里寫 .button {...},并在組件里通過 styles.button 來引用他。
定義全局 CSS
CSS Modules 默認(rèn)是局部作用域的,想要聲明一個(gè)全局規(guī)則,可用 :global 語法。
比如:
.title {
color: red;
}
:global(.title) {
color: green;
}
然后在引用的時(shí)候:
<App className={styles.title} /> // red
<App className="title" /> // green
classnames Package
在一些復(fù)雜的場景中,一個(gè)元素可能對應(yīng)多個(gè) className,而每個(gè) className 又基于一些條件來決定是否出現(xiàn)。這時(shí),classnames 這個(gè)庫就非常有用。
import classnames from 'classnames';
const App = (props) => {
const cls = classnames({
btn: true,
btnLarge: props.type === 'submit',
btnSmall: props.type === 'edit',
});
return <div className={ cls } />;
}
這樣,傳入不同的 type 給 App 組件,就會返回不同的 className 組合:
<App type="submit" /> // btn btnLarge
<App type="edit" /> // btn btnSmall
Reducer
reducer 是一個(gè)函數(shù),接受 state 和 action,返回老的或新的 state 。即:(state, action) => state
增刪改
以 todos 為例。
app.model({
namespace: 'todos',
state: [],
reducers: {
add(state, { payload: todo }) {
return state.concat(todo);
},
remove(state, { payload: id }) {
return state.filter(todo => todo.id !== id);
},
update(state, { payload: updatedTodo }) {
return state.map(todo => {
if (todo.id === updatedTodo.id) {
return { ...todo, ...updatedTodo };
} else {
return todo;
}
});
},
},
};
嵌套數(shù)據(jù)的增刪改
建議最多一層嵌套,以保持 state 的扁平化,深層嵌套會讓 reducer 很難寫和難以維護(hù)。
app.model({
namespace: 'app',
state: {
todos: [],
loading: false,
},
reducers: {
add(state, { payload: todo }) {
const todos = state.todos.concat(todo);
return { ...state, todos };
},
},
});
下面是深層嵌套的例子,應(yīng)盡量避免。
app.model({
namespace: 'app',
state: {
a: {
b: {
todos: [],
loading: false,
},
},
},
reducers: {
add(state, { payload: todo }) {
const todos = state.a.b.todos.concat(todo);
const b = { ...state.a.b, todos };
const a = { ...state.a, b };
return { ...state, a };
},
},
});
Effect
示例:
app.model({
namespace: 'todos',
effects: {
*addRemote({ payload: todo }, { put, call }) {
yield call(addTodo, todo);
yield put({ type: 'add', payload: todo });
},
},
});
Effects
put
用于觸發(fā) action 。
yield put({ type: 'todos/add', payload: 'Learn Dva' });
call
用于調(diào)用異步邏輯,支持 promise 。
const result = yield call(fetch, '/todos');
select
用于從 state 里獲取數(shù)據(jù)。
const todos = yield select(state => state.todos);
錯(cuò)誤處理
全局錯(cuò)誤處理
dva 里,effects 和 subscriptions 的拋錯(cuò)全部會走 onError hook,所以可以在 onError 里統(tǒng)一處理錯(cuò)誤。
const app = dva({
onError(e, dispatch) {
console.log(e.message);
},
});
然后 effects 里的拋錯(cuò)和 reject 的 promise 就都會被捕獲到了。
本地錯(cuò)誤處理
如果需要對某些 effects 的錯(cuò)誤進(jìn)行特殊處理,需要在 effect 內(nèi)部加 try catch 。
app.model({
effects: {
*addRemote() {
try {
// Your Code Here
} catch(e) {
console.log(e.message);
}
},
},
});
異步請求
異步請求基于 whatwg-fetch,API 詳見:https://github.com/github/fetch
GET 和 POST
import request from '../util/request';
// GET
request('/api/todos');
// POST
request('/api/todos', {
method: 'POST',
body: JSON.stringify({ a: 1 }),
});
統(tǒng)一錯(cuò)誤處理
假如約定后臺返回以下格式時(shí),做統(tǒng)一的錯(cuò)誤處理。
{
status: 'error',
message: '',
}
編輯 utils/request.js,加入以下中間件:
function parseErrorMessage({ data }) {
const { status, message } = data;
if (status === 'error') {
throw new Error(message);
}
return { data };
}
然后,這類錯(cuò)誤就會走到 onError hook 里。
Subscription
subscriptions 是訂閱,用于訂閱一個(gè)數(shù)據(jù)源,然后根據(jù)需要 dispatch 相應(yīng)的 action。數(shù)據(jù)源可以是當(dāng)前的時(shí)間、服務(wù)器的 websocket 連接、keyboard 輸入、geolocation 變化、history 路由變化等等。格式為 ({ dispatch, history }) => unsubscribe 。
異步數(shù)據(jù)初始化
比如:當(dāng)用戶進(jìn)入 /users 頁面時(shí),觸發(fā) action users/fetch 加載用戶數(shù)據(jù)。
app.model({
subscriptions: {
setup({ dispatch, history }) {
history.listen(({ pathname }) => {
if (pathname === '/users') {
dispatch({
type: 'users/fetch',
});
}
});
},
},
});
path-to-regexp Package
如果 url 規(guī)則比較復(fù)雜,比如 /users/:userId/search,那么匹配和 userId 的獲取都會比較麻煩。這是推薦用 path-to-regexp簡化這部分邏輯。
import pathToRegexp from 'path-to-regexp';
// in subscription
const match = pathToRegexp('/users/:userId/search').exec(pathname);
if (match) {
const userId = match[1];
// dispatch action with userId
}
Router
Config with JSX Element (router.js)
<Route path="/" component={App}>
<Route path="accounts" component={Accounts}/>
<Route path="statements" component={Statements}/>
</Route>
詳見:react-router
Route Components
Route Components 是指 ./src/routes/ 目錄下的文件,他們是 ./src/router.js 里匹配的 Component。
通過 connect 綁定數(shù)據(jù)
比如:
import { connect } from 'dva';
function App() {}
function mapStateToProps(state, ownProps) {
return {
users: state.users,
};
}
export default connect(mapStateToProps)(App);
然后在 App 里就有了 dispatch 和 users 兩個(gè)屬性。
Injected Props (e.g. location)
Route Component 會有額外的 props 用以獲取路由信息。
- location
- params
- children
更多詳見:react-router
基于 action 進(jìn)行頁面跳轉(zhuǎn)
import { routerRedux } from 'dva/router';
// Inside Effects
yield put(routerRedux.push('/logout'));
// Outside Effects
dispatch(routerRedux.push('/logout'));
// With query
routerRedux.push({
pathname: '/logout',
query: {
page: 2,
},
});
除 push(location) 外還有更多方法,詳見 react-router-redux
dva 配置
Redux Middleware
比如要添加 redux-logger 中間件:
import createLogger from 'redux-logger';
const app = dva({
onAction: createLogger(),
});
注:onAction 支持?jǐn)?shù)組,可同時(shí)傳入多個(gè)中間件。
history
切換 history 為 browserHistory
import { browserHistory } from 'dva/router';
const app = dva({
history: browserHistory,
});
去除 hashHistory 下的 _k 查詢參數(shù)
import { useRouterHistory } from 'dva/router';
import { createHashHistory } from 'history';
const app = dva({
history: useRouterHistory(createHashHistory)({ queryKey: false }),
});
工具
通過 dva-cli 創(chuàng)建項(xiàng)目
先安裝 dva-cli 。
$ npm install dva-cli -g
然后創(chuàng)建項(xiàng)目。
$ dva new myapp
最后,進(jìn)入目錄并啟動(dòng)。
$ cd myapp
$ npm start
Kotlin 開發(fā)者社區(qū)
國內(nèi)第一Kotlin 開發(fā)者社區(qū)公眾號,主要分享、交流 Kotlin 編程語言、Spring Boot、Android、React.js/Node.js、函數(shù)式編程、編程思想等相關(guān)主題。
