Skip to content

Commit cdb8fa0

Browse files
refactor: restructure the method to achieve better readability
1 parent 57bf5cb commit cdb8fa0

5 files changed

Lines changed: 40 additions & 39 deletions

File tree

lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/tyk-ui.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/tyk-ui.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Toast/index.js

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,13 @@ const VALID_ALIGN = ["right", "center"];
2020
*/
2121
class ToastCreator {
2222
constructor() {
23-
this.defaultOptions = {};
24-
this.themeOptions = {};
25-
this.placement = { from: 'bottom', align: 'center' };
26-
27-
const el = document.createElement('div');
28-
el.className = 'tyk-toast';
29-
this.el = el;
30-
document.body.appendChild(this.el);
31-
this.root = createRoot(this.el);
32-
33-
this.renderContainer();
23+
this._setupDefaults();
24+
this._initializeContainer();
3425
}
3526

36-
renderContainer() {
37-
this.root.render(
38-
<ToastContainer
39-
placement={this.placement}
40-
notify={this.bindNotify}
41-
/>
42-
);
43-
}
44-
45-
bindNotify = (fn) => {
46-
this.createNotification = fn;
47-
};
4827

4928
configure(options) {
50-
options = options || {}
29+
options = options ?? {}
5130
const { themes = {}, general = {} } = options
5231

5332
this.defaultOptions = { ...this.defaultOptions, ...general }
@@ -80,13 +59,14 @@ class ToastCreator {
8059
}
8160

8261
if (shouldRerender) {
83-
// Using setTimeout to avoid the React "triggering nested component updates" warning
62+
// Using setTimeout to ensure the re-render happens after the current React execution context
63+
// to avoid "nested update" warnings.
8464
setTimeout(() => this.renderContainer(), 0);
8565
}
8666
}
8767

8868
notify(message, options) {
89-
options = options || {};
69+
options = options ?? {};
9070

9171
const themeDefaults = this.themeOptions[options.theme] || {}
9272
const finalOptions = { ...this.defaultOptions, ...themeDefaults, ...options };
@@ -113,20 +93,41 @@ class ToastCreator {
11393
}
11494

11595
[RESET]() {
116-
this.defaultOptions = {};
117-
this.themeOptions = {};
118-
this.placement = { from: 'bottom', align: 'center' };
119-
12096
if (this.root) {
12197
this.root.unmount();
12298
if (this.el) this.el.remove();
12399
}
124100

125-
const newEl = document.createElement('div');
126-
newEl.className = 'tyk-toast';
127-
this.el = newEl;
128-
document.body.appendChild(newEl);
129-
this.root = createRoot(newEl);
101+
this._setupDefaults();
102+
this._initializeContainer();
103+
}
104+
105+
renderContainer() {
106+
this.root.render(
107+
<ToastContainer
108+
placement={this.placement}
109+
notify={this.bindNotify}
110+
/>
111+
);
112+
}
113+
114+
bindNotify = (fn) => {
115+
this.createNotification = fn;
116+
};
117+
118+
_setupDefaults() {
119+
this.defaultOptions = {};
120+
this.themeOptions = {};
121+
this.placement = { from: 'bottom', align: 'center' };
122+
}
123+
124+
_initializeContainer() {
125+
const el = document.createElement('div');
126+
el.className = 'tyk-toast';
127+
this.el = el;
128+
document.body.appendChild(this.el);
129+
this.root = createRoot(this.el);
130+
130131
this.renderContainer();
131132
}
132133
}

0 commit comments

Comments
 (0)