Skip to content

Commit 22e2e5b

Browse files
committed
Prevent root from being recreated on react 18
1 parent c61c171 commit 22e2e5b

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

react_ujs/index.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,8 @@ var ReactRailsUJS = {
131131
if (hydrate && supportsHydration()) {
132132
component = reactHydrate(node, component);
133133
} else {
134-
const root = createReactRootLike(node)
134+
const root = this.findOrCreateRoot(node);
135135
component = root.render(component);
136-
if(supportsRootApi) {
137-
this.roots.push({"node": node, "root": root})
138-
}
139136
}
140137
}
141138
}
@@ -164,6 +161,32 @@ var ReactRailsUJS = {
164161
detectEvents(this)
165162
},
166163

164+
findOrCreateRoot: function(node) {
165+
var root = this.findRoot(node);
166+
if (!root) {
167+
root = createReactRootLike(node);
168+
if(supportsRootApi) {
169+
this.roots.push({"node": node, "root": root})
170+
}
171+
}
172+
173+
return root;
174+
},
175+
176+
findRoot: function(node) {
177+
if (!supportsRootApi) {
178+
return;
179+
}
180+
var rootElement = this.roots.find(
181+
function(rootElement) {
182+
return rootElement["node"] && (rootElement["node"] === node)
183+
}
184+
);
185+
if (rootElement) {
186+
return rootElement["root"];
187+
}
188+
},
189+
167190
unmountRoot: function(node) {
168191
var targetRoots = this.roots.filter(
169192
function(rootElement) {

0 commit comments

Comments
 (0)