Update atom default value use Recoil

41 Views Asked by At

I would like to modify the default value of my atom "quizAtom" according to the answer provided by the API. Here, the API returns a "count" property which corresponds to the number of questions found. I would like to modify the totalQuestions property with this value.

How to replace the default value 0 of totalQuestions with the response provided by the api (res.count)?

Atom.js :

  key: "quizAtom",
  default: {
    currentQuestion: 1,
    totalQuestions: 0,
    score: 0,
  },
});

Selector.js :

  key: "questionsSelector",
  get: async ({ get }) => {
    const res = await quizService.getQuestions(
      get(formAtom).theme,
      get(formAtom).difficulty
    );

    return res;
  },
});

0

There are 0 best solutions below