发现老师的写法有点消耗心能改进了一下。
getListInfoSucc=res=>{
this.setState({
list:res.data.data,
})
}
componentDidMount(){
let id=this.props.match.params.id;
id=id?id:1;
this.currentId=id;
axios.get('http://www.dell-lee.com/react/api/list.json?id='+id)
.then(this.getListInfoSucc)
}
componentWillReceiveProps(next){
let nextId=next.match.params.id;
if(this.currentId===nextId)return;
this.currentId=nextId
axios.get('http://www.dell-lee.com/react/api/list.json?id='+this.currentId)
.then(this.getListInfoSucc)
}
改动就这三处,老师看看能不能改进一下。
还有就是最开始this.currentId这个变量我写在this.state里面,后面发现直接写this.currentId把他挂在类上也可以。我记得这种写法是在构建函数里面的用法,在类中像这种我们都写在constructor中的吧?这样写可以吗?这样写与写在this.state中的区别是什么?
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 239 份
- 解答问题 10739 个
本阶段带你深入前端开发的肌理,通过ES6基础知识和前端主流高级框架的学习,助你快速构建企业级移动webAPP应用,进入职场的终极battle
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星