Skip to content

Instantly share code, notes, and snippets.

@megamaddu
Created May 31, 2018 18:33
Show Gist options
  • Save megamaddu/12725096894c8d95431cf0608a5b5b31 to your computer and use it in GitHub Desktop.
Save megamaddu/12725096894c8d95431cf0608a5b5b31 to your computer and use it in GitHub Desktop.
react-basic Ref component
"use strict";
var React = require("react");
var ReactDOM = require("react-dom");
exports.makeRef = function(toMaybe) {
var Ref = function(props) {
this.DOMNode = null;
return this;
};
Ref.prototype = Object.create(React.Component.prototype);
Ref.displayName = "Ref";
Ref.prototype.updateDOMNode = function() {
var newDOMNode = ReactDOM.findDOMNode(this);
if (this.DOMNode !== newDOMNode) {
this.DOMNode = newDOMNode;
this.forceUpdate();
}
};
Ref.prototype.componentDidMount = function() {
this.updateDOMNode();
};
Ref.prototype.componentDidUpdate = function() {
this.updateDOMNode();
};
Ref.prototype.render = function() {
return this.props.render(toMaybe(this.DOMNode));
};
return Ref;
};
module Lumi.Components.Molecules.Ref where
import Prelude
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
import React.Basic (JSX, ReactComponent)
import React.Basic.DOM.Events (DOMNode)
type RefProps =
{ render :: Maybe DOMNode -> JSX
}
foreign import makeRef :: (Nullable ~> Maybe) -> ReactComponent RefProps
ref :: ReactComponent RefProps
ref = makeRef toMaybe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment