T
TanStack3y ago
exotic-emerald

How to use context data in a query?

I have an access token that I am keeping in a Solid context. How can I use this token in a header in my solid-query requests?
1 Reply
sunny-green
sunny-green3y ago
Hi there! queryFn is just a function that returns a promise. You can implement your custom fetch inside it and use your access-token. So for example if you are using fetch, it will look something like this
const accessToken = useContext(AcccessTokenContext)

const query = createQuery(
() => ['post'],
() => {
const response = fetch(`https://my-api.con/posts`, {
headers: {
token: accessToken
}
})
if (!response.ok) throw new Error(`Something went wrong`)
return response.json()
}
)
const accessToken = useContext(AcccessTokenContext)

const query = createQuery(
() => ['post'],
() => {
const response = fetch(`https://my-api.con/posts`, {
headers: {
token: accessToken
}
})
if (!response.ok) throw new Error(`Something went wrong`)
return response.json()
}
)

Did you find this page helpful?