关于this

来源:4-3 使用动态路由获取不同列表内容(3)

哎呦啊狗蛋

2019-08-08 13:54:37

componentWillReceiveProps(nextProps) {
		const id = nextProps.match.params.id; //为什么这里不需要加this了
		axios.get('http://www.dell-lee.com/react/api/list.json?id=' + id)
			.then(res => {
				this.setState({
					data: res.data.data
				});
			})
	}
componentDidMount() {
		let url = 'http://www.dell-lee.com/react/api/list.json';
		const id = this.props.match.params.id; //而这里需要加this
		if (id) {
			url = url + '?id=' + id;
		}
		axios.get(url)
			.then(res => {
				this.setState({
					data: res.data.data
				});
			})
	}


写回答

1回答

好帮手慕星星

2019-08-08

同学你好,

是componentWillReceiveProps这个生命周期的用法哦,props发生变化时执行componentWillReceiveProps。

参数nextProps代表的是新属性,所以直接在这个属性上获取内容就可以。如果还想使用原来的属性,就需要使用this了,例如,输出下一个id值以及当前id值:

http://img.mukewang.com/climg/5d4be04e0001cad505920164.jpg

自己可以输出测试下,祝学习愉快!

0

0 学习 · 10739 问题

查看课程