成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Redux總結(jié)

Drinkey / 1409人閱讀

做為一個前端小白,在自學(xué)Redux過程中個人認(rèn)為首先需要明白Action、Store、reducer(state,action)、Component中間的關(guān)系,簡單的可以理解為圖書館借書的過程,用戶提出借什么書的請求給管理員,管理員去reducer里查找有沒有你需要的書,reducer會返回一個state數(shù)據(jù)告訴管理員有沒有這本書,用戶通過store.getState()方法得到管理員從reducer得到的數(shù)據(jù)。

簡介:以TodoList為例由淺入深的學(xué)習(xí)redux。
一、創(chuàng)建store、reducer,引入到文件后并調(diào)用store里數(shù)據(jù)

1、創(chuàng)建store:使用redux的createStore()方法創(chuàng)建,導(dǎo)出store

// 創(chuàng)建store
import {createStore} from "redux"
// 引入reducer
import reducer from 路徑

const store=createStore(reducer);

export default store;

3、創(chuàng)建reducer數(shù)據(jù):直接返回函數(shù),默認(rèn)返回參數(shù)State

//創(chuàng)建reducer
const defaultState={}
export default (state=defaultState,action) => {
    return state;
}

4、將store文件引入到目錄下,使用stroe.getState()方法獲取reducer里的數(shù)據(jù)

5、聲明action并使用store.dispatch(action)方法將action傳遞給store,reducer里接收store自行傳過來的action與state數(shù)據(jù)并返回一個新的數(shù)據(jù),

 // 組件訂閱store
store.subscribe(被訂閱者),實(shí)現(xiàn)聯(lián)動效果

hadChange(e){
  // 聲明action
  const action={
    type:"change_input_value";
    value:e.taget.value
  }
  // 將action傳遞給store
  store.dispatch(action)
}

// state只能接收數(shù)據(jù)不能操作數(shù)據(jù),需要將數(shù)據(jù)進(jìn)行深拷貝
if(action.type === "change_input_value"){
  const newState=JSON.parse(JSON.stringify(state));
  newState.value=action.value;
  return newState;
}

//store訂閱一個更新函數(shù),待dispatch之后,執(zhí)行這個更新函數(shù),獲取新的值
store.subScribe(this.hadChangeValue.bind(this))
hadChangeValue(){
  this.setState(store.getState())
}

6、actionTyps.js是將所有action以變量的形式存到j(luò)s文件里,方便后續(xù)因拼寫出錯導(dǎo)致查找報(bào)錯原因,代碼如下:

export const CHANGE_INPUT_VALUE ="change_input_value";
export const ADD_TODO_ITEM ="add_todo_item";
export const DELE_TODO_ITEM ="dele_todo_item";

二、詳細(xì)代碼如下:

1、創(chuàng)建Antds模塊

import React, { Component,Fragment } from "react";
//引入antd庫
import { Input,Button, List} from "antd";
import store from "../store/index.js"
import {CHANGE_INPUT_VALUE ,ADD_TODO_ITEM,DELE_TODO_ITEM} from "../store/actionTyps"

class Antds extends Component {
  constructor(props){
    super(props);
    this.state=store.getState();
    this.hadChange=this.hadChange.bind(this);
    this.changeValue=this.changeValue.bind(this);
    this.hadClick=this.hadClick.bind(this);
    //訂閱store
    store.subscribe(this.changeValue)
  }

  hadChange(e){
    //聲明一個action,它是一個函數(shù)
    const action={
      type:CHANGE_INPUT_VALUE,
      value:e.target.value
    }
    // 將action傳給store,使用store提共的dispatch(action)方法
    store.dispatch(action)
  }
  // 點(diǎn)擊按鈕聲明action
  hadClick(){
    const action ={
      type:ADD_TODO_ITEM,
    }
    // 將action傳遞給store
    store.dispatch(action)
  }
  changeValue(){
    // 感知到stoe發(fā)生變化后調(diào)用store.getState()
    this.setState(store.getState())
  }
  hadClickItem(index){
    const action ={
      type:DELE_TODO_ITEM,
      index
    }
    // 將action傳遞給store
    store.dispatch(action)
  }
  render() {
    return (
      
        
( {item} )} />
); } } export default Antds;

2、創(chuàng)建store

// 利用redux里的createStore()方法創(chuàng)建store
import {createStore} from "redux"
// reducers里存放所有數(shù)據(jù)
import reducers from "./reducers"
const store=createStore(reducers);

// 導(dǎo)出store
export default store;

3、創(chuàng)建reducer

import {CHANGE_INPUT_VALUE ,ADD_TODO_ITEM,DELE_TODO_ITEM} from "./actionTyps"
const defaultState={
    inputValue:"",
      lis:[],
}

