Using React, I am writing code for a plugin that uses local storage or session storage depending on the application that is calling it. It is set up by passing in optional parameters to the component that default to local storage. If our web service is using the plugin, it passes the session storage methods in. Due to constraints, our desktop app doesn't pass in the parameters, and so we default to local storage within the component.
export function myComponent(optionalParameter?: string, storeOptionalParameter?: (selection: string) => void) {
const [variable, storeVariable] = optionalParameter && storeOptionalParameter
? [optionalParameter, storeOptionalParameter]
: useLocalStorage<string>('storageName', 'default');
}
I know this is breaking the rules of hooks. Technically, it compiles and runs just fine, since every render will be dependent on the environment it is rendered in. I'd still like to find a better way to accomplish this so my linter will stop complaining about it.