Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
React 18 Suspense does not seem to work with our router
  • Loading branch information
giuseppedeponte committed Jan 23, 2024
1 parent 2a404ac commit 992704b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
6 changes: 1 addition & 5 deletions agir/donations/components/donationForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ function getChoices(select) {
return choices;
}

const render = (widget, element) => {
renderReactComponent(widget, element);
};

const replaceForm = (selector) => {
const form = document.querySelector(selector);
if (!form) {
Expand Down Expand Up @@ -89,7 +85,7 @@ const replaceForm = (selector) => {
form.parentNode.removeChild(form);
// remove all children of the form

render(<DonationForm {...props} />, reactDiv);
renderReactComponent(<DonationForm {...props} />, reactDiv);
};

onDOMReady(() => replaceForm("form.donation-form"));
1 change: 0 additions & 1 deletion agir/front/components/app/Footer/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import PropTypes from "prop-types";
import React, { Suspense, useMemo } from "react";
import { useLocation } from "react-router-dom";

Expand Down
23 changes: 17 additions & 6 deletions agir/lib/components/utils/react.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isPropValid from "@emotion/is-prop-valid";
import ReactDOM from "react-dom";
import { createRoot } from "react-dom/client";
import React, { useState } from "react";
import PropTypes from "prop-types";

Expand Down Expand Up @@ -73,10 +73,21 @@ RootComponent.propTypes = {
fieldProps: PropTypes.object,
};

export const renderReactComponent = (component, node) => {
node &&
ReactDOM.render(<React.StrictMode>{component}</React.StrictMode>, node);
};
export const renderReactComponent = (
(roots = []) =>
(component, node) => {
if (!node) {
return;
}
let root = roots.find((r) => r.node === node)?.root;
if (!root) {
root = createRoot(node);
roots.push({ root, node });
}

root.render(<React.StrictMode>{component}</React.StrictMode>);
}
)();

export const mergeRefs = (...refs) => {
const filteredRefs = refs.filter(Boolean);
Expand All @@ -96,4 +107,4 @@ export const mergeRefs = (...refs) => {
export const validProps = (props) =>
Object.entries(props)
.filter(([k]) => isPropValid(k))
.reduce((o, [k, v]) => ({ ...o, k: v }), {});
.reduce((o, [_k, v]) => ({ ...o, k: v }), {});
5 changes: 5 additions & 0 deletions jest/setup.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<<<<<<< Updated upstream
import "@testing-library/jest-dom/jest-globals";
=======
import "@testing-library/jest-dom/extend-expect";
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
>>>>>>> Stashed changes

0 comments on commit 992704b

Please sign in to comment.