Javascript 每当道具在react中更改时,清除调用堆栈
最初尝试使用type=“ABC”执行下面的代码时,我的道具在lazyLoad函数完全执行之前突然变为type=“XYZ”(因为当时窗口没有完全加载)。结果: XYZ 你好 懒散 基础知识 ABC 如果我的道具发生了变化,我不希望我的lazyload函数执行。我该怎么办 代码片段Javascript 每当道具在react中更改时,清除调用堆栈,javascript,jquery,reactjs,redux,react-redux,Javascript,Jquery,Reactjs,Redux,React Redux,最初尝试使用type=“ABC”执行下面的代码时,我的道具在lazyLoad函数完全执行之前突然变为type=“XYZ”(因为当时窗口没有完全加载)。结果: XYZ 你好 懒散 基础知识 ABC 如果我的道具发生了变化,我不希望我的lazyload函数执行。我该怎么办 代码片段 class Type extends React.Component { constructor(props) { super(props); this.state = { type :
class Type extends React.Component {
constructor(props) {
super(props);
this.state = {
type : props.type
}
if(__CLIENT__ && this.state.type == "ABC"){
let self = this;
window.onload = function(e) {
self.lazyLoad(props.type);
}
}
}
componentDidMount(){
if(this.state.type != "ABC") {
this.someFunction();
}
}
componentWillReceiveProps(nextProps){
if(nextProps.type !== this.state.type){
if(nextProps.type != "ABC") {
this.someFunction();
}
this.setState({
type : nextProps.type
});
}
}
someFunction(){
console.log(this.state.type);
//some other logic as well
this.someFunction1();
}
lazyLoad(type) {
if(type == this.state.type){
console.log("in lazyLoad");
console.log(type);
console.log(this.state.type);
this.someFunction1();
}
}
someFunction1(){
console.log("hello");
}
render() {
//some code logic
}
}在组件的构造函数中调用
window.onload
的技术原因是什么?如果您不想在满足某些条件时运行lazyLoad
,请将对lazyLoad
的调用封装在If
语句中(而不是调用window.onload
)。我希望lazyload函数仅在我的页面完全加载时调用,但如果用户在页面完全加载之前单击“继续”按钮,我的道具将更改,并且我不希望在这种情况下再执行lazyload。@lux请进一步帮助。