question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to update selector's 'temporary' value and do a request to api at the same time

See original GitHub issue

Hey guys!

I’m fascinated by this library and wanted to try it out. I came across a use case where I have to display to the user what he’s changed in the UI while making a request to the server to save that data.

Here is my selector to retrieve the data:

export const eventsListQuery = selector({
  key: "eventsList",
  get: async ({ get }): EventType[] => {
    const currentUser = get(userState);
    return await fetchEvents(currentUser);
  },
});

Then I render events, each event item gets only its id from props and selects the event data using this selector:

export const eventItemQuery = selectorFamily({
  key: "eventItem",
  get: (eventId: string) => ({ get }): EventType => {
    return get(eventsListQuery).find((event: EventType) => event.id === eventId);
  },
});

The user can change almost anything in the data, so I wanted to have this selector updating the state in a way that I could render some updates everywhere it’s being used and send a request to the server to get it updated on the backend side at the same time.

I tried to use set method of a selectorFamily but it throws an error indicating that eventsList could not be updated because it’s read-only which is I guess make sense.

export const eventItemQuery = selectorFamily({
  key: "eventItem",
  get: (eventId: string) => ({ get }): EventType => {
    return get(eventsListQuery).find((event: EventType) => event.id === eventId);
  },
  set: (currentItem: EventType) => ({ set, get }, updatedValue) => {
    const events = get(eventsListQuery).map((event: EventType): EventType => {
      if (event.id === currentItem.id) return { ...event, ...updatedValue };
      return event;
    });

    set(eventsListQuery, events);
  },
});

My question is, how can I achieve that user experience using Recoil?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
drarmstrcommented, Nov 2, 2020

Please note that the effect function itself should not be async. Though, you can set the initial setSelf() to a Promise.

We are exploring some ability to read other state from the handlers, perhaps though a getSnapshot() approach.

0reactions
drarmstrcommented, Nov 3, 2020

Consolidate discussion of getting other state from effects in #707

Read more comments on GitHub >

github_iconTop Results From Across the Web

NGRX selectors updating too often - Stack Overflow
Memoization works by comparing all inputs to their equivalent values the last time the selector was invoked. If they are the same, ...
Read more >
Redux Fundamentals, Part 6: Async Logic and Data Fetching
It only knows how to synchronously dispatch actions, update the state by calling the root reducer function, and notify the UI that something ......
Read more >
Update By Query API | Elasticsearch Guide [8.5] | Elastic
Set ctx.op = "noop" if your script decides that it doesn't have to make any changes. The update by query operation skips updating...
Read more >
Storing the information you need — Variables - MDN Web Docs
Variables just make sense, and as you learn more about JavaScript ... You can declare and initialize a variable at the same time,...
Read more >
Using variables | Postman Learning Center
For example, if you have the same URL in more than one request, but the URL might change later, you can store the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found