The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

503337 (1) [Avatar] Offline
#1
* enable reducer reloading (3.6.2)
* create new task (will get some Id, let's say 5)
* edit this task via ui (set new status)
* edit reducer

I would expect all actions to be replayed correctly, but 'edit task' action will not be applied. The issue appears because replaying 'create task' will add tasks objects with further increased ids (6), but 'edit' will still point to older one (5).

In order to fix this I moved getUniqueId to reducers/index.js, made it derive new id from existing tasks list and removed id from action payload. While this worked for me I wonder what is the correct way to handle such issues?

...
function uniqueId(tasks) {
  if (tasks && tasks.length !== 0) {
    const lastId = tasks.sort((a, b) => b.id - a.id)[0].id
    return lastId + 1
  } else {
    return 1
  }
}

const mockTasks = [
  {
    id: 1,
    ...
  },
  {
    id: 2,
    ...
  }
]
...


Called like this:

export default function tasks(state = { tasks: mockTasks }, action) {
  switch (action.type) {
    case 'CREATE_TASK': {
      const { title, description, status } = action.payload
      const id = uniqueId(state.tasks)
      ...


Marc Garreau (28) [Avatar] Offline
#2
Nice catch, and well done for finding a solution! Our method for deriving IDs is definitely not well-suited for production; ultimately, its role is to mock behavior that a server should provide. I'll add a note to see if we add some clarifying language or code in the next round of updates. Thanks for the detailed write-up!