状态数组未更新

时间:2019-02-16 11:09:11

标签: django reactjs api

在此React类State中,Users数组未更新?但, 更新后的服务数据显示在控制台日志上。在ComponetDidMount()中,使用customersService敲击api并获取响应数据, 然后更新状态

import React, { Component } from 'react';
import CustomersService from './projectservice';
const customersService = new CustomersService();

class ProjectDeatil extends Component {

  constructor(props) {
    super(props);
    this.state = { users: []};
  }

  componentDidMount() {
      const { match: { params } } = this.props;
      if(params && params.pk) {
        customersService.getCustomer(params.pk).then((user)=>{
          console.log( user);
          this.refs.title.value = user.title;
          this.refs.description.value = user.description;
          this.refs.image.value = user.image;
          this.refs.link.value = user.link;
          this.refs.body.value = user.body;
          this.refs.language.value = user.language;
          this.refs.slink.value = user.slink;

          this.setState({users: user.data});
        });
      }
    }

  render() {
    const{ users } = this.state;

    return (
      <div >
        <div className="row e text-white">
        {/* // {this.state.users.map( users => */}
          <h1 className="card-text text-white" key={users.pk}>Hello { users.pk}</h1>
        </div>
      </div>
    );
  }  
}
export default ProjectDeatil

请指导我解决此问题

0 个答案:

没有答案