-
-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provider interceptor uses cached values? #135
Comments
Will take a look asap |
Okay, so we don't typically use onUpdate for something like this. Having We would do it something like const CustomProvider = ({ children }) => {
const [token, setToken] = useLocalStorage('token')
const getToken = async () => {
return 'my-token'
}
const options = {
interceptors: {
request: async options => {
if (!token) {
const newToken = await getToken()
setToken(newToken)
}
console.log('Interceptor called with token', token)
options.headers['x-token'] = token
return options
},
},
}
useEffect(() => {
console.log('Logging token', token)
}, [token])
return (
<Provider url={window.location.origin} options={options}>
{children}
</Provider>
)
}
const TestFetch = () => {
const [token] = useLocalStorage('token')
const [request, response] = useFetch()
useEffect(() => {
async function getSomething() {
console.log('Performing request with token in context as:', token)
await request.get('/test.json')
if (response.ok) console.log('Finished.')
}
getSomething()
}, [])
return (
<>
<span>Token is {token}</span>
</>
)
}
const App = () => {
return (
<TestProvider>
<CustomProvider>
<Snowflakes>
<Center>
<TestFetch />
</Center>
</Snowflakes>
</CustomProvider>
</TestProvider>
)
} Does this help at all? |
Thanks for getting back at me! The onUpdate was something I tried with the idea of "lets see if this works". So that shouldn't have been in the example :) You can see in the console how the CustomProvider logs the state object as it should be when the state changes, however the request is still performed without the updated state values. Perhaps I'm missing something obvious. |
Apologies for the delay on this, but I think this PR might fix this. Try |
I have created a custom Provider with (global) options to send certain headers on all requests via an interceptor. This interceptor should get a value from a context and send it with the headers, but it doesn't seem to update to the new value.
Created a CodeSandbox here
When you run it and open inspector to check the network calls, you can see that the x-token header remains null on each call, even though the 'token' value is updated in the context as showed in the console and on the component render.
The funny thing is, if I place the exact same interceptor code in the useFetch hook itself, it does send the correct header value.
Like so:
The text was updated successfully, but these errors were encountered: