componentDidUpdate 发送ajax请求进入死循环

#1
	componentWillReceiveProps(np) {
	    var { location } = np;
	    var { pathname, search } = location;
	    var path = pathname + search;
	    if (this.path !== path) {
	        //地址栏已经发生改变,重新加载数据
	        this.unMount();
	    }
	    this.initState(np,this.state);
	}
	componentDidUpdate(prevProps,prevState) {
		//render之前 获取数据
		//this.getData(prevProps,prevState);
	}


#2

componentDidUpdate表示重新渲染完成了,那么你这时候再执行this.getData(prevProps,prevState); 不就行相当于渲染完重新发送ajax请求吗?然后返回新的数据,有渲染一次,渲染完又重新请求,不断的重复。

你可以手动判断shouldComponentUpdate是否要执行更新。

#3

也可以DidUpdate 判断前后数据是否有变化,有变化了的话,重新发送请求啊。
官方推荐的写法:

componentDidUpdate() is invoked immediately after updating occurs. This method is not called for the initial render.

Use this as an opportunity to operate on the DOM when the component has been updated. This is also a good place to do network requests as long as you compare the current props to previous props (e.g. a network request may not be necessary if the props have not changed).

componentDidUpdate()