React/Redux應(yīng)用使用Async/Await的方法
Async/Await是尚未正式公布的ES7標(biāo)準(zhǔn)新特性。簡而言之,就是讓你以同步方法的思維編寫異步代碼。對于前端,異步任務(wù)代碼的編寫經(jīng)歷了 callback 到現(xiàn)在流行的 Promise ,最終會(huì)進(jìn)化為 Async/Await 。雖然這個(gè)特性尚未正式發(fā)布,但是利用babel polyfill我們已經(jīng)可以在應(yīng)用中使用它了。
現(xiàn)在假設(shè)一個(gè)簡單的React/Redux應(yīng)用,我將引入 Async/Await 到其代碼。
Actions
此例子中有一個(gè)創(chuàng)建新文章的 Action ,傳統(tǒng)方法是利用 Promise 結(jié)合 Redux-thunk 中間件實(shí)現(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 的實(shí)現(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是成對使用的,特點(diǎn)是使代碼看起來和同步代碼類似。
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)換其成為瀏覽器支持的語法,雖然沒有性能的提升,但對于代碼編寫體驗(yàn)要更好。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談React Native Flexbox布局(小結(jié))
這篇文章主要介紹了淺談React Native Flexbox布局(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01React Native預(yù)設(shè)占位placeholder的使用
本篇文章主要介紹了React Native預(yù)設(shè)占位placeholder的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09解決react中useState狀態(tài)異步更新的問題
本文主要介紹了react中useState狀態(tài)異步更新的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07解讀useState第二個(gè)參數(shù)的"第二個(gè)參數(shù)"
這篇文章主要介紹了useState第二個(gè)參數(shù)的"第二個(gè)參數(shù)",具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03react創(chuàng)建項(xiàng)目啟動(dòng)報(bào)錯(cuò)的完美解決方法
這篇文章主要介紹了react創(chuàng)建項(xiàng)目啟動(dòng)報(bào)錯(cuò)的完美解決方法,全稱為Node Package Manager,是隨同NodeJS一起安裝的包管理工具,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08react-native 配置@符號絕對路徑配置和絕對路徑?jīng)]有提示的問題
本文主要介紹了react-native 配置@符號絕對路徑配置和絕對路徑?jīng)]有提示的問題,文中通過圖文示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01