Represent DOM elements as objects in React, alternative to jsx.
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.
//jsx<div>{props.text}<div>
//react-obj
transform({comp: 'div'children: `${props.children}`})constnumbers=['one','two','three']//jsx<div>{ numbers.map(number=><divkey={number}>Counting to {number}</div>)}
</div>//react-objtransform({comp: 'div',children: numbers.map(number=>({comp: 'div',key: number,children: `Counting to ${number}`})),})//jsxconstComponent=(props)=>{const{ text, value }=propsreturn(<div><div{...props}>
Spread
</div><divtext={text}value={value}>
Object literals
</div></div>)}//react-objconstAlternative=(props)=>{const{ text, value }=propsreturntransform({comp: 'div'children: [{comp: 'div',
...props,children: 'Spread',}{comp: 'div',
text,
value,children: 'Object literals'}]})}- 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
npm install react-obj
component must be initialized before using tranform
import{init}from'react-obj'importReactfrom'react'init(React)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
importtransformfrom'react-obj'//No need to import react if making a stateless component since react-obj will use internallyconstStatelessComponent=({ text })=>transform({comp: 'div',className: 'my-stateless-componet',children: text,})exportdefaultStatelessComponentimporttransformfrom'react-obj'importReactfrom'react'importStatelessComponentfrom'./StatelessComponent'//can be used similarly with React.createClassclassExtendClassComponentextendsReact.Component{constructor(props){super(props)}render(){returntransform({comp: StatelessComponent,text: 'Written by extending React.component',})}}- 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