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

Can't get this.prop when use ES6 classes in React #4425

Closed
jasonliao opened this issue Jul 19, 2015 · 8 comments
Closed

Can't get this.prop when use ES6 classes in React #4425

jasonliao opened this issue Jul 19, 2015 · 8 comments

Comments

@jasonliao
Copy link

I'm new to React and I want to rewrite a simple react demo with ES6.

but it seem that I can't get this.props, here is the code.

class Timer extends React.Component {
  constructor (props) {
    super(props);
    this.state = {
      elapsed: 0
    }
  }
  // other methods here
  tick () {
    this.setState({
      elapsed: new Date() - this.props.start  // Cannot read property 'start' of undefined
    });
  }
  render () {
    var elapsed = Math.round(this.state.elapsed / 100);
    var seconds = (elapsed / 10).toFixed(1);
    return <p>This example was started {seconds} seconds ago.</p>
  }
}

React.render(<Timer start = {Date.now()} />, document.body);

I use "babel-loader": ^5.3.2 and "react": ^0.13.3 , thanks!

@sebmck
Copy link
Contributor

sebmck commented Jul 19, 2015

That's not the entirety of your code. Where's the tick method being called?

@cht8687
Copy link
Contributor

cht8687 commented Jul 19, 2015

I think you need manually bind instance methods when using ES6.
put this.tick = this.tick.bind(this); in the constructor.
Please let me know if I were wrong.

@jasonliao
Copy link
Author

@cht8687

you are right! It worked when I call the tick method by using this.tick.bind(this)

componentDidMount () {
  this.timer = setInterval(this.tick.bind(this), 50);
}

Thank you all! 👻

@cht8687
Copy link
Contributor

cht8687 commented Jul 19, 2015

@L-movingon

Actually, if you are using ES6, the code block of 'componentDidMonut' method can be put inside the constructor and you don't need 'compoentDidMonut' any more.

@kiki-le-singe
Copy link

@cht8687 +1
@L-movingon you can read about autobinding here no-autobinding and autobinding

You can also use the ES6 and ES7 way arrow functions and property initializers

@jasonliao
Copy link
Author

@cht8687 @kiki-le-singe Thanks again!

@waldreiter
Copy link
Contributor

@cht8687

Actually, if you are using ES6, the code block of 'componentDidMonut' method can be put inside the constructor and you don't need 'compoentDidMonut' any more.

One difference between the constructor and componentDidMount is that the constructor gets called during server rendering too. In this case @L-movingon's code uses setInterval(). If that interval gets set on the server, possible many times, then the server will become very busy. Therefore my advice is to use componentDidMount().

@ajit77shinde
Copy link

constructor(props) {
super(props);

}
Use this

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

6 participants