Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Question: How to make asynchronous calls from a form submit? #178

Closed
jimmyandrade opened this issue May 27, 2020 · 3 comments
Closed

Question: How to make asynchronous calls from a form submit? #178

jimmyandrade opened this issue May 27, 2020 · 3 comments
Labels
question Further information is requested

Comments

@jimmyandrade
Copy link

jimmyandrade commented May 27, 2020

I am building an application to check for traffic tickets for a specific car.

It should work as follows:

  1. User enters a car license plate number in an input;
  2. User clicks on submit button;
  3. An API call should be made to an endpoint using axios.

I was trying to use the car license plate number as the argument using selectorFamily, but it is still not available (#105). So... any thoughts about how to implement this using recoil?

@jimmyandrade jimmyandrade changed the title [Question] How to make asynchronous calls from a form submit [Question] How to make asynchronous calls from a form submit? May 27, 2020
@drarmstr drarmstr linked a pull request May 27, 2020 that will close this issue
@drarmstr
Copy link
Contributor

Is this issue that you're waiting for selectorFamily to be working properly from the npm package? We're hopeful to get that updated this evening.. In the meantime, you could also easily replicate that pattern yourself by making a memoized function which takes the license plate and returns a selector for that plate. In this case, though, it also sounds like you could store the license plate in an atom and reference that from the selector without needing a parameterized selector (see this example).

Duplicate of #105

@acutmore
Copy link

Hi @jimmyandrade

User enters a car license plate number in an input;

Likely I am interpreting this wrong but it sounds like there is only one license plate number entered at a time. Could you just use one Recoil atom to represent the current licence plate number. Or is there another reason why you were looking to use selectorFamily?

const currentLicensePlate = atom({
  key: "currentLicensePlate",
  default: null,
});

const dataForCurrentLicensePlate = selector({
  key: "currentLicensePlateData",
  get({ get }) {
    const lpn = get(currentLicensePlate);
    if (lpn === null) {
      return null;
    }
    return axios.get("/data", {
      params: {
        license: lpn,
      },
    });
  },
});

function MyComponent() {
  const setCurrentLicense = useSetRecoilState(currentLicensePlate);
  const [input, setInput] = useState("");

  return (
    <>
      <input type="text" value={input} onChange={setInput} />
      <button onClick={() => setCurrentLicense(input)}>Submit</button>
    </>
  );
}

function ShowData() {
  const data = useRecoilValue(dataForCurrentLicensePlate);
  return <pre>{JSON.stringify(data)}</pre>;
}

@drarmstr
Copy link
Contributor

@jimmyandrade - Please feel free to re-open if the suggestions here don't help!

@jimmyandrade jimmyandrade changed the title [Question] How to make asynchronous calls from a form submit? Question: How to make asynchronous calls from a form submit? May 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants