一、不遍历数组渲染数据,只能渲染出一级数据

上代码

  1. class MovieDetail extends Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. moviedetail: [],
  6. images: ""
  7. }
  8. }
  9. render() {
  10. return (
  11. <div className="moviedetail-content">
  12. <img src={this.state.images} alt="" />
  13. <p>{this.state.moviedetail.title}</p>
  14. <p>{this.state.moviedetail.summary}</p>
  15. </div>
  16. );
  17. }
  18. componentDidMount() {
  19. var { id } = this.props.match.params
  20. var url = `https://douban.uieee.com/v2/movie/subject/${id}`
  21. this.$http.get(url).then(res => {
  22. this.setState({
  23. moviedetail: res.data,
  24. images: res.data.images.large
  25. })
  26. console.log(this.state.moviedetail)
  27. })
  28. }
  29. }

上图片
image.png

二、解决React组件中没有history属性的问题

  • 使用 withRouter

withRouter高阶组件,提供了history让你使用~

  1. 安装依懒 yarn add react-router-dom
  1. import React from "react";
  2. import {withRouter} from "react-router-dom";
  3. class MyComponent extends React.Component {
  4. ...
  5. myFunction() {
  6. this.props.history.push("/some/Path");
  7. }
  8. ...
  9. }
  10. export default withRouter(MyComponent);
  1. [
  2. ]()
  3. [
  4. ]()