5

I'm trying to use React Horizontal Timeline in my react app but I'm getting that error (pointing to line # 379 of react-horizontal-timeline.js):

Uncaught TypeError: Cannot read property 'distance' of undefined

My code includes:

import React, { Component } from 'react';
import HorizontalTimeline from 'react-horizontal-timeline';

class Foo extends Component {
    state = {
        value : '01-01-1990',
        previous: 0
    };
    render(){
        const VALUES = ['20-04-1991'];
        return(){
            <div>
                <HorizontalTimeline values={VALUES} 
                    indexClick={(index) => {
                        this.setState({value: index, previous: this.state.value});
                    }} 
                />
                <div> {this.state.value} </div>
            </div>
        }
    }
}
export default Foo;

Can somebody please identify the real issue or alternatively suggest some good option for horizontal timeline for react?

1
  • Can this be used only with dates? is it possible to use it with strings instead of dates. I am just trying to use this package, getting all issues which you have faced, so wanted to know. Commented May 6, 2023 at 6:46

2 Answers 2

2

Changes:

1. You are returning 2 elements from render method, you need to wrap them in a div.

Check this answer for more explanation.

2. As per link you attached, values needs an array of dates with 'mm/dd/yyyy' format, but you are passing 'dd/mm/yyyy':

const VALUES = ['20-04-1991'];

Convert this to proper format:

const VALUES = ['04/20/1991'];

Try this:

render(){
        const VALUES = ['04/20/1991'];
        return(){
           <div>
               <HorizontalTimeline 
                  values={VALUES} 
                  indexClick={(index) => {
                      this.setState({value: index, previous: this.state.value});
                  }} 
               />
               <div className='text-center'>
                  {this.state.value}
               </div>
            </div>
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

I'm actually returning it inside a body div in my real code that I forgot adding in the snippet here, edited now. I also converted date format but nothing worked, still getting the same error.
but you removed the values from ques, are you using that in your code ??
Sorry didn't get, which values are you talking about?
Yeah the error discussed in the question has been resolved by these changes. But I'm unable to successfully make the timeline as discussed in that github issue.
1

There are two main issues in the snippet posted in the question.

See similar github issue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.