Skip to content

Instantly share code, notes, and snippets.

@johnpc
Last active February 28, 2022 02:32
Show Gist options
  • Save johnpc/8d5740e91d980b8cf7324379ebd574a1 to your computer and use it in GitHub Desktop.
Save johnpc/8d5740e91d980b8cf7324379ebd574a1 to your computer and use it in GitHub Desktop.
exec react component
import {useEffect, useState} from "react";
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const move = async () => {
console.log(`Moving at ${Date.now()}`);
await sleep(1000);
};
const runCode = (code) => {
eval(`const fun = async() => {${code.replaceAll('move()', 'await move()')}}; fun();`);
};
function App() {
// move(); move();
const [workingCode, setWorkingCode] = useState("");
return (
<div className="App">
<input onChange={(e) => setWorkingCode(e.target.value)}></input>
<button onClick={() => runCode(workingCode)}>Run Code</button>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment