• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答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
    主站蜘蛛池模板: 日韩欧美精品不卡| 国产精品国产AV片国产| 老司机国内精品久久久久| 中文字幕精品一区二区三区视频| 久久精品国产91久久综合麻豆自制| 最新国产成人精品2024| 精品伦精品一区二区三区视频| 欧美精品第一页| 99精品视频在线观看| 亚洲av午夜成人片精品网站| 久久久不卡国产精品一区二区| 91大神精品全国在线观看| 精品福利一区二区三区免费视频| 网友偷拍日韩精品| 精品国产AⅤ一区二区三区4区| 久久久九九有精品国产| 成人国产精品999视频| 久久久久国产精品熟女影院 | 精品久久久噜噜噜久久久 | 欧美一区二区精品| 国产精品免费无遮挡无码永久视频 | 欧美黑人巨大精品| 999精品在线| 国产精品无码专区| 国产欧美精品一区二区三区| 欧洲精品99毛片免费高清观看| 亚洲国产精品不卡毛片a在线| 欧美在线精品永久免费播放| 久久久久人妻一区精品果冻| 久久er国产精品免费观看8| 精品无码三级在线观看视频| 黑巨人与欧美精品一区| 国产欧美精品一区二区三区四区| 91精品无码久久久久久五月天| 国产精品欧美亚洲韩国日本不卡| 99久久这里只有精品| 国产精品久久免费| 真实国产乱子伦精品免费| 99久久99久久精品国产片| 国产精品 视频一区 二区三区| 国产精品热久久毛片|