Coder Social home page Coder Social logo

Document "IN_BROWSER" flag about react-magic HOT 6 CLOSED

reactjs avatar reactjs commented on May 2, 2024
Document "IN_BROWSER" flag

from react-magic.

Comments (6)

Daniel15 avatar Daniel15 commented on May 2, 2024

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:

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.

ssorallen avatar ssorallen commented on May 2, 2024

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.

mummybot avatar mummybot commented on May 2, 2024

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.

Daniel15 avatar Daniel15 commented on May 2, 2024

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.

mummybot avatar mummybot commented on May 2, 2024

That would be amazing! Are you working on this or is it something you need help with?

from react-magic.

ssorallen avatar ssorallen commented on May 2, 2024

#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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.