EXAMPLE: Sending custom messages from Python to React-based widget #420
Answered
by
manzt
kolibril13
asked this question in
JavaScript
-
Beta Was this translation helpful? Give feedback.
Answered by
manzt
Jan 28, 2024
Replies: 1 comment 2 replies
-
I transferred the issue to a discussion because this is for reference and not a bug/issue with anywidget. I would just include the relevant imports so that it is reproducible with copy-paste. The import * as React from "react";
import { createRender, useModel } from "@anywidget/react";
export const render = createRender(() => {
const model = useModel();
React.useEffect(() => {
function handle_custom_msg(msg) {
console.log(msg);
}
model.on("msg:custom", handle_custom_msg);
return () => model.off("msg:custom", handle_custom_msg);
}, [model]);
return <p>Open the console to see custom messages</p>
}); I also find that import * as React from "react";
import { createRender, useModel } from "@anywidget/react";
function useCustomMessageLogging(filter = () => true) {
let model = useModel();
React.useEffect(() => {
let handle_custom_msg = (msg) => filter(msg) && console.log(msg);
// subscribe the handler
model.on("msg:custom", handle_custom_msg);
// cleanup logic if model/filter change or component is unmounted
return () => model.off("msg:custom", handle_custom_msg);
}, [model, filter, event]);
}
export const render = createRender(() => {
useCustomMessageLogging();
return <p>Open the console to see custom messages</p>
}); |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
manzt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I transferred the issue to a discussion because this is for reference and not a bug/issue with anywidget.
I would just include the relevant imports so that it is reproducible with copy-paste. The
value
isn't really relevant to sending the custom message:I …