• This repository has been archived on 13/Aug/2020
  • Stars
    star
    230
  • Rank 174,053 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A concise, expressive way to build React DOM in pure JavaScript.

Build Status

JSnoX

Enjoy React.js, but not a fan of the JSX? JSnoX gives you a concise, expressive way to build ReactElement trees in pure JavaScript.

Works with

  • React.js v0.12 and above
  • React Native

Example

var React = require('react')
var MyOtherComponent = require('./some/path.js')
var d = require('jsnox')(React)

var LoginForm = React.createClass({
    submitLogin: function() { ... },

    render: function() {
        return d('form[method=POST]', { onSubmit: this.submitLogin },
            d('h1.form-header', 'Login'),
            d('input:email[name=email]'),
            d('input:password[name=pass]'),
            d(MyOtherComponent, { myProp: 'foo' }),
            d('button:submit', 'Login')
        )
    }
})

API

// Create a function, d, that parses spec strings into React DOM:
var React = require('react')
var ReactDOM = require('react-dom')
var d = require('jsnox')(React)

// The function returned by JSnoX takes 3 arguments:
// specString (required)    - Specifies the tagName and (optionally) attributes
// props (optional)         - Additional props (can override output from specString)
// children (optional)      - String, or an array of ReactElements
var myDom = d('div.foo', {}, 'hello')

ReactDOM.render(myDom, myElement)  // renders <div class="foo">hello</div>

JSnoX's specStrings let you specify your components' HTML in a way resembling CSS selectors:

spec strings

Each property referenced in the string is passed along in the props argument to React.createElement(). You can pass along additional props in the second argument (a JavaScript object). jsnox will merge the className attribute from both arguments automatically, useful if the element has a mix of static and dynamic classes.

Bonus features

  • append a ^ to your specString to have a key prop automatically generated from the spec string. This can help when you have dynamic children where they all have unique specStrings, eg:
render() {
    return d('ul',
        // The ^ suffix below will give each <li> a unique key:
        categories.map(cat => d(`li.category.${cat.id}^`, cat.title))
    )
}
  • you can add '@foo' to a specString to point a ref named foo to that element:
  // in render():
  return d('input:email@emailAddr')

  // elsewhere in the component (after rendering):
  var email = this.refs.emailAddr.value
  • You can pass a special $renderIf prop to your components or DOM elements. If it evaluates to false, the element won't be rendered:
  // in render():
  return d('div.debugOutput', { $renderIf: DEV_MODE }, 'hi')

Install

npm install jsnox

Npm is the recommended way to install. You can also include jsnox.js in your project directly and it will fall back to exporting a global variable as window.jsnox.

Why this instead of JSX?

  • No weird XML dialect in the middle of your JavaScript
  • All your existing tooling (linter, minifier, editor, etc) works as it does with regular JavaScript
  • No forced build step

Why this instead of plain JS with React.DOM?

  • More concise code; specify classes/ids/attributes in a way similar to CSS selectors
  • Use your custom ReactComponent instances on React 0.12+ without needing to wrap them with React.createFactory() everywhere

Notes/gotchas

  • Your top-level component should also be wrapped by the jsnox client, to prevent warnings about createFactory. For example:

    var d = require('jsnox')(React)
    
    // Good:
    React.render(d(MyTopLevelComponent, { prop1: 'foo'}), document.body)
    
    // Bad (will trigger a warning, and break in future React versions):
    React.render(MyTopLevelComponent({ prop1: 'foo'}), document.body)
  • All attributes you specify should be the ones that React understands. So, for example, you want to type 'input[readOnly]' (camel-cased), instead of 'readonly' like you'd be used to with html.

  • JSnoX gives you a saner default type for button elements– unless you specify 'button:submit' their type will be "button" (unintentionally form-submitting buttons is a personal pet peeve).

See also

More Repositories

1

envalid

Environment variable validation for Node.js
TypeScript
1,357
star
2

djangbone

Simple Django backends for Backbone.js apps.
Python
151
star
3

hyperpaper-planner

Dayplanner pdf for large e-readers (eg Remarkable 2, Supernote, Kindle Scribe, Boox)
70
star
4

apollo-local-query

Simpler server rendering with apollo-client 1.x, using a local GraphQL networkInterface
JavaScript
65
star
5

backprop

Use ES5 properties with Backbone. Goodbye get() and set()!
JavaScript
51
star
6

bs-d3

Experimental d3 5.x bindings for BuckleScript
Reason
43
star
7

dotfiles

Just my dotfiles - neovim, tmux, hammerspoon
Perl
34
star
8

shmup.re

Learning Reason/OCaml by making an old-school canvas game.
OCaml
24
star
9

af.github.com

Silly little personal site
TypeScript
11
star
10

shamus

A HUD for project automation.
JavaScript
5
star
11

garbagepatch.js

Bookmarklet that reports on the global variables on a page.
JavaScript
4
star
12

HMR-time

Experiments with webpack's Hot Module Replacement
JavaScript
2
star
13

openra_chart

JavaScript
2
star
14

js1k_harness

A collection of command-line tools for developing JS1K submissions.
JavaScript
2
star
15

turrentine

A very simple CMS for django with a different take on WYSIWYG editing. See the README for more details.
Python
2
star
16

canvasulative

A fun little canvas game, based on http://www.guylima.com/cumulative/
JavaScript
1
star
17

wiseau

A build tool for client-side javascript.
JavaScript
1
star
18

test-ts-action

TypeScript
1
star
19

promises-talk

JavaScript
1
star
20

faction

Wrangle your flux/redux actions
JavaScript
1
star
21

votesplit2011

How did "vote-splitting" affect the 2011 Canadian election?
JavaScript
1
star
22

react-talk

Lunch & learn talk on React.js
JavaScript
1
star
23

tap-extras

Wraps the tap-parser npm module to also parse extra lines (stack traces, etc)
JavaScript
1
star