Create a React Connector


To create your React connector, we recommend that you first create a sample React connector and edit the generated files. It’s easier to get all the files and directory structure your connector needs by just using an existing example.

To create your React connector, do the following steps.

Step 1: Create a boilerplate React connector

  1. Enter the following command to create the connector:

    taco create my-react-connector --boilerplate react 
    

    This creates a directory with the earthquake data boilerplate code, which is included with the toolkit.

  2. Change directories to the my-react-connector directory.
    cd my-react-connector
    
  3. Build the connector by entering the following command:

    taco build
    

    This command clears any previous or existing build caches, then installs the dependencies, then builds the frontend code and the backend code (handlers), then copies the connector.json file (the configuration file).

Step 2: Configure your connector’s properties

In your new React connector directory, find and open the connector.json file.

{
  "name": "my-react-connector",
  "version": "1.0.0",
  "tableau-version": {
    "min": "2022.3"
  },
  "vendor": {
    "name": "vendor-name",
    "support-link": "https://vendor-name.com",
    "email": "support@vendor-name.com"
  },
  "permission": {
    "api": {
      "https://*.usgs.gov/": [
        "GET",
        "POST",
        "HEAD"
      ]
    }
  },
  "auth": {
    "type": "none"
  },
  "window": {
    "height": 800,
    "width": 600
  }
}

Make the following changes:

  1. Change the general properties.

    Name Value
    name Your connector’s name
    version Your connector’s version
    min The earliest Tableau version your connector supports
  2. Change the company properties.

    Name Value
    vendor.name Your company name
    vendor.support-link Your company’s URL
    vendor.email Your company’s email
  3. Change the permissions.

    Name Value
    permission.api The URI for the API that the connector is allowed to access, along with the methods (POST, GET, PUT, PATCH, DELETE) that the connector is allowed to use.
  4. Change the authentication type.

    Name Value
    auth.type Accepted values are api-key, basic-auth, custom, none, and oauth2.

    For more information about authentication, see the Authentication section in the Considerations for Building Your Connector topic.

  5. Change the HTML pane size.

    Name Value
    window.height The height of the connector HTML pane
    window.width The width of the connector HTML pane

Step 3: Create the user interface

When you open a web data connector in Tableau, the connector displays an HTML page that links to your JavaScript code and to your connector’s handlers. Optionally, this page can also display a user interface for your users to select the data that they want to download.

To create a user interface for your connector, open the /app/index.html file.

<!DOCTYPE html>
<html>

<head>
  <title>Sample Connector</title>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta http-equiv="Cache-Control" content="no-store" />
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
  <link rel="stylesheet" href="app.css">

</head>

<body>
  <div id="root"></div>
  <script type="module" src="./App.tsx"></script>
</body>

</html>

Let’s run through what the code is doing. Skipping over the standard markup for an HTML page, notice the following between the head tags:

Step 4: Edit the connector object

Now that you’ve created a user interface, it’s time to edit the JavaScript code for the connector’s button. First, open the /app/App.txs file.

import ReactDOM from 'react-dom/client'
import React from 'react'
import 'bootstrap'
import ConnectorView from './components/ConnectorView'

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
root.render(
  <React.StrictMode>
    <ConnectorView />
  </React.StrictMode>
)

Step 5: Update the fetcher file

If your data is complex and needs preprocessing, use the TACO Toolkit library to prepare your data. The following is the default code that the /handlers/MyFetcher.ts files uses to get the data:

import { Fetcher, FetchUtils, FetchOptions } from '@tableau/taco-toolkit/handlers'

export default class MyFetcher extends Fetcher {
  async *fetch({ handlerInput }: FetchOptions) {
    yield await FetchUtils.fetchJson(handlerInput.data.url)
  }
}

Step 6: Configure how the data is presented

Now you must define how you want to map the data to one or more or tables. This mapping of data is done in the schema.

To decide how to map your data, look at your data source. When you’re done looking at the summary of the JSON data source, make the necessary edits in the /handlers/MyParser.ts file to structure the returned data.

import { DataContainer, DataType, log, ParseOptions, Parser } from '@tableau/taco-toolkit/handlers'

export default class MyParser extends Parser {
  parse(fetcherResult: any, { dataContainer }: ParseOptions): DataContainer {
    const tableName = 'My Sample Data'
    log(`parsing started for '${tableName}'`)

    const containerBuilder = Parser.createContainerBuilder(dataContainer)
    const { isNew, tableBuilder } = containerBuilder.getTable(tableName)

    if (isNew) {
      tableBuilder.setId('mySampleData')
      tableBuilder.addColumnHeaders([
        {
          id: 'id',
          dataType: DataType.String,
        },
        {
          id: 'mag',
          alias: 'magnitude',
          dataType: DataType.Float,
        },
        {
          id: 'title',
          alias: 'title',
          dataType: DataType.String,
        },
        {
          id: 'location',
          dataType: DataType.Geometry,
        },
      ])
    }

    tableBuilder.addRows(
      fetcherResult.features.map((row: any) => {
        return { id: row.id, mag: row.properties.mag, title: row.properties.title, location: row.geometry }
      })
    )

    return containerBuilder.getDataContainer()
  }
}

Some notes:

Step 7: Build your connector

Enter these commands to build, pack, and run your new connector:

taco build
taco pack
taco run Desktop