Skip to content

EXAMPLE: Sending custom messages from Python to React-based widget #420

Answered by manzt
kolibril13 asked this question in JavaScript
Discussion options

You must be logged in to vote

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:

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 …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@manzt
Comment options

@manzt
Comment options

Answer selected by manzt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #418 on January 28, 2024 17:48.