一、不遍历数组渲染数据,只能渲染出一级数据
上代码
class MovieDetail extends Component {
constructor(props) {
super(props);
this.state = {
moviedetail: [],
images: ""
}
}
render() {
return (
<div className="moviedetail-content">
<img src={this.state.images} alt="" />
<p>{this.state.moviedetail.title}</p>
<p>{this.state.moviedetail.summary}</p>
</div>
);
}
componentDidMount() {
var { id } = this.props.match.params
var url = `https://douban.uieee.com/v2/movie/subject/${id}`
this.$http.get(url).then(res => {
this.setState({
moviedetail: res.data,
images: res.data.images.large
})
console.log(this.state.moviedetail)
})
}
}
上图片
二、解决React组件中没有history属性的问题
- 使用 withRouter
withRouter高阶组件,提供了history让你使用~
安装依懒 yarn add react-router-dom
import React from "react";
import {withRouter} from "react-router-dom";
class MyComponent extends React.Component {
...
myFunction() {
this.props.history.push("/some/Path");
}
...
}
export default withRouter(MyComponent);
[
]()
[
]()