Getting recoil state inside fetch-query function
See original GitHub issueIm using react with relay… + Recoil for state management
In relay fetch query i check for cookies and if cookie with user_id change, im regenerating environment and i wanna get current state from recoil where global user id is stored (for needs to compare it with cookie )
But im not able to find way to get that out of hook… i need to check that cookies and get recoil state in all fetch respons… that is why im trying to put it in global fetch query…
What is correct way of accessing recoil state out of useRecoilState … out of using hook… ?
function fetchQuery(
operation,
variables,
cacheConfig
) {
const queryID = operation.text;
const isMutation = operation.operationKind === "mutation";
const isQuery = operation.operationKind === "query";
const forceFetch = cacheConfig && cacheConfig.force;
// Try to get data from cache on queries
const fromCache = cache.get(queryID, variables);
const [userStore, setUserStore] = useRecoilState(projectselector);
function checkCookies() {
const cookie_user_id = getUserIdFromCookie();
if (cookie_user_id) {
const value = parseInt(cookie_user_id);
if (value !== 0 && userStore.user_id !== value) {
setUserStore({ user_id: value });
return true;
}
}
return false;
}
if (isQuery && fromCache !== null && !forceFetch) {
return fromCache;
}
// Otherwise, fetch data from server
return fetch(BASE_GRAPHQL_URL, {
credentials: "include",
// mode: "cors",
method: "POST",
headers: {
Accept: "application/json",
// "Access-Control-Allow-Origin": "http://localhost:3000/",
"Content-Type": "application/json",
},
body: JSON.stringify({
query: operation.text,
variables,
operationName: operation.name,
}),
})
.then((response) => {
if (checkCookies()) {
// todo
}
return response.json();
})
.then((json) => {
// Update cache on queries
if (isQuery && json) {
cache.set(queryID, variables, json);
}
// Clear cache on mutations
if (isMutation) {
cache.clear();
}
return json;
});
}
export function createEnvironment() {
const network = Network.create(fetchQuery);
const store = new Store(new RecordSource());
return new Environment({
network,
store,
});
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Asynchronous Data Queries - Recoil
Recoil provides a way to map state and derived state to React components via a data-flow graph. What's really powerful is that the...
Read more >Mastering data fetching with React Query and Next.js
Learn how React Query simplifies data fetching and caching for you and how it works in tandem with the Next.js pre-rendering methods.
Read more >How to Use Recoil for State Management in Your React Projects
useRecoilValue is a hook provided by recoil that only returns the current state of date in the atom. We will use this hook...
Read more >calling recoil inside a function - reactjs - Stack Overflow
But when i place LikeApi module (function) inside the button function, i get an error: import {LikeApi} from ".
Read more >Syncing query parameters with react state - Inkoop
To achieve this, we will need a way to set and fetch query ... We will be using it inside our react components...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@BenjaBobs - There is this page in the docs discussing some state synchronization approaches. Though, I’m not a big fan of it. I’m planning to provide a new guide in the coming weeks based on the experimental API.
The
recoil-relay
library is now available for working with GraphQL and Recoil using Relay.