八十六推薦組件的redux-thunk異步解決方案、Ajax獲取推薦數(shù)據(jù)

      網(wǎng)友投稿 701 2022-05-30

      @Author:Runsen

      今天我們來看一個(gè) Redux 官方出品的 middleware 庫:redux-thunk。

      Redux官方實(shí)現(xiàn)的異步解決方案----Redux-Thunk

      Redux-Thunk和前面寫過的Redux和React-Redux其實(shí)都是Redux官方團(tuán)隊(duì)的作品,他們的側(cè)重點(diǎn)各有不同:

      Redux:是核心庫,功能簡單,只是一個(gè)單純的狀態(tài)機(jī),但是蘊(yùn)含的思想不簡單,是傳說中的“百行代碼,千行文檔”。

      React-Redux:是跟React的連接庫,當(dāng)Redux狀態(tài)更新的時(shí)候通知React更新組件。

      Redux-Thunk:提供Redux的異步解決方案,彌補(bǔ)Redux功能的不足。

      比如,當(dāng)我聚焦的時(shí)候,推薦組件需要出現(xiàn),搜索框需要拉長。這里涉及了兩種函數(shù),需要使用redux-thunk異步。

      安裝redux-thunk:cnpm install redux-thunk

      import {createStore,compose, applyMiddleware} from 'redux' import thunk from 'redux-thunk' import reducer from './reducer' // reduxredux中的高級(jí)用法 引入redux-thunk 異步 const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; const store = createStore(reducer,composeEnhancers( applyMiddleware(thunk) )) export default store;

      1

      2

      3

      4

      5

      6

      7

      8

      9

      index.js代碼如下。

      import React ,{Component} from 'react' import { CSSTransition } from 'react-transition-group' import { connect } from 'react-redux' import {actionCreators} from './store' import { HeaderWrapper, Logo, Nav, NavItem, NavSearch, SearchWrapper, Addition, Button, SearchInfo, SearchInfoTitle, SearchInfoSwitch, SearchInfoItem, SearchInfoList, } from './style' class Header extends Component{ getListArea() { const {focused, list} = this.props if (focused) { return ( 熱門搜索 換一批 {list.map((item) => { return {item} })} ) }else{ return null } } render() { const {focused, handleInputFocus,handleInputBlur} = this.props return (

      ) } } const mapStateToProps = (state) => { return { // state.getIn是immutable fromJS的用法 相當(dāng)于 // state.get('header').get('focused') focused: state.getIn(['header','focused']), list: state.getIn(['header','list']) } } const mapDispathToProps = (dispatch) => { return { handleInputFocus() { dispatch(actionCreators.getList()); dispatch(actionCreators.searchFocus()); }, handleInputBlur() { dispatch(actionCreators.searchBlur()); } } } export default connect(mapStateToProps, mapDispathToProps)(Header);

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      八十六、推薦組件的redux-thunk異步解決方案、Ajax獲取推薦數(shù)據(jù)

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      69

      70

      71

      72

      73

      74

      75

      76

      77

      78

      79

      80

      81

      82

      83

      84

      85

      86

      87

      88

      89

      90

      91

      92

      93

      94

      95

      96

      97

      98

      99

      100

      101

      102

      103

      104

      105

      106

      107

      108

      109

      Ajax獲取推薦數(shù)據(jù)

      在actionCreators中有一個(gè)getList,來獲取推薦數(shù)據(jù)。我們需要使用Ajax獲取推薦數(shù)據(jù)。

      React 只是使用 props 和 state 兩處的數(shù)據(jù)進(jìn)行組件渲染。

      個(gè)人推薦 axios,這也是本文所使用的。不過,認(rèn)真的說,如果你偏偏不喜歡它,那就選其他的唄,比如Promise。

      安裝:cnpm install axios --save

      import * as constants from './constants' import axios from 'axios' import {fromJS} from 'immutable' export const searchFocus = () =>({ type: constants.SERACH_FOCUS }) export const searchBlur = () =>({ type: constants.SERACH_BLUR }) const changList = (data) => ({ type: constants.CHANGR_LIST, // fromJS處理 data: fromJS(data) }) export const getList = () =>{ return (dispatch) => { axios.get('/api/headerList.json').then((res) => { const data = res.data dispatch(changList(data.data)) }).catch(()=>{ console.log('error') }) } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      import * as constants from './constants' import {fromJS} from 'immutable' const defaultState = fromJS({ focused: false, list: [] }); // eslint-disable-next-line import/no-anonymous-default-export export default (state = defaultState, action) => { // eslint-disable-next-line default-case switch(action.type) { case constants.SERACH_FOCUS : return state.set('focused',true); case constants.SERACH_BLUR : return state.set('focused',false); case constants.CHANGR_LIST : return state.set('list',action.data) } return state; }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      Ajax Redux

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:文本文件和二進(jìn)制文件的判別
      下一篇:Pandas完美讀取html格式的Excel所有隱藏?cái)?shù)據(jù)
      相關(guān)文章
      亚洲AV成人精品日韩一区18p| 久久久久亚洲AV成人网| 伊人久久亚洲综合| 亚洲色偷偷综合亚洲AVYP| 亚洲欧美在线x视频| 亚洲麻豆精品果冻传媒| 久久久久亚洲AV成人无码网站| 免费亚洲视频在线观看| 国产亚洲精品AAAA片APP| 亚洲色丰满少妇高潮18p| 日韩亚洲产在线观看| 91大神亚洲影视在线| 亚洲宅男永久在线| 亚洲理论电影在线观看| 亚洲免费观看视频| 亚洲国产精品无码久久久不卡| 亚洲日韩精品无码一区二区三区| 国产成人麻豆亚洲综合无码精品| 亚洲综合国产一区二区三区| 亚洲人精品午夜射精日韩| 久久亚洲综合色一区二区三区| 国产精品国产亚洲精品看不卡| 亚洲国产精品自在线一区二区| 亚洲一本大道无码av天堂| 亚洲欧洲专线一区| 亚洲AV无码片一区二区三区| 国产精品亚洲专区无码牛牛| 亚洲?V无码成人精品区日韩| 亚洲精品成人久久久| 亚洲综合伊人久久综合| 国产亚洲精久久久久久无码| 亚洲国产精品免费视频| 亚洲精品永久www忘忧草| 亚洲制服丝袜第一页| 亚洲人成网站999久久久综合| 欧美日韩亚洲精品| 国产精品亚洲精品日韩已方| 亚洲精品无码午夜福利中文字幕| 久久精品国产亚洲香蕉| 亚洲熟妇无码久久精品| 亚洲欧美国产欧美色欲|