React/Redux應(yīng)用使用Async/Await的方法
Async/Await是尚未正式公布的ES7標(biāo)準(zhǔn)新特性。簡而言之,就是讓你以同步方法的思維編寫異步代碼。對于前端,異步任務(wù)代碼的編寫經(jīng)歷了 callback 到現(xiàn)在流行的 Promise ,最終會進化為 Async/Await 。雖然這個特性尚未正式發(fā)布,但是利用babel polyfill我們已經(jīng)可以在應(yīng)用中使用它了。
現(xiàn)在假設(shè)一個簡單的React/Redux應(yīng)用,我將引入 Async/Await 到其代碼。
Actions
此例子中有一個創(chuàng)建新文章的 Action ,傳統(tǒng)方法是利用 Promise 結(jié)合 Redux-thunk 中間件實現(xiàn)。
import axios from 'axios'
export default function createPost (params) {
const success = (result) => {
dispatch({
type: 'CREATE_POST_SUCCESS',
payload: result
})
return result
}
const fail = (err) => {
dispatch({
type: 'CREATE_POST_FAIL',
err
})
return err
}
return dispatch => {
return axios.post('http://xxxxx', params)
.then(success)
.catch(fail)
}
}
現(xiàn)在將它改寫為 async/await 的實現(xiàn):
import axios from 'axios'
export default function createPost (params) {
const success = (result) => {
dispatch({
type: 'CREATE_POST_SUCCESS',
payload: result
})
return result
}
const fail = (err) => {
dispatch({
type: 'CREATE_POST_FAIL',
err
})
return err
}
return async dispatch => {
try {
const result = await axios.post('http://xxxxx', params)
return success(result)
} catch (err) {
return fail(err)
}
}
}
async和await是成對使用的,特點是使代碼看起來和同步代碼類似。
Components
同樣,在React組件中,也比較一下 Promise 和 Async/Await 的方法異同。
傳統(tǒng)地使用 Promise :
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { createPost } from '../actions/post'
class PostEditForm extends Component {
constructor(props) {
super(props)
}
contributePost = e => {
e.preventDefault()
// .... get form values as params
this.props.createPost(params)
.then(response => {
// show success message
})
.catch(err => {
// show error tips
})
}
render () {
return (
<form onSubmit={this.contributePost}>
<input name="title"/>
<textarea name="content"/>
<button>Create</button>
</form>
)
}
}
export default connect(null, dispatch => {
return {
createPost: params => dispatch(createPost(params))
}
})(PostEditForm)
如果使用 Async/Await
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { createPost } from '../actions/post'
class PostEditForm extends Component {
constructor(props) {
super(props)
}
async contributePost = e => {
e.preventDefault()
// .... get form values as params
try {
const result = await this.props.createPost(params)
// show success message
} catch (err) {
// show error tips
}
}
render () {
return (
<form onSubmit={this.contributePost}>
<input name="title"/>
<textarea name="content"/>
<button>Create</button>
</form>
)
}
}
export default connect(null, dispatch => {
return {
createPost: params => dispatch(createPost(params))
}
})(PostEditForm)
可以見得,兩種模式, Async\Await 的更加直觀和簡潔,是未來的趨勢。但是目前,還需要利用babel的 transform-async-to-module-method 插件來轉(zhuǎn)換其成為瀏覽器支持的語法,雖然沒有性能的提升,但對于代碼編寫體驗要更好。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談React Native Flexbox布局(小結(jié))
這篇文章主要介紹了淺談React Native Flexbox布局(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
React Native預(yù)設(shè)占位placeholder的使用
本篇文章主要介紹了React Native預(yù)設(shè)占位placeholder的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
解決react中useState狀態(tài)異步更新的問題
本文主要介紹了react中useState狀態(tài)異步更新的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
解讀useState第二個參數(shù)的"第二個參數(shù)"
這篇文章主要介紹了useState第二個參數(shù)的"第二個參數(shù)",具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
react-native 配置@符號絕對路徑配置和絕對路徑?jīng)]有提示的問題
本文主要介紹了react-native 配置@符號絕對路徑配置和絕對路徑?jīng)]有提示的問題,文中通過圖文示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01

