0

I know its possible to ADD and DELETE a file through the context API, but how would you edit one?

For example: This is the DELETE function with Global Context

case "DELETE_TRANSACTION":
  return {
    ...state,
    transactions: state.transactions.filter(
      (transaction) => transaction.id !== action.payload
    ),
  };

or in this ADD function example:

   case "ADD_TRANSACTION":
      return {
        ...state,
        transactions: [action.payload, ...state.transactions],
      };

What would the edit function be?

Toan Lam
  • 109
  • 2
  • 12

1 Answers1

0

case 'EDIT_TRANSACTION': const updatedTransaction = action.payload;

        const updatedTransactions = state.transactions.map((transaction) => {
            if (transaction.id === updatedTransaction.id) {
                return updatedTransaction;
            }
            return transaction;
        });

        return {
            ...state,
            transactions: updatedTransactions
        };