Creating a fetch wrapper may solve your problem:
function updateOptions(options) { const update = { ...options }; if (localStorage.jwt) { update.headers = { ...update.headers, Authorization: 'Bearer ${localStorage.jwt}', }; } return update; } export default function fetcher(url, options) { return fetch(url, updateOptions(options)); }
You also get the added benefit of being able to easily switch the request client to all the calls in your application if you decide that you like Axios or another package more. And you can do other things, for example, check if options.body object, and add the header 'Content-Type: application/json .
source share