export default (state=defaultState,action)=>{
    if(action.type===CHANGE_INPUT_VALUE ){
        // 深拷貝
        const newState=JSON.parse(JSON.stringify(state));
        newState.inputValue=action.value;
        return newState;
    }
    if(action.type === ADD_TODO_ITEM){
        // 深拷貝
        const newState=JSON.parse(JSON.stringify(state));
        newState.lis.push(newState.inputValue);
        newState.inputValue="";
        return newState;
    }
    if(action.type === DELE_TODO_ITEM){
        // 深拷貝
        const newState=JSON.parse(JSON.stringify(state));
        newState.lis.splice(action.index)
        return newState;
    }
    return state;
}

4、將所有action以變量形式存到文件中

export const CHANGE_INPUT_VALUE ="change_input_value";
export const ADD_TODO_ITEM ="add_todo_item";
export const DELE_TODO_ITEM ="dele_todo_item";

-----------------持續(xù)更新中-------------------

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/109628.html

相關(guān)文章

  • redux和react-redux的理解和總結(jié)(一)

    摘要:使得在變化和異步中可預(yù)測。它是數(shù)據(jù)的唯一來源。指定了應(yīng)用狀態(tài)的變化如何響應(yīng)并發(fā)送到的,只是描述了有事情發(fā)生了這一事實(shí),并沒有描述應(yīng)用如何更新。更新的函數(shù)稱為,它是一個純函數(shù),接受舊的和,返回新的。是和之間的橋梁,是把它們聯(lián)系到一起的對象。 為什么使用redux 隨著前端單頁面開發(fā)越來越復(fù)雜,javascript需要管理越來越多的狀態(tài)state。如果一個model的變化引起另一個mode...

    skinner 評論0 收藏0
  • 單頁應(yīng)用開發(fā)總結(jié)

    摘要:本文想通過自己這一年的單頁應(yīng)用開發(fā)經(jīng)驗(yàn),來對的開發(fā)做一個總結(jié)。但是要知道,現(xiàn)如今頁面都比較復(fù)雜,一般的單頁應(yīng)用都需要一個可靠的數(shù)據(jù)流去處理,否則在日后維護(hù)方面會難度巨大。 本文想通過自己這一年的單頁應(yīng)用開發(fā)經(jīng)驗(yàn),來對SPA的開發(fā)做一個總結(jié)。 頁面開發(fā)模式 通常我們在開發(fā)頁面時,都會拿到一份設(shè)計(jì)圖,假設(shè)我們拿到一份這樣的設(shè)計(jì)圖 showImg(https://segmentfault.c...

    zzbo 評論0 收藏0
  • 一篇文章總結(jié)redux、react-redux、redux-saga

    摘要:編輯器頂層組件不就了嗎這就是。官方提供的綁定庫。具有高效且靈活的特性。在的中,可以使用或者等來監(jiān)聽某個,當(dāng)某個觸發(fā)后,可以使用發(fā)起異步操作,操作完成后使用函數(shù)觸發(fā),同步更新,從而完成整個的更新。 不就ok了嗎?這就是 react-redux。Redux 官方提供的 React 綁定庫。 具有高效且靈活的特性。 React Redux 將組件區(qū)分為 容器組件 和 UI 組件 前者會處理邏輯...

    April 評論0 收藏0
  • Redux 的簡單總結(jié)

    摘要:目前,官方?jīng)]有提供監(jiān)控部分改變的方法。這個函數(shù)執(zhí)行后,在中被提及的成員會被替換。這個函數(shù)與相比,唯一的好處是假如組件定義不在入口文件如中,這種方法可以免于入口文件中的全局。 Redux https://redux.js.org/https://cn.redux.js.org/ store.getState() https://redux.js.org/api-refe... 這個函數(shù)返...

    Elle 評論0 收藏0
  • React組件設(shè)計(jì)實(shí)踐總結(jié)05 - 狀態(tài)管理

    摘要:要求通過要求數(shù)據(jù)變更函數(shù)使用裝飾或放在函數(shù)中,目的就是讓狀態(tài)的變更根據(jù)可預(yù)測性單向數(shù)據(jù)流。同一份數(shù)據(jù)需要響應(yīng)到多個視圖,且被多個視圖進(jìn)行變更需要維護(hù)全局狀態(tài),并在他們變動時響應(yīng)到視圖數(shù)據(jù)流變得復(fù)雜,組件本身已經(jīng)無法駕馭。今天是 520,這是本系列最后一篇文章,主要涵蓋 React 狀態(tài)管理的相關(guān)方案。 前幾篇文章在掘金首發(fā)基本石沉大海, 沒什么閱讀量. 可能是文章篇幅太長了?掘金值太低了? ...

    ideaa 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<