Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add context for data communication #61

Closed
wants to merge 2 commits into from
Closed

Conversation

lvisei
Copy link
Member

@lvisei lvisei commented Dec 13, 2024

import type { CodeBlockComponent } from '@antv/gpt-vis';
import { GPTVisLite, useGPTVisContext, withChartCode } from '@antv/gpt-vis';
import React, { useCallback, useMemo, useState } from 'react';

/**
 * 自定义代码块渲染器
 */
const MyUIRenderer: CodeBlockComponent = ({ children }) => {
  const context = useGPTVisContext();
  console.log('context: ', context);
  return null;
};
const customRenderers = { 'my-ui': MyUIRenderer };
const components = {
  code: withChartCode({
    languageRenderers: customRenderers, // register custom block renderer
  }),
};

export default () => {
  const [count, setCount] = useState(0);
  const handleClick = useCallback(() => {
    console.log('handleClick');
    setCount((pre) => pre + 1);
    // do something
  }, []);
  const context = useMemo(() => ({ count: count, onClick: handleClick }), [count]);

  return (
    <>
      <p>count: {count}</p>
      <GPTVisLite context={context} components={components}>
        {content}
      </GPTVisLite>
    </>
  );
};

Copy link

changeset-bot bot commented Dec 13, 2024

🦋 Changeset detected

Latest commit: 77e60a7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@antv/gpt-vis Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Dec 13, 2024

🎊 PR Preview 77e60a7 has been successfully built and deployed to https://antvis-GPT-Vis-preview-pr-61.surge.sh

🕐 Build time: 14.431s

🤖 By surge-preview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant