question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React components?

See original GitHub issue

How do you handle React components (=function calls)? You could either have a registry for component names or do this:

class EchoComponent {
    ···
}
let comp = jsx`<${EchoComponent} />`;
React.renderComponent(comp, document.body);

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
niksycommented, Dec 30, 2016

For anyone looking for a “workaround” for this, I’ve managed to create a proof of concept. Basically, it uses function which returns another function in which we check first letter of tag element. If it’s uppercase, we treat it as component and evaluate that string to variable declaration.

This obviously only works if method is in scope and eval has access to defined variable. Maybe it could be made to work regardless of scope? It seems like this could be properly processed with hyperxify - if first letter is uppercased, don’t stringify tag name and just return original value.

Edit: it seems like proposal on processing has already been implemented in fork of hyperxify; maybe this could be somehow merged to original project?

function r ( h ) {
	return function ( tagName, attrs, children ) {
		if ( tagName[0].toLowerCase() !== tagName[0] ) {
			tagName = eval(tagName);
		}
		return h(tagName, attrs, children);
	}
}

const React = require('react');
const ReactDOM = require('react-dom');
const hyperx = require('hyperx');
const hx = hyperx(r(React.createElement));
const Slider = require('react-slider');

module.exports = React.createClass({
	render() {
		return hx`
			<div className='controls__toolbar__volume__slider'>
				<Slider value="${this.props.volume} withBars="${true}" />
			</div>`
		;
	}
});

Or example of a more complex implementation:

function r ( h ) {
	return function ( tagName, attrs, children ) {
		if ( tagName[0].toLowerCase() !== tagName[0] ) {
			tagName = eval(tagName);
		}
		return h(tagName, attrs, children);
	}
}

const React = require('react');
const ReactDOM = require('react-dom');
const hyperx = require('hyperx');
const hx = hyperx(r(React.createElement));

const Wrap = React.createClass({
	render: function () {
		return hx`<b>${this.props.children}</b>`;
	}
});

const Input = ( props ) => {
	return hx`<input type="text" defaultValue="${props.value}" class="${props.value}" onChange=${props.onChange} />`;
};

const App = React.createClass({
	getInitialState: function () {
		return {
			text: ''
		};
	},
	render: function () {
		return hx`
			<div>
				<span class="bar">
					<Wrap>${['foo','bar'].map(( value, index ) => {
						return hx`<Input value=${value} key="${index}" onChange=${this.handleChange} />`;
					})}
					</Wrap>
				</span>
				<b>${this.state.text}</b>
			</div>
		`;
	},
	handleChange: function ( ev ) {
		this.setState({
			text: ev.target.value
		});
	}
});
  
ReactDOM.render(React.createElement(App), document.querySelector('body'));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Components and Props
Components can refer to other components in their output. This lets us use the same component abstraction for any level of detail. A...
Read more >
React Components
Components are independent and reusable bits of code. They serve the same purpose as JavaScript functions, but work in isolation and return HTML....
Read more >
Absolutely Awesome React Components & Libraries
Absolutely Awesome React Components & Libraries. This is a list of AWESOME components. Nope, it's NOT a comprehensive list of every React component...
Read more >
ReactJS Components
A Component is one of the core building blocks of React. In other words, we can say that every application you will develop...
Read more >
MUI: The React component library you always wanted
MUI provides a simple, customizable, and accessible library of React components. Follow your own design system, or start with Material Design.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found