Answer the question
In order to leave comments, you need to log in
How to properly delay the execution of functions?
When the page loads, I get the id of the component and render it,
but on the left there is a menu with buttons, when clicked, I change the routing and the function for changing the component is triggered (in the state I write a new this.props.params.id ), so this function works before the routing changes.
I fixed it with a setTimeout crutch with minimal delay.
But I want to know what is the right thing to do in such a situation?
In render:
<Link to={'/temp/'+props.id+'/'+item.id} onClick={props.handleChange} className="btn project-list-item" key={i}>{item.name}</Link>
handleChange(){
let _this = this;
setTimeout(function() {
_this.setState({currentId : _this.props.params.id});
}, 4);
}
Answer the question
In order to leave comments, you need to log in
Most likely, there is a problem somewhere in the code ...
But if you read your question literally, then besides setTimeout there is also setInterval and requestAnimationFrame (all three do not suit you).
Usually, if you want the function to be executed after receiving some result, use "callbacks".
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question