1

import React, { useState, useRef, useReducer } from "react";
import "./App.css";

function reducer(state,action){ 
  const {past,present,future} = state
  switch(action.type){
    case 'ADD' : return {

      past : [...past,present],
      present : [...present,{id : Math.random(),name : action.payload}],
      future : [],
      canUndo : true,
      canRedo : false,
      
    }

    case "UNDO" : if(!state.canUndo) return state;
    return {
      present :past[past.length - 1], 
      past : past.slice(0,-1),
      future : [present,...future],
      canUndo : past.length > 1,
      canRede : true
    }
    case 'REDO' : return {
    
    }
    default: return state;
  }

}
function App(){
  const input = useRef();

  const initialState = {
    present : [],
    past : [],
    future : [],
    canUndo : false,
    canRedo : false,
  }

  const[items,dispatch] = useReducer(reducer,initialState);

  const addItem = (e) => {
    e.preventDefault();
    dispatch({type : 'ADD', payload : input.current.value})
    input.current.value = '';
 
  }
  return(
    <div>
      <h1>Shopping list</h1>
      <form className='add-product' onSubmit={addItem}>
        <label htmlFOR='product'>product</label>
        <input ref={input} type='text' id='product'/>
        <button type='submit'>Add</button>
      </form>

      <div className='actions'>
        <button onClick={() => dispatch({type : 'UNDO'})}>Undo</button>
        <button onClick={() => dispatch({type : 'REDO'})}>Redo</button>
      </div><ul>
        {items.present.map(item => <li key={item.id}>{item.name}</li>)}
      </ul>

      
    </div>
  )
}



export default App;

Hello Everyone, i am having hard time understanding this concept

present : past[past.length - 1]

So after adding a value, This should return a single value when I click on Undo button, but it is returning a full array except the last one, if anyone explains this concept I will be very thankful, why it is behaving like slice method

Adan Ali
  • 9
  • 5
  • 1
    I think issue is with some of your operations where you're concatenating arrays like this: `[...past,present]` -> here, shouldn't it be `[...past,...present]`. Also, this `future : [present,...future]` should be: `future : [...present,...future]` . Right? I could be wrong, but that's what it seemed like, to me... – Ketan Malhotra Sep 10 '21 at 20:43
  • Running your code in a [codesandbox](https://codesandbox.io/s/present-pastpast-length-1-this-should-return-a-single-value-but-it-is-re-osmun) it seems to be running correctly (as I am understanding). What is the issue? Are you asking why `past[past.length - 1]` returns an array? – Drew Reese Sep 10 '21 at 20:50

1 Answers1

0

If I'm understanding your post, you are asking why past[past.length - 1] returns an array instead of a single value.

The reason for this is because it IS returning a single value, the last element of the past array, and this value just happens to also be an array. In other words, your state, i.e. present is an array of values and you are maintaining "undo" and "redo" stacks. When a new item is added to the present array, the previous present array is added (PUSHED) to the past undo stack. When you undo this you are removing the last item (POPPED) from the past undo stack and replacing the current present array.

Example:

Initial state:

past: []
present: []
future: []

ADD 1: present is pushed into past ([].push([]) -> [[]]), 1 is pushed to present ([].push(1) -> [1])

past: [[]]
present: [1]
future: []

ADD 2: present is pushed into past ([[]].push([1]) -> [[], [1]]), 2 is pushed to present ([1].push(2) -> [1, 2])

past: [[], [1]]
present: [1, 2]
future: []

UNDO: present is pushed into future ([].push([1, 2]) -> [[1, 2]]), past is popped ([[], [1]].pop() -> [[]]) and set as present ([1])

past: [[]]
present: [1]
future: [[1, 2]]

ADD 3: present is pushed into past ([[]].push([1]) -> [[], [1]]), 3 is pushed to present ([1].push(3) -> [1, 3]), future is cleared ([])

past: [[], [1]]
present: [1, 3]
future: []

ADD 4: present is pushed into past ([[], [1]].push([1, 3]) -> [[], [1], [1, 3]]), 4 is pushed to present ([1, 3].push(4) -> [1, 3, 4])

past: [[], [1], [1, 3]]
present: [1, 3, 4]
future: []

Note that any element entry in past or future arrays (stacks) is an array.

Edit present-pastpast-length-1-this-should-return-a-single-value-but-it-is-re

enter image description here

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • wow, thank you very much for explaining this deep, sorry for my bad english i again have a one question, suppose when i will use this `present :past[past.length - 1]` ... doesnt it will change the original present state to just 1 value i mean suppose present have 3 items present [ {1}, {2}, {3}] now if i use `present :past[past.length - 1]`, it will only return a singla array like [ {} ] we are undoing only 1 value which is the last one {3}. in present array but by doing this `present :past[past.length - 1]`, it will replace present with single array – Adan Ali Sep 11 '21 at 07:11
  • please read this, i have tried to explain my 2nd question https://stackoverflow.com/questions/69142118/present-pastpast-length-1-this-should-be-overwritten-by-a-single-value-but, – Adan Ali Sep 11 '21 at 10:37
  • @AdanAli Your other question seems to be a duplicate of this question. `present` is *always* just a single value, i.e. the current value, while the `past` and `future` will *always* be an array (a "stack" essentially) of "current" values. All you are doing when you undo is move the current present value onto the top of the `future` stack and move the top of the `past` stack into the present value, and the opposite when you redo. – Drew Reese Sep 11 '21 at 19:45
  • present have multiple items in it, whatever i click on `add` ..`items` is getting stored in `present`, that is why i am using `item.present.map(item =>
  • {item}
  • ` to show link items on browser – Adan Ali Sep 12 '21 at 06:54