-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use a hook instead of a component to load Pyodide
- Loading branch information
1 parent
e0ee034
commit 335bd83
Showing
3 changed files
with
43 additions
and
45 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
export const usePyodide = () => { | ||
const [pyodide, setPyodide] = useState(null); | ||
|
||
useEffect(() => { | ||
let isActive = true; | ||
|
||
const loadPyodide = async () => { | ||
if (!window.pyodide) { | ||
const { loadPyodide: loadPyodideModule } = await import("https://cdn.jsdelivr.net/pyodide/v0.25.1/full/pyodide.mjs"); | ||
window.pyodide = await loadPyodideModule(); | ||
} | ||
if (isActive) { | ||
setPyodide(window.pyodide); | ||
} | ||
}; | ||
|
||
loadPyodide(); | ||
|
||
return () => { | ||
isActive = false; | ||
}; | ||
}, []); | ||
|
||
return pyodide; | ||
}; |