Coder Social home page Coder Social logo

Comments (1)

danielbayley80 avatar danielbayley80 commented on August 22, 2024

I got this working in a very crude manner. At the minute I have to ensure all my views are wrapped in the LayoutContainer that then uses context to determine what layout to load the view within.

I think it would be cleaner if I could create my views and layouts separately and have the TSX-views engine piece it together using the default view specified in config, or passed view the render method.

export default function LayoutContainer(props: any): ReactElement {
      const context = useContext(DefaultContext);
      let LayoutComponent;
      if (context.layout) {
            try {
                  const module = require(context.layout);

                  LayoutComponent = module.default;
            } catch (error) {
                  console.log(`Error loading layout '%s'`, context.layout)
            }
      }
      return LayoutComponent ? <LayoutComponent>{props.children}</LayoutComponent> : <>{props.children}</>;
}

Another thought I had was if there was a mechanism to have context globally available to views. For example res.locals. not played with this yet, but I think again if the TSX engine should perhaps have a default context that it makes available.

from express-tsx-views.

Related Issues (12)

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.