@cosmograph/react

中文文档
2.5.1发布于 2026/08/15CC-BY-NC-4.0TypeScriptES Module近 30 天下载 5.8万

Cosmograph React Wrapper

原始使用文档
文档目录10 节
  1. ✨ Features
  2. 🚀 Quick Start
  3. CosmographProvider
  4. useCosmograph
  5. Refs
  6. 🌙 Available Components
  7. 🛸 Issues and Contributing
  8. 💫 License
  9. 🌌 Cosmograph Universe
  10. 👩🏻‍🚀 Contacts

🪐 Cosmograph React

npm version license GitHub Issues Discord Docs

User-friendly library for integrating @cosmograph/cosmograph into your React applications. Ready-made components and providers make it straightforward to ship interactive knowledge graph explorers—large entity–relationship networks with search, legends, and cross-filtering—alongside semantic maps from machine learning embeddings, in 2D or 3D, without reimplementing layout or WebGL yourself.

✨ Features

  • Knowledge-graph friendly: compose the graph with search, selection, legends, histograms, and timelines for entity–relationship workloads at scale
  • 2D and 3D GPU force layout via spaceDimensions, with an orbit camera in 3D and optional Z coordinates for embeddings
  • Simple React component API for the full power of Cosmograph
  • Ready-to-use interactive components: Timeline, Histogram, Bars, Legends, Search and even more
  • React hooks for accessing the Cosmograph instance and preparing raw data
  • Full TypeScript support
  • Context provider for sharing Cosmograph state

🚀 Quick Start

Install the package:

npm install @cosmograph/react

Get the data, configure the graph and render Cosmograph:

import React, { useEffect, useState } from 'react'
import { Cosmograph, CosmographConfig, prepareCosmographData } from '@cosmograph/react'

// Points and links data can be:
// - Array of objects
// - File (.csv/.tsv, .parquet/.pq, .arrow, .json)
// - URL string to a file
// - Apache Arrow Table (binary data Uint8Array/ArrayBuffer)
// - DuckDB table name if connection is passed into the Cosmograph constructor

const rawPoints = [{ id: 'a' }, { id: 'b' }, { id: 'c' }]

const rawLinks = [
  { source: 'a', target: 'b' },
  { source: 'b', target: 'c' },
  { source: 'c', target: 'a' },
]

export const component = (): JSX.Element => {
  const [config, setConfig] = useState<CosmographConfig>({})

  // Create a config to map your data into Cosmograph's internal format
  const dataConfig = {
    points: {
      pointIdBy: 'id',
    },
    links: {
      linkSourceBy: 'source',
      linkTargetsBy: ['target'],
    },
  }

  const loadData = async (): Promise<void> => {
    // Prepare data and config for Cosmograph
    const result = await prepareCosmographData(dataConfig, rawPoints, rawLinks)

    if (result) {
      // Update Cosmograph config with prepared output
      const { points, links, cosmographConfig } = result
      setConfig({ points, links, ...cosmographConfig })
    }
  }

  // Load data when component is mounted
  useEffect(() => { loadData() }, [])

  return <Cosmograph {...config} />
}

CosmographProvider

CosmographProvider connects Cosmograph instance with companion components like CosmographTimeline and CosmographHistogram. Wrap your components with it to enable this connection:

import { CosmographProvider, CosmographHistogram, Cosmograph } from '@cosmograph/react'

function App() {
  ...
  return (
    <CosmographProvider>
      <Cosmograph {...config} />
      // Assuming you have a numeric column "value" in your points data
      <CosmographHistogram accessor={"value"}>
    </CosmographProvider>
  )
}

⚠️ Note: Always place Cosmograph companion components inside CosmographProvider and make sure it has Cosmograph instance inside.

useCosmograph

After surrounding your app with CosmographProvider, the useCosmograph hook can now be used to access the Cosmograph instance.

import { useCosmograph, CosmographProvider } from '@cosmograph/react'

const FitButton = () => {
  const { cosmograph } = useCosmograph()
  
  return (
    <button onClick={() => cosmograph.fitView()}>
      Fit View
    </button>
  )
}

const App = () => {
  ...
  return (
    <CosmographProvider>
      <Cosmograph {...config}/>
      <FitButton />
    </CosmographProvider>
  )
}

❗ Note: useCosmograph must be called within a component that is a direct descendant of the CosmographProvider. Attempting to use it outside of the provider will trigger an error.

Refs

Cosmograph exposes a ref property that can be used to access the underlying Cosmograph instance, giving you direct access to its methods and properties.

import React, { useRef } from 'react'
import { CosmographRef, Cosmograph } from '@cosmograph/react'

function App () {
  ...
  const cosmograph = useRef<CosmographRef>()

  const onClick = (pointIndex: number | undefined) => {
    cosmograph.current?.selectPoint(point)
  }}

  return (
    <Cosmograph {...config} onClick={onClick} ref={cosmograph}/>
  )
}

🌙 Available Components

@cosmograph/react provides a set of components connected to the Cosmograph instance and ready for use:

✏️ Note: All the components included support refs and can be accessed via useRef or useCallback for developers to have better control and management. Check out Cosmograph React docs for more details.

🛸 Issues and Contributing

While currently closed source, we welcome your feedback! Help improve Cosmograph by submitting bug reports and feature ideas in our issues repository.

💫 License

Cosmograph is licensed under the CC-BY-NC-4.0 license and free for any non-commercial usage. If you want to use it in a commercial project, please reach out to us.

🌌 Cosmograph Universe

@cosmograph/cosmograph - Cosmograph library for JavaScript and TypeScript
py_cosmograph - Jupyter widget for data science workflows in Python

👩🏻‍🚀 Contacts

🌎 cosmograph.app
📩 [email protected]
👾 Cosmograph Discord channel

版本与发布记录

@cosmograph/react 共发布 76 个版本,首次发布于 2023/07/04;过去 12 个月发布了 21 个版本。

版本发布日期距今
2.5.1latest2026/08/152 个月前
2.5.02026/08/112 个月前
2.4.12026/07/233 个月前
2.4.02026/07/223 个月前
2.4.0-beta.4beta2026/07/223 个月前
2.3.32026/07/163 个月前

关于 @cosmograph/react 的常见问题

@cosmograph/react 是什么?

Cosmograph React Wrapper

如何安装 @cosmograph/react?

在项目目录运行 npm install @cosmograph/react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.8.0 || ^17 || ^18)、react-dom(>=16.8.0 || ^17 || ^18)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cosmograph/react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cosmograph/react 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cosmograph/react 可以商用吗?

@cosmograph/react 使用 CC-BY-NC-4.0 许可。声明的许可为 CC-BY-NC-4.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@cosmograph/react 的下载量有多少?

根据 npm 官方下载统计,@cosmograph/react 在 2026/10/11 – 2026/11/09 这 30 天内被下载 58,199 次,日均约 1,940 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cosmograph/react 还在维护吗?

最新版本 2.5.1 发布于 2026/08/15(2 个月前);至今共发布 76 个版本,其中过去 12 个月发布了 21 个;首次发布于 2023/07/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。