Last active
February 28, 2022 02:32
-
-
Save johnpc/8d5740e91d980b8cf7324379ebd574a1 to your computer and use it in GitHub Desktop.
exec react component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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