zoukankan      html  css  js  c++  java
  • Redux Counter Vanilla example

    此示例不需要构建系统或视图框架,并且存在以显示与ES5一起使用的原始Redux API。

    代码如下

    <!DOCTYPE html>
    <html>
      <head>
        <title>Redux basic example</title>
        <script src="https://unpkg.com/redux@latest/dist/redux.min.js"></script>
      </head>
      <body>
        <div>
          <p>
            Clicked: <span id="value">0</span> times
            <button id="increment">+</button>
            <button id="decrement">-</button>
            <button id="incrementIfOdd">Increment if odd</button>
            <button id="incrementAsync">Increment async</button>
          </p>
        </div>
        <script>
          function counter(state, action) {
            if (typeof state === 'undefined') {
              return 0
            }
    		//将action和state联系起来的reducer
            switch (action.type) {
              case 'INCREMENT':
                return state + 1
              case 'DECREMENT':
                return state - 1
              default:
                return state
            }
          }
    	//store存储数据
          var store = Redux.createStore(counter)
          var valueEl = document.getElementById('value')
    
          function render() {
            valueEl.innerHTML = store.getState().toString()
          }
    
          render()
          store.subscribe(render)
          document.getElementById('increment')
            .addEventListener('click', function () {
    		//通过store.dispatch将action传到store
              store.dispatch({ type: 'INCREMENT' })
            })
    
          document.getElementById('decrement')
            .addEventListener('click', function () {
              store.dispatch({ type: 'DECREMENT' })
            })
    
          document.getElementById('incrementIfOdd')
            .addEventListener('click', function () {
              if (store.getState() % 2 !== 0) {
                store.dispatch({ type: 'INCREMENT' })
              }
            })
    
          document.getElementById('incrementAsync')
            .addEventListener('click', function () {
              setTimeout(function () {
                store.dispatch({ type: 'INCREMENT' })
              }, 1000)
            })
        </script>
      </body>
    </html>
    
  • 相关阅读:
    数学分析学习笔记
    参数注解检查方法入参
    AOP 织入 Redis 缓存
    手写单例 Redis 分布式锁
    Leetcode 347 前K个高频元素 topK问题手写大顶堆
    PCB 网页WebODB++与Genesis同屏实现方法
    Git多密钥配置
    npm安装问题解决
    设计模式
    odoo14里面开发一个简单的action.client 的tag 模板例子
  • 原文地址:https://www.cnblogs.com/smart-girl/p/10781499.html
Copyright © 2011-2022 走看看