Skip to content

Commit 6d2ed9b

Browse files
committed
refactor: Swap effect for input handler
1 parent 59cbbc0 commit 6d2ed9b

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/components/controllers/repl/index.jsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from 'preact/hooks';
1+
import { useState } from 'preact/hooks';
22
import { Splitter } from '../../splitter';
33
import { EXAMPLES, getExample, loadExample } from './examples';
44
import { ErrorOverlay } from './error-overlay';
@@ -42,18 +42,22 @@ export function Repl({ code, slug }) {
4242
});
4343
};
4444

45-
useEffect(() => {
45+
const onEditorInput = (value) => {
46+
setEditorCode(value);
47+
48+
// Clears the example & code query params when a user
49+
// begins to modify the code
50+
if (!exampleSlug || !location.search) return;
4651
const example = getExample(exampleSlug);
47-
(async function () {
48-
if (example) {
49-
const code = await loadExample(example.url);
50-
if (location.search && code !== editorCode) {
52+
if (example) {
53+
loadExample(example.url).then(exampleCode => {
54+
if (exampleCode !== value) {
5155
setExampleSlug('');
5256
history.replaceState(null, null, '/repl');
5357
}
54-
}
55-
})();
56-
}, [editorCode]);
58+
});
59+
}
60+
};
5761

5862
const share = () => {
5963
if (!exampleSlug) {
@@ -138,7 +142,7 @@ export function Repl({ code, slug }) {
138142
value={editorCode}
139143
baseExampleSlug={exampleSlug}
140144
error={error}
141-
onInput={setEditorCode}
145+
onInput={onEditorInput}
142146
/>
143147
</Splitter>
144148
</div>

0 commit comments

Comments
 (0)