Rematch: React example contains an error

Created on 21 Nov 2018  路  4Comments  路  Source: rematch/rematch

https://codesandbox.io/s/92mk9n6vww

The remove reducer function violates purity.

remove(state, id) {
  delete state[id] // <<< mutates state
  return {
    ...state
  }
}

Should be written in another way, e.g.

remove(state, id) {
  return Object.keys(state).reduce(
    (acc, cur) => {
      if (cur === id) return acc
      acc[cur] = state[cur]
      return acc
    },
    {}
  )
}
wontfix

Most helpful comment

In the current example it may be okay, but overall it is a bad practice, usually leading to bugs.
Feel free to close this issue if you think that it is not important for this case.

All 4 comments

But that state is immediately replaced with a copy (and a new ref) of that state before anything else could read it...

If a tree falls in the woods and no one is there to hear it, does it make a sound?

In the current example it may be okay, but overall it is a bad practice, usually leading to bugs.
Feel free to close this issue if you think that it is not important for this case.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Closing as already fixed. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

slbucur picture slbucur  路  4Comments

kdela picture kdela  路  4Comments

ghost picture ghost  路  5Comments

ShMcK picture ShMcK  路  6Comments

suazithustra picture suazithustra  路  6Comments