# Custom component fro table with api data

**URL:** <https://forum.builder.io/t/custom-component-fro-table-with-api-data/5863>\
**Category:** Visual Editor Help\
**Tags:** visual-editor, styling, react, api, nextjs\
**Created:** [June 25, 2024, 9:36am UTC](https://forum.builder.io/t/custom-component-fro-table-with-api-data/5863 "2024-06-25T09:36:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amanuel.tadesse](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@amanuel.tadesse](https://forum.builder.io/u/amanuel.tadesse)\
**Post date:** [June 25, 2024, 9:36am UTC](https://forum.builder.io/t/custom-component-fro-table-with-api-data/5863/1 "2024-06-25T09:36:29Z")

</div>

**What are you trying to accomplish**  
_i want to add a custom component for tables and my data is coming from api_

**Screenshots or video link**

 ![Screenshot 2024-06-25 at 12.32.12 in the afternoon](https://us1.discourse-cdn.com/flex020/uploads/builder/original/2X/7/76377482492c1c1e9c4809a79ab8265704d47948.png)  
the problem am facing is the style of the table might be different and all of their data should be fetched from api data. my solution was to go with a custom component that will accept 2 BuilderBlocks one for the table heading and the other one for the table content’s but with that approach am not sure how to bind the api data with the builder builtin components any help or idea how can i approach this issue will be much appreciate thanks

**Code stack you are integrating Builder with**  
_NextJS, react_

---

<div class="post-metadata">

**Author:** ![Stephane](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@Stephane](https://forum.builder.io/u/Stephane)\
**Post date:** [July 2, 2024, 2:34pm UTC](https://forum.builder.io/t/custom-component-fro-table-with-api-data/5863/2 "2024-07-02T14:34:51Z")

</div>

Sure, I understand that you want to create a custom table component in [Builder.io](http://Builder.io) that can dynamically fetch data from an API and render it, while allowing the table’s header and content styles to be customizable via Builder Blocks.

Here is a step-by-step guide on how to achieve this:

### Step-by-Step Guide:

1. **Create and Register the Custom Component in Your Codebase:**

2. **Configure the Component in [Builder.io](http://Builder.io):**

### Example Implementation:

#### 1. Create and Register the Custom Table Component:

First, create your custom Table component and ensure it renders with BuilderBlocks for headers and contents.

```javascript
import React, { useEffect, useState } from 'react';
import { Builder, BuilderComponent, BuilderBlocks } from '@builder.io/react';

// Replace with your actual Public API Key
const YOUR_API_KEY = 'YOUR_API_KEY';
Builder.init(YOUR_API_KEY);

const CustomTable = ({ apiEndpoint, tableHeaders, tableContents }) => {
  const [data, setData] = useState([]);

  useEffect(() => {
    // Fetch data from the API endpoint
    async function fetchData() {
      const response = await fetch(apiEndpoint);
      const result = await response.json();
      setData(result);
    }
    fetchData();
  }, [apiEndpoint]);

  return (
    <table>
      <thead>
        <tr>
          <BuilderBlocks parentElementId={tableHeaders} blocks={tableHeaders} />
        </tr>
      </thead>
      <tbody>
        {data.map((row, index) => (
          <tr key={index}>
            <BuilderBlocks parentElementId={tableContents} blocks={tableContents} data={{ row }} />
          </tr>
        ))}
      </tbody>
    </table>
  );
};

// Register the CustomTable component with Builder.io
Builder.registerComponent(CustomTable, {
  name: 'CustomTable',
  inputs: [
    {
      name: 'apiEndpoint',
      type: 'string',
      defaultValue: 'https://your-api-endpoint.com/data',
      friendlyName: 'API Endpoint',
    },
    {
      name: 'tableHeaders',
      type: 'uiBlocks',
      defaultValue: [],
      friendlyName: 'Table Headers',
    },
    {
      name: 'tableContents',
      type: 'uiBlocks',
      defaultValue: [],
      friendlyName: 'Table Contents',
    },
  ],
});

export default CustomTable;

```

#### 2. Use the Custom Component in a Page or Section Model:

Ensure that you have integrated [Builder.io](http://Builder.io) into your Next.js project and then create a page that uses the custom table component:

```jsx
// pages/_app.js or _app.tsx
import { Builder } from '@builder.io/react';
import '../styles/globals.css';

Builder.init('YOUR_PUBLIC_API_KEY');

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />;
}

export default MyApp;

// pages/index.js or index.tsx
import { BuilderComponent } from '@builder.io/react';

export default function Home() {
  return <BuilderComponent model="page" />;
}

```

#### 3. Add the Custom Component to a Builder Page:

1. **Create a [Page model](https://www.builder.io/c/docs/models-pages) or a [Section model](https://www.builder.io/c/docs/models-sections) in [Builder.io](http://Builder.io)** if you haven’t done this already.
2. **Drag and Drop** the `CustomTable` component into the Builder Visual Editor.
3. **Set the API Endpoint:** Configure the API endpoint from where the table data will be fetched.
4. **Customize Table Headers and Contents:**
  - For **Table Headers** , drag in Builder Blocks (like Text) and define the headers.
  - For **Table Contents** , drag in Builder Blocks and bind the data fields as needed.

### Example of Table Header and Contents Binding:

In the Visual Editor, when setting up `tableHeaders` and `tableContents`:

```jsx
// Define Table Headers in the Visual Editor
<tableHeader>
  <th>Header 1</th>
  <th>Header 2</th>
  <th>Header 3</th>
</tableHeader>

// Define Table Contents in the Visual Editor
<tableContents>
  <td>{{ row.field1 }}</td>
  <td>{{ row.field2 }}</td>
  <td>{{ row.field3 }}</td>
</tableContents>

```

### Binding Data to Elements:

In the `tableContents` Builder Blocks, bind each cell to the corresponding data field from your API. For example:

```jsx
<td>{{ row.name }}</td> // assuming `name` is a field in your API data
<td>{{ row.value }}</td> // assuming `value` is another field

```

### Final Integration and Testing:

1. **Publish the Page:**

2. **Verify the Output:**

### Summary:

- **Custom Component:** Create a custom component that fetches data from an API and renders it dynamically.
- **Builder Blocks:** Utilize Builder Blocks for flexible and customizable headers and contents within the table.
- **Dynamic Data Binding:** Bind data fields from your API to the table rows within the Builder Visual Editor.

For more detailed information and advanced customization, you can refer to the [Custom Components Setup documentation](https://www.builder.io/c/docs/custom-components-setup) and [Integrating Custom Components](https://www.builder.io/c/docs/integrate-cms-data). These resources will provide further insights into maximizing the capabilities of [Builder.io](http://Builder.io) for dynamic content integration.

---

<div class="post-metadata">

**Author:** ![amanuel.tadesse](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@amanuel.tadesse](https://forum.builder.io/u/amanuel.tadesse)\
**Post date:** [July 2, 2024, 5:31pm UTC](https://forum.builder.io/t/custom-component-fro-table-with-api-data/5863/3 "2024-07-02T17:31:49Z")

</div>

am going to try it … thanks for the help
