You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ReactTable遍历对象数组渲染失败及TypeError报错解决

Fixing "TypeError: resolvedData.map is not a function" in ReactTable

Hey there! Let's break down why you're hitting this error and how to fix it quickly.

The Root Cause

Your API returns an object with a user property that holds the array of user data (like {user: Array(5)}), but you're passing the entire object to ReactTable's data prop. ReactTable expects data to be an array (so it can use .map() to render rows), and since objects don't have a map method, you get that TypeError.

Step-by-Step Fix

  1. Update your state to store the actual user array
    In your componentDidMount method, instead of saving the full API response object, extract the user array and save that to state:

    componentDidMount(){
      const url="http://localhost:5000/api/users/tenantView";
      fetch (url,{ method: "GET" })
        .then(response=> response.json())
        .then(usersList=>{
          console.log('API Response:', usersList) ;
          // Save the user array instead of the whole object
          this.setState({ usersList : usersList.user });
        })
    }
    
  2. Initialize state with an empty array (optional but recommended)
    To avoid potential issues when the component renders before the fetch completes, set an initial empty array for usersList in your component's constructor:

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

Full Modified Code Example

Here's how your component should look with these fixes:

class YourComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      usersList: []
    };
  }

  componentDidMount(){
    const url="http://localhost:5000/api/users/tenantView";
    fetch (url,{ method: "GET" })
      .then(response=> response.json())
      .then(usersList=>{
        console.log('API Response:', usersList) ;
        this.setState({ usersList : usersList.user });
      })
  }

  render() {
    const columns =[
      { Header:"User ID", accessor:"_id", sortable: false, filterable: false, style:{ textAlign: "left" }, width: 100, maxWidth: 100, minWidth: 100, },
      { Header:"Name ", accessor:"name", filterable: false, },
      { Header:"Email", accessor:"email" },
      { Header:"Password", accessor:"password" },
      { Header:"Farm", accessor:"farm" },
    ]
    return(
      <ReactTable columns={columns} data={this.state.usersList} defaultPageSize={20} />
    );
  }
}

Why This Works

Now ReactTable's data prop receives the actual array of users from your API response. It can now call .map() on this array to render each row of the table, eliminating the TypeError.

内容的提问来源于stack exchange,提问作者user5140792

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:23:24