• react之redux增加删除数字


    比如在页面中添加和删除‘222’

    action.js

    export const ADD= 'ADD';
    export const RED='RED';
    export const add=(str)=>{
      return{
        type:'ADD',
        payload:str
      }
    };
    export const red=(str)=>{
      return {
        type:'RED',
        payload:str
      }
    };

    reducer.js

    import {combineReducers} from 'redux';
    import {ADD,RED} from '../action';
    const initialState = {
      cartData:["1","2","3"]
    };
    const cartReducer=(state=initialState,action)=>{
      state = JSON.parse(JSON.stringify(state));//深拷贝数据
      switch (action.type){
        case ADD:
          state.cartData.push('222');
          return {cartData:state.cartData};
          break;
        case RED:
          state.cartData.pop();
          return {cartData:state.cartData}
        default:
          return state;
      }
    };
    const reducer = combineReducers({
      cartReducer
    });
    export default reducer;

    addStr.js

    import React,{Component} from 'react'
    import { connect } from 'react-redux';
    class AddStr extends Component{
      constructor(props){
        super(props)
      }
      render(){
        const {cartData,addCartData}= this.props;
        const datas=this.state.datas;
        console.log(datas)
        return(
          <div className='adds'>
            <ul>
              {
                cartData.map((item,index)=>{
                   return <li key={index}>{item}</li>
                })
              }
            </ul>
            <button onClick={()=>addCartData()}>添加字符</button>
          </div>
        )
      }
    }
    function mapStateToProps(state) {
      return{
        cartData:state.cartReducer.cartData
      }
    }
    function mapDispatchToProps(dispatch) {
      return{
        addCartData:()=>dispatch({type:'ADD'})
      }
    
    }
    export default connect(
      mapStateToProps,
      mapDispatchToProps
    )(AddStr);

    redStr.js

    import React, { Component } from 'react';
    import { connect } from 'react-redux';
    class RedStr extends Component{
      constructor(props){
        super(props);
      };
      render(){
        const {cartData,addCartData}= this.props;
        return (
          <div>
            <button onClick={()=>addCartData()}>删除字符串</button>
          </div>
        )
      }
    }
    function mapStateToProps(state) {
      console.log(state.cartReducer);
      return{
        cartData:state.cartReducer.cartData
      }
    }
    function mapDispatchToProps(dispatch) {
      return{
        addCartData:()=>dispatch({type:'RED'})
      }
    }
    export default connect(
      mapStateToProps,
      mapDispatchToProps
    )(RedStr);

    roots.js

    import React, { Component } from 'react';
    import AddStr from '../components/addStr'
    import RedStr from '../components/redStr'
    class Roots extends Component{
      constructor(props){
        super(props);
      }
      render(){
        return (
          <div>
            <AddStr/>
            <RedStr />
          </div>
        )
      }
    }
    export default Roots;
  • 相关阅读:
    LVS的DR模式测试案例<仅个人记录>
    awk命令小结
    iptables命令提取总结,包含扩展模块<取自朱双印博客>
    如何配置nginx屏蔽恶意域名解析指向《包含隐藏nginx版本号》
    CentOS升级OpenSSL至OpenSSL 1.1.0f版本<其中有遇到libcrypto.so的问题>
    U-Mail企业邮箱如何导入授权文件
    Linux花生壳使用篇
    windows远程桌面连接时,显示发生身份验证错误,给函数提供的身份无效
    批量屏蔽符合条件的IP地址,支持添加白名单,IP段,增量,大于指定次数的IP
    rsync 定时备份<crontab+backrsync.sh> 简陋版
  • 原文地址:https://www.cnblogs.com/wdxue/p/8134373.html
Copyright © 2020-2023  润新知