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.

Question: Post data through API with RecoilJS

See original GitHub issue

In RecoilJS docs, there is an example how to handle asynchronous data queries, but it’s only about get data.

Let say I have a simple state:

const accountState = atom({
  key: "accountState",
  default: {
    id: "",
    name: "",
  }
});

And a component which is a register form:

const RegisterForm = () => {
  return (
    <form>
      <input type="text" name="username" />
      <button type="submit">Register</button>
    </form>
  )
}

The posted data is in FormData. After successfully created new account, the server will send a response that contains id and name of the account.

{
  "id": "abcdef123456",
  "name": "example"
}

This response data will be set as a new state of accountState.

How can I handle the process in RecoilJS?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

28reactions
adrianbwcommented, Jul 10, 2020

A followup to say that I’d love to see an async setter so that we could do something much more simple like this:

const postSelector = selectorFamily({
  get: // whatever
  set: async (params) => ({ set }) => {
    const response = await postQuery(params);
    set(YourAtom, // etc);
  }
});
9reactions
drarmstrcommented, Jul 10, 2020

A followup to say that I’d love to see an async setter so that we could do something much more simple like this:

@adrianbw - Instead of an async post selector set, consider just using a setter for YourAtom from useSetRecoilState() or useRecoilCallback() to provide a callback which can asynchronously set the atom with the response.

const yourPostQuery = useRecoilCallback(({set}) => async params => {
  const response = await postQuery(params);
  set(yourAtom, response.data);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Post data through API with RecoilJS - reactjs - Stack Overflow
The posted data is in FormData . After successfully created new account, the server will send a response that contains id and name...
Read more >
CRUD with RecoilJS and remote API - Medium
RecoilJS is a modern state management library for React developed by the Facebook team. You may ask “Another one state management lib?
Read more >
Asynchronous Data Queries - Recoil
Asynchronous Data Queries. Recoil provides a way to map state and derived state to React components via a data-flow graph. What's really powerful...
Read more >
Limit of recoil and when to add or use extra library : r/reactjs
For example in one of my app, I have classic API calls and I build my state with async selectors which fetch the...
Read more >
React + Recoil - Set atom state after async HTTP GET or ...
This is a quick example of how to set the state of a Recoil atom after sending an async HTTP GET or POST...
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