javascript - 什么是最佳实践,直接在渲染或状态中使用 const?
问题描述
我是新手,请帮助我了解最佳实践。
我应该直接在渲染或状态中使用 const 吗?
下面是示例代码。
import React, { Component } from 'react';
class VehicleDetail extends Component{
constructor(props){
super(props);
this.state = {vehicle: [] };
}
componentDidMount() {
axios.get(`/getDetails/${this.props.match.params.id}`)
.then(response => {
this.setState({ vehicle : response.data.vehicle });
});
}
render() {
const vehicle = this.state.vehicle;
return(
<div className="col-12 col-md-5 car-price-detail">
<h3>{vehicle.title}</h3>
<h5><span>Mileage:</span> {vehicle.mileage}</h5>
<h5><span>Color:</span> {vehicle.exterior_color}</h5>
</div>
);
}
}
import React, { Component } from 'react';
class VehicleDetail extends Component{
constructor(props){
super(props);
this.state = {vehicle: [] };
}
componentDidMount() {
axios.get(`/getDetails/${this.props.match.params.id}`)
.then(response => {
this.setState({ vehicle : response.data.vehicle });
});
}
render() {
return(
<div className="col-12 col-md-5 car-price-detail">
<h3>{this.state.vehicle.title}</h3>
<h5><span>Mileage:</span> {this.state.vehicle.mileage}</h5>
<h5><span>Color:</span> {this.state.vehicle.exterior_color}</h5>
</div>
);
}
}
解决方案
ESLINT 建议您使用解构变量:
const { vehicle } = this.state;
推荐阅读
- amazon-web-services - 我的 CodePipeline 没有在 s3 事件触发器上触发
- python - 为什么使用以下代码和 __name__ == "__main__?
- javascript - 使用节点客户端库在 Elasticsearch 中插入数字(浮点数)
- git - 如何使用 Jenkins Freestyle Project 访问远程 Git 存储库
- r - 使用 rvest 进行网络抓取:用 NA 替换 html_nodes 的缺失值
- string - SQLite 连接
- cmake - 为现代 CMake 中的所有目标导出编译器标志
- sql - 在 oracle 中的更新表上未调用触发器
- graphics - 使用栅栏清理命令缓冲区并同时同步交换链图像
- javascript - Electron - 从 Menu 向渲染器发送自定义事件