npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-obj

v1.0.3

Published

represent DOM elements as objects in React, alternative to jsx

Downloads

16

Readme

react-obj

Represent DOM elements as objects in React, alternative to jsx.

Github repo

npm module

Motivation

The great part of React, compared to other UI frameworks, is that it extends javascript instead of attempting to extend HTML. react-obj takes this approach a step further by allowing components to be created with plain javascript objects allowing for a powerful and familiar development experience. Thus not needing XML, nor another file type.

Overview

One templating language

  //jsx
  <div>
    {props.text}
  <div>

  //react-obj
  transform({
    comp: 'div'
    children: `${props.children}`
  })

Programmatically compose components inline (map, conditionals, etc)

  const numbers = ['one', 'two', 'three']

  //jsx
  <div>
    { numbers.map(number =>
        <div key={number}>Counting to {number}</div>
    )}
  </div>

  //react-obj
  transform({
    comp: 'div',
    children: numbers.map(number => ({
      comp: 'div',
      key: number,
      children: `Counting to ${number}`
    })),
  })

Better use of es6 operators (spread, object literals)

  //jsx
  const Component = (props) => {
    const { text, value } = props

    return (
      <div>
        <div {...props}>
          Spread
        </div>
        <div text={text} value={value}>
          Object literals
        </div>
      </div>
    )
  }

  //react-obj
  const Alternative = (props) => {
    const { text, value } = props

    return transform({
      comp: 'div'
      children: [
        {
          comp: 'div',
          ...props,
          children: 'Spread',
        }
        {
          comp: 'div',
          text,
          value,
          children: 'Object literals'
        }
      ]
    })
  }

Further Considerations

  • Javascript everywhere, no XML
  • Better linting
  • One file type
  • Simple wrapper around React.createElement
  • Works with existing components written in jsx
  • Can use with Stateless Components, React createClass, and extending React.Component

Usage

Installing

npm install react-obj

Initializing

component must be initialized before using tranform

import { init } from 'react-obj'
import React from 'react'

init(React)

Documentation

Every object is required to have a comp property which represents the component that you want to render. The comp property can either be a string for standard elements(div, span, etc) or a react component. The other important property is children. The children property can either be a string you want rendered into the parent component, or it can be an array of further components to render. Every other property will be passed down as props.

comp: either a string('div', 'span') or a React Component

children: a string or an array of further compliant objects

Usage

import transform from 'react-obj'
//No need to import react if making a stateless component since react-obj will use internally

const StatelessComponent = ({ text }) => transform({
  comp: 'div',
  className: 'my-stateless-componet',
  children: text,
})

export default StatelessComponent
import transform from 'react-obj'
import React from 'react'
import StatelessComponent from './StatelessComponent'

//can be used similarly with React.createClass
class ExtendClassComponent extends React.Component {
  constructor(props) {
    super(props)
  }
  render() {
    return transform({
      comp: StatelessComponent,
      text: 'Written by extending React.component',
    })
  }
}

Future

  • allow users to set the key in the object that represents the component
  • Object validator
  • possibly add better method of hooking into react (class, mixin), thus not needing to call transform directly