欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Redux實(shí)現(xiàn)組合計(jì)數(shù)器的示例代碼

 更新時間:2018年07月04日 10:44:15   作者:木子昭  
本篇文章主要介紹了Redux實(shí)現(xiàn)組合計(jì)數(shù)器的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Redux是一種解決數(shù)據(jù)共享的方案

import {createStore} from 'redux';
import React from 'react';
import ReactDOM from 'react-dom';
import {connect, createProvider} from 'react-redux'


// data
let allNum = {num :1000}

// 創(chuàng)建reducer, 名字的默認(rèn)值為
function reducer(state, action) {
  let tmp = {}
  if (action.type == "decrease"){
    allNum.num = allNum.num - action.value;
    tmp = Object.assign({}, state, {num: allNum.num})
    return tmp
  }else if(action.type == "increase"){
    allNum.num = allNum.num + action.value;
    tmp = Object.assign({}, state, {num: allNum.num})
    return tmp
  }else{
    return state
  }
}

// 創(chuàng)建store存儲數(shù)據(jù)(傳入處理函數(shù)reducer, 核心數(shù)據(jù)allNum)
let store = createStore(reducer, allNum)
console.log("初始化的數(shù)據(jù)為",store.getState('num'))

// 添加監(jiān)聽函數(shù)
store.subscribe(() => {console.log("監(jiān)聽函數(shù)發(fā)出:", store.getState())});

// 發(fā)出action
let tmp = {};
tmp = store.dispatch({type: "decrease", value: 10})
console.log("---->", tmp);
tmp = store.dispatch({type: "decrease", value: 100})
console.log("---->", tmp);
tmp = store.dispatch({type: "increase", value: 31})
console.log("---->", tmp);
tmp = store.dispatch({type: "increase", value: 123})
console.log("---->", tmp);

class MyComponent extends React.Component {
 render() {return <div>Hello World</div>;}
}

ReactDOM.render(<MyComponent />, document.getElementById("root"));

React和Redux組合使用

React組件, 有兩個數(shù)據(jù)集, props和state

props表示外部傳入組件的參數(shù)(數(shù)據(jù)由外部傳入, 可以被外部更改)

state表示組件固有的屬性(數(shù)據(jù)私有, 不可以被外部更改)

我們可以把多個React組件的props交由Redux進(jìn)行管理, 這樣就實(shí)現(xiàn)了React組件之間數(shù)據(jù)的共享

組件如何讀寫數(shù)據(jù)

組件通過action發(fā)送信號, reducer處理action, story內(nèi)的值被reducer修改, 由于React組件已經(jīng)被綁定到story中, 所以story內(nèi)的數(shù)據(jù)被修改后, 可以直接同步到React的組件中

小案例: 實(shí)現(xiàn)一個組合計(jì)數(shù)器

單個計(jì)數(shù)器的數(shù)據(jù)由組件自身state管理

三個計(jì)數(shù)器的數(shù)據(jù)只和由Redux管理

動圖演示

實(shí)現(xiàn)的源碼如下

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>react-webpack-demo</title>
</head>
<body>
  <div id="root"></div>
</body>
</html>

index.js

import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import './index.scss';
import Redux from 'redux';
import { connect, Provider } from 'react-redux';
import { createStore } from 'redux';
import { PropTypes } from 'prop-types';

class ManageCounter extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return ( <div>
      <p className="title">計(jì)數(shù)器</p> 
      <Counter id = "0" />
      <Counter id = "1" />
      <Counter id = "2" />
      <p className="result"> 組件值的和為: { this.props.sum } </p> 
      </div> )
  }
}


class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.changeSum = this.changeSum.bind(this)
    this.decrease = this.decrease.bind(this)
    this.increase = this.increase.bind(this)
    this.state = { value: 0 };
  }
  changeSum() {
    this.props.dispatch({ type: 'changeSum', payload: { id: this.props.id, value: this.state.value } })
  }
  decrease() {
    let self = this;
    this.setState({ value: this.state.value - 1 }, () => {
      self.changeSum()

    })
  }

  increase() {
    let self = this;
    self.setState({ value: this.state.value + 1 }, () => {
      self.changeSum()
    })
  }

  render() {
    const { value } = this.state;
    let { id } = this.props;
    return ( <div >
      <input type = "button"value = "減1" onClick = { this.decrease }/> 
      <span > { value } < /span><br/ >
      <input type = "button" value = "加1" onClick = { this.increase }/>
      </div> )
  }
}

// 創(chuàng)建reducer
function reducer(state = { number: [0, 0, 0], sum: 0 }, action = {}) {
  if (action.type == 'changeSum') {
    let { id, value } = action.payload
    console.log("id:", id, "value:", value);
    state.number[id] = value
    let tmpSum = 0;
    for (let i = 0; i < state.number.length; i++) {
      tmpSum += state.number[i]
    }
    return Object.assign({}, state, { sum: tmpSum });
  } else {
    return state;
  }
}

