vis-timeline icon indicating copy to clipboard operation
vis-timeline copied to clipboard

Using with react

Open stevenKirill opened this issue 2 years ago • 9 comments

Hello! Can I use this component timeline with React.js library?

stevenKirill avatar Jun 21 '22 07:06 stevenKirill

I tried to use this library with react and got this error, could anybody help me please? vis1 vis2 vis3 vis4

I guess it's coz of peer dependencies

stevenKirill avatar Jun 21 '22 09:06 stevenKirill

I'm using vis-timeline in my react-project without any problems. Moment.js is just a dependency and hasn't to be listed in your package.json file. It should be installed automatically by vis-timeline itself if you're using a normal packetmangager like npm. Just try to uninstall and install vis-timeline again.

RunRanger avatar Aug 07 '22 16:08 RunRanger

Could you share your code example please? How did you use it in react.js please

stevenKirill avatar Aug 08 '22 16:08 stevenKirill

I just see it now, you have to import from 'vis-timeline/standalone' and not 'vis-timeline'. if you use any interfaces for typescript you can use 'vis-timeline'.

My code:

import { useRef, useEffect } from "react";
import {
  Timeline as Vis,
  TimelineEventPropertiesResult,
} from "vis-timeline/standalone";
import createOptions from "./createOptions";
import MusicMetaData from "@types/MusicMetaData";

interface Props {
  musicData: MusicMetaData,
  onChangePosition: (positionInPercent: number) => void,
}

function Timeline({ musicData, onChangePosition }: Props) {
  const containerRef = useRef<HTMLDivElement | null>(null);
  const timelineRef = useRef<Vis | null>(null);

  const initTimeline = () => {
    if (!containerRef.current) return;
    timelineRef.current = new Vis(
      containerRef.current,
      [],
      [],
      createOptions({ musicData })
    );
    const timeline = timelineRef.current;
    timeline.addCustomTime(new Date(1920, 0, 1));
    timeline.setCustomTimeTitle("0:0");
    timeline.on("timechange", onTimeChange);
    timeline.on("click", onTimeChange);
  };

  const onTimeChange = (event: TimelineEventPropertiesResult) => {
    const time =
      event.time.getMinutes() * 60 +
      event.time.getSeconds() +
      event.time.getMilliseconds() / 1000;
    onChangePosition((time / musicData.duration) * 100);
  };

  useEffect(() => {
    if (!timelineRef.current) initTimeline();
  }, [containerRef]);

  return <div ref={containerRef} />;
}

export default Timeline;

RunRanger avatar Aug 09 '22 11:08 RunRanger

Great, Could you also send the implementation of createOptions and onChangePosition functions , I would like to understand how does this change your timeline

stevenKirill avatar Aug 13 '22 12:08 stevenKirill

onChangePosition is not relevant for vis or react. It's just a function as property to provide the position of the customMarker to other compontents/outer states. CreateOptions is just a function which returns the vis-options like you can see in any other sample from offical vis-homepage.

RunRanger avatar Aug 14 '22 21:08 RunRanger

Great, thank you very much) can you show me please the final version if it please?

stevenKirill avatar Aug 18 '22 16:08 stevenKirill

With this approach, writing and receiving to the state does not work. setState gets when exiting option
Screenshot 2022-10-24 at 14 27 35

KratosHome avatar Oct 24 '22 11:10 KratosHome

we can't do anything without exact error message and with the code snippet. We need more to help you.

RunRanger avatar Oct 31 '22 11:10 RunRanger