Skip to content
On this page

useInjectFactory

Wrapper on inject with treatDefaultAsFactory: true argument

Parameters

typescript
import { useInjectFactory } from '@elonehoo/pistachio'

const value = useInjectFactory(key, factory)
import { useInjectFactory } from '@elonehoo/pistachio'

const value = useInjectFactory(key, factory)
ParametersTypeRequiredDescription
keyString | Symboltruekey
factoryFunction<T> | Function<Promise<T>>trueWill be called if there's no value provided

Example

typescript
import { useInjectFactory } from '@elonehoo/pistachio'

const users = useInjectFactory("myValue", () => {
  if (new Date().getDate() === 2) {
    return {
      a: 1,
    };
  }

  return {
    b: 1,
  };
});

// promise
const users = useInjectFactory("myValue", () =>
  axios.get("/users").then((x) => x.data)
);
if (isPromise(users)) {
  // no value found, we can handle it
} else {
  // users provided
}
import { useInjectFactory } from '@elonehoo/pistachio'

const users = useInjectFactory("myValue", () => {
  if (new Date().getDate() === 2) {
    return {
      a: 1,
    };
  }

  return {
    b: 1,
  };
});

// promise
const users = useInjectFactory("myValue", () =>
  axios.get("/users").then((x) => x.data)
);
if (isPromise(users)) {
  // no value found, we can handle it
} else {
  // users provided
}

Released under the MIT License.