const CounterMapStateToProps = (state) => ({

})

const ManageCounterMapStateToProps = (state) => ({
  sum: state.sum
})

const mapDispatchToProps = (dispatch) => ({
  dispatch: dispatch
})


// 創(chuàng)建store
let store = createStore(reducer)
// connect連接
Counter = connect(CounterMapStateToProps, mapDispatchToProps)(Counter)
ManageCounter = connect(ManageCounterMapStateToProps, mapDispatchToProps)(ManageCounter)

ReactDOM.render(
  <Provider store = { store }>
  <ManageCounter />
  </Provider> ,
  document.getElementById('root'));

index.scss

$designWidth: 750;
@function px2rem($px) {
  @return $px*10/$designWidth+rem;
}

#root {
  div {
    p {
      font-size: px2rem(300);
      color: #5EA1F3;
      text-align: center;
    }
    div {
      font-size: px2rem(500);
      display: flex;
      color: #64B587;
      border: 1px solid #F0BB40;
      input {
        flex: 1 1 auto;
        background-color: #64B587;
        font-size: px2rem(200);
        outline: none;
        color:#ffffff;
      }
      span {
        width: 300px;
        flex: 1 1 auto;
        text-align: center;
      }
    }
    .title {
      color: #BDBDBD;
    }
    .result {

      font-size: px2rem(200);
    }
  }
}

小結(jié)

redux的設(shè)計(jì)思想是很簡單的, 也有了很成熟的庫函數(shù)供我們調(diào)用, 所以面對一個問題時, 我們考慮的重點(diǎn)是: React組件內(nèi)哪些數(shù)據(jù)需要被Redux管理?把重點(diǎn)問題考慮清楚, 問題也就解決了大半!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 返回函數(shù)的JavaScript函數(shù)

    返回函數(shù)的JavaScript函數(shù)

    理解返回函數(shù)的JavaScript函數(shù)是什么意思,通過幾個簡短的例子真正理解返回函數(shù)的JavaScript函數(shù)到底是什么?感興趣的小伙伴們可以參考一下
    2016-06-06
  • 使用focus方法讓光標(biāo)默認(rèn)停留在INPUT框

    使用focus方法讓光標(biāo)默認(rèn)停留在INPUT框

    讓光標(biāo)默認(rèn)停留在INPUT框中,用focus方法可以實(shí)現(xiàn),下面有個示例代碼,需要的朋友可以參考下
    2014-07-07
  • webpack 5 mode的作用和區(qū)別解析

    webpack 5 mode的作用和區(qū)別解析

    Webpack 5 是一款強(qiáng)大的模塊打包工具,可用于將許多分散的模塊按照依賴關(guān)系打包成一個(或多個)bundle,這篇文章給大家介紹webpack 5 mode的作用和區(qū)別解析,感興趣的朋友跟隨小編一起看看吧
    2024-01-01
  • JS實(shí)現(xiàn)不使用圖片仿Windows右鍵菜單效果代碼

    JS實(shí)現(xiàn)不使用圖片仿Windows右鍵菜單效果代碼

    這篇文章主要介紹了JS實(shí)現(xiàn)不使用圖片仿Windows右鍵菜單效果代碼,涉及文鼎字及css樣式的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • 微信小程序可滑動月日歷組件使用詳解

    微信小程序可滑動月日歷組件使用詳解

    這篇文章主要為大家詳細(xì)介紹了微信小程序可滑動月日歷組件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 深入學(xué)習(xí)JavaScript對象

    深入學(xué)習(xí)JavaScript對象

    今天小編就和大家深入學(xué)習(xí)JavaScript對象,感興趣的小伙伴們可以參考一下,大家一起學(xué)習(xí)。
    2015-10-10
  • 淺談js數(shù)組splice刪除某個元素爬坑

    淺談js數(shù)組splice刪除某個元素爬坑

    這篇文章主要介紹了淺談js數(shù)組splice刪除某個元素爬坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 基于HTML+JavaScript實(shí)現(xiàn)中國象棋

    基于HTML+JavaScript實(shí)現(xiàn)中國象棋

    這篇文章主要為大家詳細(xì)介紹了如何利用HTML+CSS+JS實(shí)現(xiàn)中國象棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • js遍歷json對象所有key及根據(jù)動態(tài)key獲取值的方法(必看)

    js遍歷json對象所有key及根據(jù)動態(tài)key獲取值的方法(必看)

    下面小編就為大家?guī)硪黄猨s遍歷json對象所有key及根據(jù)動態(tài)key獲取值的方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • javascript圖片切換綜合實(shí)例(循環(huán)切換、順序切換)

    javascript圖片切換綜合實(shí)例(循環(huán)切換、順序切換)

    這篇文章主要介紹了javascript圖片切換綜合實(shí)例,包括javascript圖片循環(huán)切換、javascript圖片順序切換,兩張圖片的切換,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-01-01

最新評論