做爰高潮a片〈毛片〉,尤物av天堂一区二区在线观看,一本久久A久久精品VR综合,添女人荫蒂全部过程av

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

React/Redux應用使用Async/Await的方法

來源:懂視網 責編:小采 時間:2020-11-27 22:25:41
文檔

React/Redux應用使用Async/Await的方法

React/Redux應用使用Async/Await的方法:Async/Await是尚未正式公布的ES7標準新特性。簡而言之,就是讓你以同步方法的思維編寫異步代碼。對于前端,異步任務代碼的編寫經歷了 callback 到現在流行的 Promise ,最終會進化為 Async/Await 。雖然這個特性尚未正式發布,但是利用babel polyf
推薦度:
導讀React/Redux應用使用Async/Await的方法:Async/Await是尚未正式公布的ES7標準新特性。簡而言之,就是讓你以同步方法的思維編寫異步代碼。對于前端,異步任務代碼的編寫經歷了 callback 到現在流行的 Promise ,最終會進化為 Async/Await 。雖然這個特性尚未正式發布,但是利用babel polyf

Async/Await是尚未正式公布的ES7標準新特性。簡而言之,就是讓你以同步方法的思維編寫異步代碼。對于前端,異步任務代碼的編寫經歷了 callback 到現在流行的 Promise ,最終會進化為 Async/Await 。雖然這個特性尚未正式發布,但是利用babel polyfill我們已經可以在應用中使用它了。

現在假設一個簡單的React/Redux應用,我將引入 Async/Await 到其代碼。

Actions

此例子中有一個創建新文章的 Action ,傳統方法是利用 Promise 結合 Redux-thunk 中間件實現。

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)
 }
}

現在將它改寫為 async/await 的實現:

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 的方法異同。

傳統地使用 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 插件來轉換其成為瀏覽器支持的語法,雖然沒有性能的提升,但對于代碼編寫體驗要更好。

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

React/Redux應用使用Async/Await的方法

React/Redux應用使用Async/Await的方法:Async/Await是尚未正式公布的ES7標準新特性。簡而言之,就是讓你以同步方法的思維編寫異步代碼。對于前端,異步任務代碼的編寫經歷了 callback 到現在流行的 Promise ,最終會進化為 Async/Await 。雖然這個特性尚未正式發布,但是利用babel polyf
推薦度:
標簽: 使用的 async React
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 闸北区| 贡嘎县| 永康市| 邢台市| 青铜峡市| 札达县| 托克托县| 洛南县| 张家界市| 务川| 澎湖县| 新沂市| 巍山| 梁河县| 泰州市| 香港 | 从化市| 白山市| 鄂托克前旗| 梓潼县| 紫金县| 甘肃省| 永嘉县| 白银市| 东方市| 桑日县| 偏关县| 宝坻区| 大安市| 镶黄旗| 平度市| 浦北县| 舞阳县| 怀远县| 海伦市| 深水埗区| 城市| 贵溪市| 山阴县| 绥阳县| 长沙县|