This repository has been archived by the owner on Jan 1, 2025. It is now read-only.
I want to separate functions like set and get states from code #1142
Unanswered
nabezokodaikon
asked this question in
Q&A
Replies: 2 comments 2 replies
-
Can do import {
selector,
} from "recoil"
import { RecoilSelectorKeys } from "../define/RecoilKeys";
import { CityId } from "../model/cityId";
import { selectedCityIdState } from "../state/selectedCityIdState";
export const getSelectedCityIdSelector = selector<CityId>({
key: RecoilSelectorKeys.SELECTED_CITY_ID_SELECTOR,
get: ({get}) => {
const cityId = get(selectedCityIdState);
return cityId;
}
}); export const getSelectedCityId = () => useRecoilValue(getSelectedCityIdSelector); thank you. |
Beta Was this translation helpful? Give feedback.
1 reply
-
For a typesafe getter hook you need to also refine based on the // Getter
export const getSelectedCityId = () => useRecoilCallback(({snapshot}) => () => {
const cityId = snapshot.getLoadable(selectedCityIdState).getValue();
return cityId;
}); But, an important note about using useRecoilValue(selectedCityIdState); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to separate functions like set and get states from code.
Regarding the set, I was able to do the following, but the acquisition will be the type.
Is there any good way?
Beta Was this translation helpful? Give feedback.
All reactions