T
TanStack4y ago
foreign-sapphire

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
equal-aqua
equal-aqua4y 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?