Comments (4)
Will take a look asap
from use-http.
Okay, so we don't typically use onUpdate for something like this. Having onUpdate
here would make every http request on your page re-run if the token changed which does not sound like something you want.
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?
from use-http.
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 :)
I understand the example that you're giving, however something still seems to not be working.
I have updated the example with some clearer code perhaps.
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.
from use-http.
Apologies for the delay on this, but I think this PR might fix this. Try v0.2.3
. I'm going to close this. If the issue persists, let me know and I will reopen.
from use-http.
Related Issues (20)
- function to overwrite global options don't update after dependency change HOT 6
- useFetch `post` call inserts quotes when passed a string HOT 6
- React Native and Cache
- [Feature Request] time denounce http get requests
- does useFetch have option for skip request?
- Data not populated at mount HOT 5
- React 18 StrictMode HOT 22
- `Error: Invalid hook call` after upgrading to React 18.1 HOT 1
- Interceptors aren't chained HOT 8
- Cannot abort delayed retries
- use-Fetch Post is having issue with csrf headers HOT 7
- [Feature Request] A way to invalidate the cache HOT 1
- Upload File Progress
- can recall post when parameter change?
- does post can auto fire? HOT 2
- Eslint rule react-hooks/exhaustive-deps questions on having response as depency HOT 1
- React 18 not supported? HOT 1
- Following instructions in .github/contributing.md fails when using the latest React 18 HOT 2
- Initial URL with a trailing slash adds double slash on relative urls
- TS: Ability to add expected type for fetch body. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from use-http.