Skip to content

Commit

Permalink
RFC: use simpler constructs for createRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodyowl committed Apr 28, 2020
1 parent 4e28433 commit 67a2e13
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
11 changes: 5 additions & 6 deletions lib/js/src/ReactDOMRe.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

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

function renderToElementWithClassName(reactElement, className) {
Expand Down Expand Up @@ -30,18 +30,17 @@ function renderToElementWithId(reactElement, id) {
function createRootWithClassName(className) {
var elements = document.getElementsByClassName(className);
if (elements.length !== 0) {
return /* Ok */Block.__(0, [ReactDom.createRoot(elements[0])]);
} else {
return /* Error */Block.__(1, ["ReactDOMRe.Unstable.createRootWithClassName: no element of class " + (className + " found in the HTML.")]);
return Caml_option.some(ReactDom.createRoot(elements[0]));
}

}

function createRootWithId(id) {
var match = document.getElementById(id);
if (match == null) {
return /* Error */Block.__(1, ["ReactDOMRe.Unstable.createRootWithId: no element of id " + (id + " found in the HTML.")]);
return ;
} else {
return /* Ok */Block.__(0, [ReactDom.createRoot(match)]);
return Caml_option.some(ReactDom.createRoot(match));
}
}

Expand Down
18 changes: 4 additions & 14 deletions src/ReactDOMRe.re
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,14 @@ module Experimental = {

let createRootWithClassName = className =>
switch (_getElementsByClassName(className)) {
| [||] =>
Error(
"ReactDOMRe.Unstable.createRootWithClassName: no element of class "
++ className
++ " found in the HTML.",
)
| elements => Ok(createRoot(Array.unsafe_get(elements, 0)))
| [||] => None
| elements => Some(createRoot(Array.unsafe_get(elements, 0)))
};

let createRootWithId = id =>
switch (_getElementById(id)) {
| None =>
Error(
"ReactDOMRe.Unstable.createRootWithId: no element of id "
++ id
++ " found in the HTML.",
)
| Some(element) => Ok(createRoot(element))
| None => None
| Some(element) => Some(createRoot(element))
};
};

Expand Down

0 comments on commit 67a2e13

Please sign in to comment.