Skip to content

Instantly share code, notes, and snippets.

@retronav
Last active May 10, 2020 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retronav/95a5d5dd094372fe1bd89ef2092f7aa4 to your computer and use it in GitHub Desktop.
Save retronav/95a5d5dd094372fe1bd89ef2092f7aa4 to your computer and use it in GitHub Desktop.
Using TeddyTags in different enviornments

Using TeddyTags in different enviornments

TeddyTags can be used in any browser compatible with ES5+ features. Here is a small example targeting ES5 and ES6 enviornments:

  • ES5
    //# ES5 implementation of a class
    var App = function(props) {
      Component.call(props)
      this.props = props;
    };
    App.prototype.render = function (){
      return h("h1", null, "Hi, " + props.name || "");
    }
    new Tag({ name: "App", to: App });
    var el1 = h("h1", null, "Hi");
    render(el1, document.body.querySelector("root"));
    setTimeout(function() {
      document.body.appendChild(
        document.createRange().createContextualFragment("<App name='foo' />")
        //equivalent of document.body.innerHTML but preserves elements
      );
      render(h("h1", null, "Hello"), document.body.querySelector("root"));
    }, 1000);
    • ES6+
    class App extends Component {
      constructor(props){
        super(props)
      }
      render(){
        return h("h1", null, `Hi, ${props.name || ""}`);
      }
    }
    new Tag({ name: "App", to: App });
    var el1 = h("h1", null, "Hi");
    render(el1, document.body.querySelector("root"));
    setTimeout(() => {
      document.body.appendChild(
        document.createRange().createContextualFragment("<App name='foo' />")
        //equivalent of document.body.innerHTML but preserves elements
      );
      render(h("h1", null, "Hello"), document.body.querySelector("root"));
    }, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment