Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
React - Handle errors in reducers

Note: I am not using Redux (instead, React Context + reducers)

I have the following reducer:

export default (contents, action) => {
  switch (action.type) {
    case "like-content": {
      let { content } = action;

      const prevContent = contents.get(content.id);

      return new Map([...contents, [content, {
           ...content,
           totalLikes: prevContent.totalLikes + 1 
      }]);
    }

   ...

And I need to check if the given content is stored in my contents map before updating the state.

I have think about updating my code to:

export default (contents, action) => {
  switch (action.type) {
    case "like-content": {
      let { content } = action;

      const prevContent = contents.get(content.id);

      if (!prevContent) throw Error("The content doesn't exist");

      return new Map([...contents, [content, {
           ...content,
           totalLikes: prevContent.totalLikes + 1 
      }]);
    }

 ...

But, personally, it seems incorrect, as I am "breaking" the state update.

How can I handle this?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!