-
-
Notifications
You must be signed in to change notification settings - Fork 193
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
onReady issue #13
Comments
You have a binding: Use one of these 5 suggested approaches to solve it. My preference goes out to number 5 “Arrow Function in Class Property“: class Example extends React.Component {
// By using an Arrow Function, the function will be bound to the execution context from which it is called (“lexical this”)
setDistance = (distance,duration_in_traffic) => {
this.setState({distance:parseFloat(distance),durationInTraffic:parseInt(duration_in_traffic)});
}
render() {
// ...
}
} See Javascript ES6 — Arrow Functions and Lexical |
I have tried and it should not be related to this issue since it can successfully execute the code inside setDistance, but somehow it will drive onReady to infinity loop when I call the function or this.setState(). |
Ok, think I see it now: setState() will trigger a re-render, thus re-rendering |
The problem is in this line: if ((nextProps.origin != this.props.origin) || (nextProps.destination != this.props.destination) || !isEqual(nextProps.waypoints, this.props.waypoints)) { You should not compare arrays in JS directly, because two arrays are always not equal. You should compare route values. Use isEqual instead of !== to fix this issue. |
Good point @sidious18. Working and testing a fix right now. |
It took a bit longer than expected, but couldn't trigger this one myself at first. The “culprit” was the fact that I used an array holding all coordinates in my state. This then resulted in the fact that the references remained the same all the time, even after a re-render. In contrast: the example above creates a new object on every render. Even though Thanks to all involved for their input :) |
I don't know why if I want to set variables in onReady, it will goes to an infinity loop, however if I just use console.log, it is alright, here is the code. The code should not affected the origin and destinaiton.
The text was updated successfully, but these errors were encountered: