Skip to content

Commit 8eb0a9e

Browse files
react 18 (createRoot, unmount)
1 parent 9eba975 commit 8eb0a9e

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

bundles/paikkatietoikkuna/coordinatetransformation/Flyout.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import ReactDOM from 'react-dom';
2+
import { createRoot } from 'react-dom/client';
33
import { FlyoutContent } from './view/FlyoutContent';
44
import { LocaleProvider, ThemeProvider } from 'oskari-ui/util';
55
import { Spin } from 'oskari-ui';
@@ -13,6 +13,7 @@ Oskari.clazz.define('Oskari.coordinatetransformation.Flyout',
1313
this.loc = Oskari.getMsg.bind(null, BUNDLE);
1414
this.container = null;
1515
this.handler = null;
16+
this._reactRoot = null;
1617
}, {
1718
getName: function () {
1819
return 'Oskari.coordinatetransformation.Flyout';
@@ -24,6 +25,7 @@ Oskari.clazz.define('Oskari.coordinatetransformation.Flyout',
2425
flyout.addClass(BUNDLE);
2526
this.flyout = flyout;
2627
this.container = el[0];
28+
this._reactRoot = createRoot(this.container);
2729
this.container.classList.add(BUNDLE);
2830
},
2931
getHandler: function () {
@@ -35,6 +37,9 @@ Oskari.clazz.define('Oskari.coordinatetransformation.Flyout',
3537
},
3638
teardown: function () {
3739
this.handler?.onFlyoutClose();
40+
if (this._reactRoot) {
41+
this._reactRoot.unmount();
42+
}
3843
},
3944
// For some screen sizes css + media doesn't give enough space for content
4045
setContainerMaxHeight: function (mapHeight) {
@@ -64,7 +69,7 @@ Oskari.clazz.define('Oskari.coordinatetransformation.Flyout',
6469
</ThemeProvider>
6570
</LocaleProvider>
6671
);
67-
ReactDOM.render(content, this.container);
72+
this._reactRoot.render(content);
6873
}
6974
}, {
7075
extend: ['Oskari.userinterface.extension.DefaultFlyout']

bundles/paikkatietoikkuna/inspire/Flyout.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import ReactDOM from 'react-dom';
2+
import { createRoot } from 'react-dom/client';
33
import { Message } from 'oskari-ui';
44

55
Oskari.clazz.define('Oskari.inspire.Flyout',
@@ -18,6 +18,7 @@ Oskari.clazz.define('Oskari.inspire.Flyout',
1818
},
1919
setEl: function (el, flyout, width, height) {
2020
this.container = el[0];
21+
this._reactRoot = createRoot(this.container);
2122
this.flyout = flyout;
2223
this.container.classList.add('inspire');
2324
this.flyout.addClass('inspire');
@@ -28,11 +29,10 @@ Oskari.clazz.define('Oskari.inspire.Flyout',
2829
* @method createContent
2930
*/
3031
createContent: function () {
31-
const root = this.container;
32-
if (!root) {
32+
if (!this._reactRoot) {
3333
return;
3434
}
35-
ReactDOM.render(<Message bundleKey={ this.instance.getName() } messageKey="flyoutContent.content" allowHTML={true} />, root);
35+
this._reactRoot.render(<Message bundleKey={ this.instance.getName() } messageKey="flyoutContent.content" allowHTML={true} />);
3636
},
3737
startPlugin: function () {}
3838
});

bundles/paikkatietoikkuna/mobileuserguide/instance.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import ReactDOM from 'react-dom';
2+
import { createRoot } from 'react-dom/client';
33
import { Message } from 'oskari-ui';
44
import { Link } from './Link';
55
import styled from 'styled-components';
@@ -55,7 +55,8 @@ Oskari.clazz.define('Oskari.pti.mobileuserguide.UserGuideBundleInstance',
5555
if (linksContainer) {
5656
const container = document.createElement('div');
5757
linksContainer.append(container);
58-
this.addLink(container);
58+
const reactRoot = createRoot(container);
59+
this.addLink(reactRoot);
5960
}
6061
this.addDesktopRequestLink();
6162
},
@@ -64,22 +65,22 @@ Oskari.clazz.define('Oskari.pti.mobileuserguide.UserGuideBundleInstance',
6465
return LINKS[Oskari.getLang()] || LINKS.fi;
6566
},
6667

67-
addLink: function (root) {
68-
ReactDOM.render(<div>
68+
addLink: function (reactRoot) {
69+
reactRoot.render(<div>
6970
<Link href={ this.getHref() }><Message bundleKey={ this.getName() } messageKey='title' /></Link>
70-
</div>, root);
71+
</div>);
7172
},
7273

7374
addDesktopRequestLink: function () {
7475
// generate root for React
75-
const linkContainer = document.createElement('div');
76+
const linkContainer = createRoot(document.createElement('div'));
7677
// attach under the disclaimer
7778
const disclaimerEl = document.getElementById('pti_disclaimer');
7879
disclaimerEl.after(linkContainer);
7980
// render the link
80-
ReactDOM.render(<DesktopLink href={ this.createLinkHref() }>
81+
linkContainer.render(<DesktopLink href={ this.createLinkHref() }>
8182
<Message bundleKey={ this.getName() } messageKey='requestDesktop' />
82-
</DesktopLink>, linkContainer);
83+
</DesktopLink>);
8384
},
8485
createLinkHref: function () {
8586
const url = new URL(window.location.href);

0 commit comments

Comments
 (0)