xplato / useUndoable

↪ React hook for undo/redo functionality (with batteries included)
MIT License
168 stars 7 forks source link

Falsy check on payload leads to payloadError when using first element of an enum #16

Closed stijn26 closed 1 year ago

stijn26 commented 1 year ago

Hi,

When using the mutate function with an enum, a payloadError is throwed because payload is evaluated to false, because the first element in an enum evaluates to 0.

For now we use a PLACEHOLDER value in our enum to work around the issue.

var mutate = function mutate(state, action) {
  var past = state.past,
      present = state.present,
      future = state.future;
  var payload = action.payload,
      behavior = action.behavior,
      historyLimit = action.historyLimit,
      ignoreIdenticalMutations = action.ignoreIdenticalMutations,
      cloneState = action.cloneState,
      ignoreAction = action.ignoreAction;

  if (!payload) { // this line is the culprit
    payloadError('mutate');
  }
xplato commented 1 year ago

Hello!

Thanks for bringing this up. Could you provide a little more information on your use case?

What does the enum look like? What does your usage of the hook look like? I'm trying to synthesize a solution that will work correctly, as this condition could potentially cause some backward compatibility issues.

xplato commented 1 year ago

Hey @stijn26, quick ping here :)

xplato commented 1 year ago

I'm going to close this due to inactivity. If you decide to revisit it, I will reopen this.