question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue related to passing a ref prop (property 'slider' does not exist on type 'xyz')

See original GitHub issue

So I’ve been trying to pass refs to the slider component and yet had no luck. I found a similar issue #1261 which lead me to this file and with the code example

https://github.com/akiran/react-slick/blob/c8d24263517273ae86b982cf783cd4e05ec0be08/examples/AutoPlayMethods.js#L28

This is how my component looks like

<Slider
    ref={slider => (this.slider = slider)}
    {...carouselSettings}
    className={fade ? 'slick-fade' : null}
>
    {children}
</Slider>

and here is the issue I’m having

ERROR in [at-loader] ./src/components/molecules/Carousel/index.tsx:53:38 
TS2339: Property 'slider' does not exist on type 'Carousel'.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

7reactions
katia-aacommented, Aug 20, 2020

This is great! thanks a lot. You can also achieve the same thing with Function Components and Hooks:

import React from 'react'
import Slider from 'react-slick'

export const PreviousNextMethods = () => {
  const slider = React.useRef<Slider>(null)

  const settings = {
    infinite: true,
    speed: 500,
    slidesToShow: 1,
    slidesToScroll: 1,
  }

  return (
    <div>
      <h2>Previous and Next methods</h2>
      <Slider ref={slider} {...settings}>
        <div key={1}>
          <h3>1</h3>
        </div>
        <div key={2}>
          <h3>2</h3>
        </div>
        <div key={3}>
          <h3>3</h3>
        </div>
        <div key={4}>
          <h3>4</h3>
        </div>
        <div key={5}>
          <h3>5</h3>
        </div>
        <div key={6}>
          <h3>6</h3>
        </div>
      </Slider>
      <div style={{ textAlign: 'center' }}>
        <button className="button" onClick={() => slider?.current?.slickPrev()}>
          Previous
        </button>
        <button className="button" onClick={() => slider?.current?.slickNext()}>
          Next
        </button>
      </div>
    </div>
  )
}
4reactions
ummahuslacommented, Dec 11, 2018

Sorted it by slider: ISlider; and binding this.slider = React.createRef(); in the constructor. Then I just passed ref={this.slider} to the slider component as a prop. To pass the typescript issue, I’ve initially assigned slider: any, but then replaced it with an empty interface and started digging around the props it takes.

class Carousel extends Component<ICarouselProps, ICarouselState> {
    slider: ISlider;

    constructor(props) {

        ....

        this.slider = React.createRef();
    }
   
    ...

    render() {
        
        ...

        return (
            <Slider
                ref={this.slider}
                ...
            >
                {children}
            </Slider>
        );
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

property '$refs' does not exist on type - You.com
QUESTION ANSWERS. Trying to auto-focus on the next input field after 1 character is typed. Getting error: Property '$refs' does not exist on...
Read more >
Property 'ref' does not exist on type 'IntrinsicAttributes'
I don't see any error when replicate your code. But it does have 1 error at runtime: Warning: Function components cannot be given...
Read more >
TypeScript Fundamentals - Joy of Code
You can't access any object properties unless you type narrow first and then use type assertion; Type unknown can only be assigned to...
Read more >
React: Property 'X' does not exist on type 'Readonly<{}>'
The React.js error "Property does not exist on type 'Readonly<{}>'" occurs when we try to access the props or state of a class...
Read more >
Safari Technology Preview Release Notes - Apple Developer
Note: Tab Groups do not sync in this release. Web Inspector. Elements. Added CSS keyword completions for standard logical properties (r280588). Console. Fixed ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found