plougsgaard / react-timeout

Component wrapper for setTimeout et al that cleans up after itself.
127 stars 17 forks source link

this.props.setTimeout not a function #8

Closed joebien closed 6 years ago

joebien commented 7 years ago

please help! How does this.props.setTimeout access the react-timeout function?

plougsgaard commented 7 years ago

@joebien

When you wrap your own component with react-timeout, your component is provided with the setTimeout function in its props. So the this.props.setTimeout function in your component is the react-timeout function. Hope that's somewhat understandable! 😄

alphakennyn commented 6 years ago

Hi, I'm getting this.props.setTimeout is not a function. I followed the instructions but I dont know what else to do anymore.

This is my code

import React from "react";
import ReactTimeout from 'react-timeout'

import "./main.css";
import Content from "../content/content";

import { connect } from "react-redux";

class Main extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isHovered: false,
      scrollValue: 0
    };
    this.smoothScroll = this.smoothScroll.bind(this);

  }

  smoothScroll = () => {
    this.setState(prevState => {scrollValue: prevState+1})
  }

  componentDidMount = () => {
    this.props.setTimeout( this.smoothScroll , 100) // call the `toggle` function after 100ms 
  }
}
plougsgaard commented 6 years ago

@alphakennyn how are you calling the ReactTimeout on the Main component? I assume you're doing it in conjunction with the connect somewhere later?

alphakennyn commented 6 years ago

I'm calling it from componentDidMount(). I'm trying to move from traditional javascript setTimeout to react-timeout. As of now, my connect is just imported but not in use yet.

My error is called here this.props.setTimeoutis not a function

plougsgaard commented 6 years ago

@alphakennyn

If I had to guess I'd think you've forgotten to call the ReactTimeout function on Main.

There's an example of how to export it for a class in the readme (last line of the code example):

https://github.com/plougsgaard/react-timeout#es6-classes---the-light-switch

alphakennyn commented 6 years ago

Oh you're right! thank you!

plougsgaard commented 6 years ago

Glad it worked out. 😄