1

In my react app, I have currently set hbs as view engine. My Views folder has a few hbs files.

I have the following right now in my app js

// View engine
app.engine(
  'hbs',
  hbs({
    extname: 'hbs',
  })
)

app.set('view engine', 'hbs')

The problem: I want to use a UI framework such as material UI. I need to be able to use statements such as:

import Button from '@material-ui/core/Button';

This is not possible in handlebars I think. So I need to switch all my views to javascript. How can I get rid of hbs and use javascript in my views folder for pages?

Here is my sample home hbs view:

{{> nHeader }}



 <div class="fullscreen_cover cover1">
    <div class="welcomer">
        <h1 class="mb-3 lead">
            This is my home page
        </h1>

    </div>
  </div>


{{> footer }}
3
  • "use javascript in my views" What does that mean exactly? HBS is javasript. How is this a React app if it uses handlebars? Commented Aug 5, 2018 at 15:00
  • hbs is javascript ? I didint know that. See my hbs code above.. Does that look like js ? Commented Aug 5, 2018 at 15:18
  • 1
    If you need React for server templates then use it as view engine. It's not just 'javascript'. It's React app. Commented Aug 5, 2018 at 15:39

1 Answer 1

1

You can't do this because this is impossible. What you actually want is to serve one HTML file with an associated bundle as a <script> tag which is your react app.

The users then loads this html file and the JS file(s). The JS bundle is your react app which is responsible for rerendering the DOM when the state of your application updates.

For the bundling you should use something like webpack.

Sign up to request clarification or add additional context in comments.

7 Comments

I use webpack and bundle js for the rest of the javascripts. For the views I am using hbs.. All javascripts are bundled using webpack.. Can you please advise how I can change the views to javascript ? I cant imagine everyone is using a template engine...
You can leave your viewengine at hbs. You just need to add your bundled scripts to your index.html and serve that when the user makes a request to your home URL
View engine javascript doesn't exist, React uses no view engine it just uses JS under the hood to update the DOM and thus your user interface
I see, So I should simply delete the views folder.. and take care of templating in my js files.. correct ? I added a sample of my home hbs.
The templating is enterily done on the front end. When you send your index.html and react bundles to the client, the browser will render it on the frontend not on the backend. So there is no viewengine required.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.