-
I would like to listen to and trigger events inside the bundled code. Example: const code = `
export default function App() {
const { dispatch } = useSandpackEvents
const buttonClick = () => {
dispatch({ type: 'click', message: { text: 'Hello from code' }
}
return <button onClick={buttonClick}>Trigger event</button>
}`;
const SandpackListener = () => {
const { dispatch, listen, sandpack } = useSandpack();
useEffect(() => {
const unsubscribe = listen((msg) => {
//msg here could also be from code
});
return () => unsubscribe();
}, []);
return null;
};
<SandpackProvider
files={{
"/App.js": code,
}}
template="react"
>
<SandpackLayout>
<SandpackCodeEditor />
<SandpackListener />
<SandpackPreview showOpenInCodeSandbox={false} showRefreshButton={false} />
</SandpackLayout>
</SandpackProvider>; |
Beta Was this translation helpful? Give feedback.
Answered by
alexnm
Apr 6, 2021
Replies: 1 comment 1 reply
-
This would require a framework agnostic approach, as sandpack can run any frontend framework/code. I'm curious though, what would be your use case for this? You can theoretically make it work with the current implementation if you do a postMessage in your code towards the window.parent. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
nkovacic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This would require a framework agnostic approach, as sandpack can run any frontend framework/code. I'm curious though, what would be your use case for this? You can theoretically make it work with the current implementation if you do a postMessage in your code towards the window.parent.