SolidJSS
SolidJSโ€ข4mo agoโ€ข
2 replies
Cryptoflop

Best practice for supporting static and reactive args

If I want to create a reactive function that takes in either a static or reactive arg like this:
function createRequest(resource: string | Accessor<string>) {
  return createResource(
    typeof resource === "function" ? resource : () => resource,
    resource => fetch(resource)...
  );
}


I always wondered if there's a better alternative to string | Accessor<string>
to support the following static and reactive scenarios:
function Reactive() {
  const [dynamicResource, ...] = createSignal("/resourceA");
  const data = createRequest(dynamicResource);
  ...
}

function Static() {
  const data = createRequest("/resourceA");
  ...
}
Was this page helpful?