Comments (6)
Ah that's a good point. This repository was initially only "react-magic" and had no NPM package. The NPM package was added later, but only really designed for use from Node.js. So at the moment I'm building for two use cases:
- react-magic, ran in browsers, defines IN_BROWSER in the build (https://github.com/reactjs/react-magic/blob/master/gulpfile.js#L39-L41)
- HTMLtoJSX, ran in Node.js, does not define IN_BROWSER
I didn't think about the third use case of installing the NPM package and using that with frontend code, as honestly very few people use NPM for frontend at the moment. Perhaps I should make it do environmental detection instead (eg. typeof window !== 'undefined'
or similar) rather than using a variable.
from react-magic.
I think your Gulpfile is how I originally found how you used IN_BROWSER
. It seems reasonable to me to keep it as a flag, but then again I am comfortable using NPM for writing browser code. The nice part of a flag is Webpack can treat it like a #define
and remove the code that will never execute during compilation.
from react-magic.
Hey @Daniel15 & @ssorallen,
Your conversation on IN_BROWSER steered me in the right direction for usage with Webpack.
I have written a small library (not open sourcing until I can be sure I'm on the right track) which utilises this library for HTML -> JSX -> HTML interpretation in the browser. A simplified version of the "magic" is the following:
import Htmltojsx from 'htmltojsx';
import reactTools from 'react-tools';
const converter = new Htmltojsx({
createClass: false
});
function convertToJSX(standardHTMLStr) {
const ret = reactTools.transform(converter.convert(standardHTMLStr));
return eval(ret); // evil!!!
}
function insertExistingHTML(query) {
// find existing HTML domNode on a page,
// convert it to JSX and
// return it as React virtual dom.
return convertToJSX(document.querySelector(query).outerHTML);
}
I haven't been able to figure out a way of having React render the JSX created by the htmltojsx library without piping it through reactTools and doing an eval on the resulting JS. Do you know of a way to render the JSX at browser runtime without using eval? I am using babel for JSX conversion in my build, but this is not applicable when dynamically grabbing domNodes.
Sample usage below:
render() {
return <div>
{insertExistingHTML('.tpbc-logo')}
<div className="container">
{insertExistingHTML(
'header .container .navigationMain'
)}
{insertExistingHTML(
'header .container form'
)}
</div>
<Hamburger toggle={this.toggle} menu={this.state.menu} />
</div>;
}
from react-magic.
I haven't been able to figure out a way of having React render the JSX created by the htmltojsx library without piping it through reactTools and doing an eval on the resulting JS
Adding the ability to compile directly to React.createElement
calls would help with that, as you wouldn't need to deal with JSX at all. Issue #19 tracks that.
from react-magic.
That would be amazing! Are you working on this or is it something you need help with?
from react-magic.
#19 would negate having to document this. This is a rather old issue and not particularly useful, so I am closing it.
from react-magic.
Related Issues (20)
- Ability to preload links on hover, or expose enough of an API for users to implement. HOT 1
- Comparison to TurboLinks
- Getting hit with wall of errors HOT 3
- Convert hyphenated attributes into camelCase HOT 1
- [HtmlToJsx] View logic? F.e., how do we loop through items in an HTML view? HOT 2
- onclick attribute not getting renamed to onClick HOT 1
- Feature Request : Make this work in a web worker HOT 1
- HTMLtoJSX page is broken HOT 3
- HTML to JSX is broken HOT 5
- Autofocus isn't camel-cased HOT 2
- Change React.createClass to ES class
- feature request: serialize nodes and attributes to custom code
- HTMLtoJSX, better document default behavior
- SVG: incorrect gradientunits attribute
- complex style prop not parsed correctly
- White-space conversion on text nodes or inline elements HOT 2
- react 16 HOT 1
- Reactmagic site have "<" unexcpected error
- Add option to output Functional component (as well as import and export statement)
- ref value in react
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-magic.