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

useNetwork crashes #2662

Open
skimchiebay opened this issue Oct 21, 2024 · 1 comment
Open

useNetwork crashes #2662

skimchiebay opened this issue Oct 21, 2024 · 1 comment

Comments

@skimchiebay
Copy link

skimchiebay commented Oct 21, 2024

I've made the most basic useNetwork implementation and I get an error.
There is no documentation or example for using useNetwork

This is my code:

import React from 'react';
import { ResponsiveNetwork } from '@nivo/network';
import { useNetwork } from '@nivo/network';

const data = {
  nodes: [{ id: 'A' }, { id: 'B' }, { id: 'C' }],
  links: [
    { source: 'A', target: 'B', distance: 100 },
    { source: 'B', target: 'C', distance: 100 },
    { source: 'C', target: 'A', distance: 100 },
  ],
};

const MyNetworkChart = () => {
  const { network, nodes, links } = useNetwork({
    nodes: data.nodes,
    links: data.links,
    width: 400,
    height: 400,
  });

  console.log({ network, nodes, links });
  return (
    <div style={{ width: '100%', height: 400 }}>
      <ResponsiveNetwork
        nodes={nodes}
        links={links}        
        nodeColor={(node) => 'blue'}
        linkColor={(link) => 'grey'}
      />
    </div>
  );
};

export default MyNetworkChart;

This is the error:
react-dom.development.js:11102 Uncaught TypeError: Cannot read properties of undefined (reading '0') at F.oe.linkDistance (hooks.ts:43:1) at F (hooks.ts:66:1)

@plouc
Copy link
Owner

plouc commented Oct 23, 2024

You don't need to use the useNetwork hook in order to use ResponsiveNetwork, it's already used internally, so you should just pass the nodes and links as is to the data property, the way it's done for https://nivo.rocks/network/.

useNetwork can be used if you want to completely render things by yourself, but yes, there's no documentation about it, so you'll need to inspect what the hook returns, and you'd probably want to have a look at the default implementation.

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

No branches or pull requests

3 participants
@plouc @skimchiebay and others