Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Pug with React?

Tags:

reactjs

pug

Something like:

btn = ({click, text})->
    a.pug.btn(target='blank' on-click=click) #{text}
like image 400
Andrey Stehno Avatar asked Jan 28 '18 00:01

Andrey Stehno


People also ask

Is Pug better than HTML?

Notice that coding using the PUG framework is much easier and more readable than thr HTML codes. PUG makes use of indentation to distinguish between where a tag starts and ends. This makes the code much cleaner than HTML, where lack of indentation and a need for closing tags makes the code a little cluttered.

Is Pug still used?

Pugs are known for being sociable and gentle companion dogs. The American Kennel Club describes the breed's personality as "even-tempered and charming". Pugs remain popular into the twenty-first century, with some famous celebrity owners.


Video Answer


2 Answers

I see this question is quite old, but if you are still looking for the answer, then there is an official plugin for babel, which transpiles pug into jsx: https://github.com/pugjs/babel-plugin-transform-react-pug

For example:

export default const ReactComponent = props => pug`
  .wrapper
    if props.shouldShowGreeting
      p.greeting Hello World!

    button(onClick=props.notify) Click Me
`

Will provide similar to this:

export default const ReactComponent = props => (
  <div className="wrapper">
    {props.shouldShowGreeting && (
      <p className="greeting">Hello World</p>
    )}
    <button onClick={props.notify}>Click Me</button>
  </div>
)

There are more examples in repository.

like image 184
ezhlobo Avatar answered Oct 07 '22 17:10

ezhlobo


You can use pug-as-jsx : https://github.com/bluewings/pug-as-jsx-loader

You will find steps to do it here : can i use pug (ex-jade) with react framework?

A blog about this : https://medium.com/nishantsblog/pug-templates-for-react-presentation-components-7610967954a

like image 33
Nishant Avatar answered Oct 07 '22 16:10

Nishant