Aessandro
Aessandro

Reputation: 5761

Redux action returns undefined

I have the following reducer:

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
    }
  }

the "CLEAR_NUMBER" case is met when "deleteNumber" is clicked:

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

it essentially removes the last element from the array (like you would have a clear function on a calculator).

The error I get is when there is one item left in the array:

enter image description here

Upvotes: 3

Views: 604

Answers (3)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

You should be using like this to avoid such error:

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

Upvotes: 2

Roy Wang
Roy Wang

Reputation: 11260

pop returns the removed element, rather than the modified array.

You need to do

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

Alternatively, use return state.slice(0, -1).

Upvotes: 2

Tomasz Mularczyk
Tomasz Mularczyk

Reputation: 36169

That's neat what you do, but what if an array is empty? pop will return undefined hence the error.

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

EDIT:

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

  return state;
}

Upvotes: 2

Related Questions