Coder Social home page Coder Social logo

react-omit's Introduction

react-omit

tl;dr: This package lets you omit the second argument (props) when using React.createElement() instead of JSX.

JSX (and XML in general) looks messy due closing tags and the attributes all being on one line. You can use React without JSX but React.createElement() hasn't been made very user-friendly.

After trying hyperscript, r-dom, and even trying to implement pug, I concluded that a shorthand for React.createElement is indeed the simplest, but that when no attributes are passed you're forced to put an empty object as the second argument. This little function removes that need.

As a bonus, combined with the new Coffeescript 2, you can omit the closing tags altogether. The result is the same syntax for all JS and JSX in a file, with easy to type code and clearer oversight.

It allows code like this:

import React from 'react'
import o from 'react-omit'
import { Grid, Row, Col } from 'react-flexbox-grid'

const Home = () =>
  o(Grid,
    o(Row,
      o(Col, {xs: 6},
        o('h2', 'All Users'),
        o('ul', props.users.map((user, key) =>
          o('li', {key: key},
            o('b', 'Name: '),
            user.name,
            o('br'),
            o('b', 'Email: '),
            user.email
          )
        ))
      ),
      o(Col, {xs: 6},
        o('h2', 'All Projects'),
        o('ul', props.projects.map((project, key) =>
          o('li', {key: key},
            o('b', 'Title: '),
            project.title,
            o('br'),
            o('b', 'Owner: '),
            project.owner
          )
        ))
      )
    )
  )

export default Home

Or, using Coffeescript 2, allows you to write without closing tags, which looks very much like HAML or jade / pug:

import React from 'react'
import o from 'react-omit'
import { Grid, Row, Col } from 'react-flexbox-grid'

const Home = () ->
  o Grid,
    o Row,
      o Col, {xs: 6},
        o 'h2', 'All Users'
        o 'ul', props.users.map (user, key) ->
          o 'li', {key: key},
            o 'b', 'Name: '
            user.name
            o 'br'
            o 'b', 'Email: '
            user.email
      o Col, {xs: 6},
        o 'h2', 'All Projects'
        o 'ul', props.projects.map (project, key) ->
          o 'li', {key: key},
            o 'b', 'Title: '
            project.title
            o 'br'
            o 'b', 'Owner: '
            project.owner

export default Home

Even better, since Coffeescript allows for objects to look like YML, you can do this:

import React from 'react'
import o from 'react-omit'

Loading = () ->
  o 'svg',
    width: 38
    height: 38
    viewBox: '0 0 38 38'
    xmlns: 'http://www.w3.org/2000/svg'
    stroke: '#00AA11'
    o 'g',
      fill: 'none'
      fillRule: 'evenodd'
      o 'g',
        transform: 'translate(1 1)'
        strokeWidth: 2
        o 'circle',
          strokeOpacity: '.5'
          cx: 18
          cy: 18
          r: 18
        o 'path',
          d: 'M36 18c0-9.94-8.06-18-18-18'
          o 'animateTransform',
            attributeName: 'transform'
            type: 'rotate'
            from: '0 18 18'
            to: '360 18 18'
            dur: '1s'
            repeatCount: 'indefinite'

export default Loading

To generate this:

<svg width="38" height="38" viewBox="0 0 38 38" xmlns="http://www.w3.org/2000/svg" stroke="#00AA11">
  <g fill="none" fill-rule="evenodd">
    <g transform="translate(1 1)" stroke-width="2">
      <circle stroke-opacity=".5" cx="18" cy="18" r="18"></circle>
      <path d="M36 18c0-9.94-8.06-18-18-18" transform="rotate(187.894 18 18)">
        <animateTransform attributeName="transform" type="rotate" from="0 18 18" to="360 18 18" dur="1s" repeatCount="indefinite"></animateTransform>
      </path>
    </g>
  </g>
</svg>

This code is super naive and would love your feedback and PR's ๐Ÿค 

react-omit's People

Contributors

pascalpixel avatar

Watchers

 avatar

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.