0

I'm trying to add a element to a array, which is called allLogbooks. This array contains objects called Logbook. My issue is, how do I fit in code so that it adds the new element to the array which is declared in my reducer? This is my initial state:

const initialState = {
  isFetching: false,
  error: undefined,
  isRegistered: false,
  isUpdated: false,
  hasChecked: false,
  isError: false,
  registerStepOne: true,
  registerStepTwo: false,
  registerStepThree: false,
  registerStepFour: false,
  logbooks: undefined,
  allLogbooks: [],
  userInfo: undefined,
  logbookAddSuccess: false,
  newLogbook: undefined,
  graphFilter: 1
}

This is my reducer:

case ADD_LOGBOOK_SUCCESS:
    allLogbooks.push(action.newLogbook);
      return {
        ...state,
        isFetching: false,
        logbookAddSuccess: true,
        isUpdated: true,
        isError: false,
      }

And this is my action:

function addLogbookSuccess(newLogbook) {
    return {
        type: ADD_LOGBOOK_SUCCESS
        }
}

I then make a POST call to a nodejs server, where it will respond with a message if it was successful, and the logbook which was just created. the following data is what it returns:

{
    "success": true,
    "logbook": {
        "created_by": "",
        "created_at": "",
        "_id": "",
        "__v": 0
    }
}

I then dispatch in the API call, as such:

.then(data => data.json())
            .then(json => {
                Keyboard.dismiss();
                dispatch(addLogbookSuccess(json.logbook));

            })
            .catch(err => dispatch(addLogbookFailed(err)))

I've substituted this by using AsyncStorage to access the array abroad all my views, but I know this is wrong.

1 Answer 1

2

I am struggling a little to comprehend your question so I've tried my best. Please let me know if I'm completely off with my answer.

Your reducer:

case ADD_LOGBOOK_SUCCESS: {
  const newLogbook = action.payload;
  return {
    ...state,
    isFetching: false,
    logbookAddSuccess: true,
    isUpdated: true,
    isError: false,
    allLogBooks: [
      ...state.allLogBooks,
      newLogbook
    ]
  }
}

Your action:

function addLogbookSuccess(newLogbook) {
    return {
      type: ADD_LOGBOOK_SUCCESS,
      payload: newLogbook
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Definately on the right track. Sorry if my explanation is abit off. Your code gives me the error [TypeError: Object is null or undefined]. It successfully makes the call to the API and posts the new element, but it doesn't update the redux array allLogbooks. If I refresh Expo, and relog into the app, then it shows up, but only then.
I'll add some more of my code to the post, might give more insight as to how I've set it up.
Your code works perfectly, I am sorry. The error was a typo. "allLogBooks" instead of "allLogbooks". Thank you so much, makes perfect sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.