Skip to content

Commit

Permalink
Merge pull request #520 from bloodyowl/dont-import-errors-in-react-dom
Browse files Browse the repository at this point in the history
Use Console.error instead of throwing so that ReactDOMRe doesn't import Caml_builtin_exceptions
  • Loading branch information
rickyvetter committed Apr 27, 2020
2 parents 9ea879c + 942ef0f commit e46c8a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
25 changes: 8 additions & 17 deletions lib/js/src/ReactDOMRe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,23 @@

var React = require("react");
var ReactDom = require("react-dom");
var Caml_builtin_exceptions = require("bs-platform/lib/js/caml_builtin_exceptions.js");

function renderToElementWithClassName(reactElement, className) {
var elements = document.getElementsByClassName(className);
if (elements.length !== 0) {
ReactDom.render(reactElement, elements[0]);
return /* () */0;
} else {
throw [
Caml_builtin_exceptions.invalid_argument,
"ReactDOMRe.renderToElementWithClassName: no element of class " + (className + " found in the HTML.")
];
console.error("ReactDOMRe.renderToElementWithClassName: no element of class " + (className + " found in the HTML."));
return /* () */0;
}
}

function renderToElementWithId(reactElement, id) {
var match = document.getElementById(id);
if (match == null) {
throw [
Caml_builtin_exceptions.invalid_argument,
"ReactDOMRe.renderToElementWithId : no element of id " + (id + " found in the HTML.")
];
console.error("ReactDOMRe.renderToElementWithId : no element of id " + (id + " found in the HTML."));
return /* () */0;
} else {
ReactDom.render(reactElement, match);
return /* () */0;
Expand All @@ -36,20 +31,16 @@ function hydrateToElementWithClassName(reactElement, className) {
ReactDom.hydrate(reactElement, elements[0]);
return /* () */0;
} else {
throw [
Caml_builtin_exceptions.invalid_argument,
"ReactDOMRe.hydrateToElementWithClassName: no element of class " + (className + " found in the HTML.")
];
console.error("ReactDOMRe.hydrateToElementWithClassName: no element of class " + (className + " found in the HTML."));
return /* () */0;
}
}

function hydrateToElementWithId(reactElement, id) {
var match = document.getElementById(id);
if (match == null) {
throw [
Caml_builtin_exceptions.invalid_argument,
"ReactDOMRe.hydrateToElementWithId : no element of id " + (id + " found in the HTML.")
];
console.error("ReactDOMRe.hydrateToElementWithId : no element of id " + (id + " found in the HTML."));
return /* () */0;
} else {
ReactDom.hydrate(reactElement, match);
return /* () */0;
Expand Down
30 changes: 12 additions & 18 deletions src/ReactDOMRe.re
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,21 @@ external _getElementById: string => option(Dom.element) =
let renderToElementWithClassName = (reactElement, className) =>
switch (_getElementsByClassName(className)) {
| [||] =>
raise(
Invalid_argument(
"ReactDOMRe.renderToElementWithClassName: no element of class "
++ className
++ " found in the HTML.",
),
Js.Console.error(
"ReactDOMRe.renderToElementWithClassName: no element of class "
++ className
++ " found in the HTML.",
)
| elements => render(reactElement, Array.unsafe_get(elements, 0))
};

let renderToElementWithId = (reactElement, id) =>
switch (_getElementById(id)) {
| None =>
raise(
Invalid_argument(
"ReactDOMRe.renderToElementWithId : no element of id "
++ id
++ " found in the HTML.",
),
Js.Console.error(
"ReactDOMRe.renderToElementWithId : no element of id "
++ id
++ " found in the HTML.",
)
| Some(element) => render(reactElement, element)
};
Expand All @@ -47,12 +43,10 @@ external hydrate: (React.element, Dom.element) => unit = "hydrate";
let hydrateToElementWithClassName = (reactElement, className) =>
switch (_getElementsByClassName(className)) {
| [||] =>
raise(
Invalid_argument(
"ReactDOMRe.hydrateToElementWithClassName: no element of class "
++ className
++ " found in the HTML.",
),
Js.Console.error(
"ReactDOMRe.hydrateToElementWithClassName: no element of class "
++ className
++ " found in the HTML.",
)
| elements => hydrate(reactElement, Array.unsafe_get(elements, 0))
};
Expand Down

0 comments on commit e46c8a5

Please sign in to comment.