首页 > 解决方案 > map 不是一个函数,它可能是由于数组

问题描述

    class QuestionAPI extends Component {
  constructor(props) {
    console.log(props)
    super(props);
    this.state = {
      items: [],
    };
  }
  //Question answer api call
  componentDidMount = () => {
    axios.get(apiURL, {
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        Authorization: `Bearer  ` + token,
      },
    }).then((result) => {
      //  console.log(result.data);
      this.setState({items:result}) 
       //error is hear
         this.state.items.map((item) =>
           //QuestionAPI container
          this.props.QuestionApiHandler({ id: item.id }))
   })
  }
  render() {
    return (
      <div>
      </div>
    );
  };
}
export default QuestionAPI;

标签: reactjsapi

解决方案


  • 我认为这可能有效
  • 我希望它对你有帮助。
componentDidMount = () => {
    axios.get(apiURL, {
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        Authorization: `Bearer  ` + token,
      },
    }).then(response => response.data)
      .then((data) => {
        this.setState({ items: data })
        this.state.items.map((item) =>
          this.props.QuestionApiHandler({ id: item.id })
        )

推荐阅读