Coder Social home page Coder Social logo

Comments (1)

garbles avatar garbles commented on July 18, 2024 1

Hello! I wrote a previous explanation but deleted it because I didn't think it answered your question.

The library's purpose is to make dependency injection easier. It's entirely possible to use a simple React context that does the same thing, but there's a lot of boiler plate. If you wanted the same interface as this library, you might write something like this,

type Hooks = {
  useCurrentUser(): User;

  projects: {
    useOne(id: string): Project;
    useMany(): Project[];
  },

  // ... etc.
};

const HooksContext = React.createContext<Hooks | null>(null);

export const hooks: Hooks = {
  useCurrentUser() {
    const ctx = React.useContext(HooksContext);
    
    if (ctx === null) throw;

    return ctx.useCurrentUser();
  },

  projects: {
    useOne(id: string) {
      const ctx = React.useContext(HooksContext);

      if (ctx === null) throw;

      return ctx.projects.useProject(id);
    },

    useMany() {
      const ctx = React.useContext(HooksContext);

      if (ctx === null) throw;

      return ctx.projects.useMany();
    }
  },

  // ... etc.
};

export const ImplementationProvider = HooksContext.Provider;

The Proxy allows you to completely forgo the implementation of the hooks object while the type alias makes it appear as if there is a concrete implementation with specific key paths such as hooks.project.useMany() where everything type checks correctly.

Does this answer the question?

from react-facade.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.