Amr Eraky
Amr Eraky

Reputation: 1791

Can I use OOP in Redux Store actions

I'm using React JS and Redux JS. I know that Redux actions functions should be a pure function

But, I was reading about Object Oriented Programming (OOP) and its benefits.

So, Can I use OOP in Redux action (Class) instead of pure functions ??

For example: If I've a state named "articles", It may have actions like:

function getArtcles() { ... }
function addArtcle({data}) { ... }
function deleteArtcle(id) { ... }
......

So, can I replce that with:

 class Artice {
    getArtcles() {...}
    addArtcle() {...}
    deleteArtcle() {...}
}

??

Upvotes: 0

Views: 138

Answers (1)

Nicholas Tower
Nicholas Tower

Reputation: 85112

I assume those functions are action creators? Ie, they return an object with a type property, and possibly some additional data?

function deleteArticle(id) {
  return { type: 'deleteArticles', id };
}

Or they may create thunks:

function deleteArticle(id) {
  return async function (dispatch) {
    dispatch({ type: 'deleteStarting' });
    // do something asynchronous
    dispatch({ type: 'deleteSuccessful' });
  }
}

If so, yes, you can put them in a class if you want. I don't see a benefit to putting these in a class, but redux doesn't even have a way to know how you created the actions, so it won't mind.

Upvotes: 1

Related Questions