【问题标题】:Redux action returns undefinedRedux 操作返回未定义
【发布时间】:2018-10-14 19:07:26
【问题描述】:

我有以下减速器:

export const showResult = (state = [], action) => {
    switch (action.type) {
      case 'GET_NUMBER': 
        return [
          ...state,
          action.n
        ]
      case 'CLEAR_NUMBER':
         return [
          ...state
        ].pop()
      case 'RESET_RESULT': 
        return []
      default:
        return state
    }
  }

点击“deleteNumber”时遇到“CLEAR_NUMBER”情况:

import React, { Component } from 'react'
import Display from './Container'
import Operator from './Container'
import { selectOperator } from './../util'

class Calculator extends Component {
    constructor(props) {
      super(props)

      this.state = {
          val1: '',
          val2: ''
      }

    }
    displayNumber = e => {
        const { getNumber, resetResult, getOperator } = this.props
        getNumber(parseInt(e.target.dataset.n))
    }

    getOperator = e => {
        const { getOperator, n, resetResult, resetOperator } = this.props
        resetOperator()
        getOperator(e.target.dataset.sign)

        if(this.state.val1 == "") {
            this.setState({
                val1: n
            })
            resetResult()
        }

    }
    getSum = val => {
        val.reduce((acc, val) => acc + val)
    }
    deleteNumber = () => {
        const { clearNumber } = this.props
        clearNumber()
    }
    getTotal = () => {
        const { n, operator, resetResult } = this.props,
            { val1, val2 } = this.state,
            value1 = this.getSum(val1),
            value2 = val2 != "" ? this.getSum(val2) : null;
        let operatorVal = operator[0]

        this.setState({ val2: n })
        resetResult()
        selectOperator(operatorVal, value1, value2)

    }
    render() {
        const { n, operator, getNumber } = this.props
        return (
            <div>
                <Display val={n == "" ? null : n} />
                <Operator val={operator} />

                {/* NUMBERS */}
                    <p data-n="1" onClick={this.displayNumber}>1</p>
                    <p data-n="2" onClick={this.displayNumber}>2</p>
                    <p data-n="3" onClick={this.displayNumber}>3</p>

                {/* OPERATORS */}
                    <p data-sign="+" onClick={this.getOperator}>+</p>
                    <p data-sign="-" onClick={this.getOperator}>-</p>
                    <p data-sign="/" onClick={this.getOperator}>/</p>
                    <p data-sign="*" onClick={this.getOperator}>*</p>
                    <p onClick={this.getTotal}>equal</p>
                    <p onClick={this.deleteNumber}>clear</p>
            </div>
        )
    }
}
   
export default Calculator

它实质上是从数组中删除最后一个元素(就像您在计算器上有一个 clear 函数一样)。

我得到的错误是数组中只剩下一项:

【问题讨论】:

    标签: javascript reactjs ecmascript-6 redux


    【解决方案1】:

    这很好,但如果数组为空怎么办? pop 将返回 undefined 因此错误。

    case 'CLEAR_NUMBER':
         return [
          ...state
        ].pop()
    

    编辑:

    case 'CLEAR_NUMBER': {
      if(state.length > 0) {
        return [
          ...state
        ].pop()
      }
    
      return state;
    }
    

    【讨论】:

    • 不完全,即使数组中还剩下一个数字,我也会收到错误
    • @Alex 你能像编辑一样做一个简单的检查吗?顺便说一句pop 返回数组中的最后一个元素,不确定你是否知道。
    【解决方案2】:

    pop 返回删除的元素,而不是修改后的数组。

    你需要做的

    const newState = [...state];
    newState.pop();
    return newState;
    

    或者,使用return state.slice(0, -1)

    【讨论】:

      【解决方案3】:

      您应该这样使用以避免此类错误:

      case 'CLEAR_NUMBER':
        if ([...state].length > 1) {
          return [...state].pop()
        }
        return [] // or return null, but not undefined ie. no explicit return
      

      【讨论】:

      • @Alex 这适用于 state.length >= 2?它会将您的状态从数组更改为元素...
      猜你喜欢
      • 2019-05-17
      • 2018-09-15
      • 2018-03-27
      • 2021-04-28
      • 2022-01-21
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2015-09-22
      相关资源
      最近更新 更多