npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ra-external-data

v1.0.3

Published

External data component for React Admin

Downloads

11

Readme

ra-external-data

External data component for React Admin.

Description

This component enables you to load external json formatted data into your React Admin app easily and use the external data seamlessly with standard React Admin field components.

Installation

npm install ra-external-data

Usage

After adding the package to your React Admin app, you must register the externalDataReducer in your App as a custom reducer:

import { externalDataReducer } from 'ra-external-data';

const App = () => (
  <Admin customReducers={{externalDataReducer}}>
	/* your resources here */
  </Admin>  
);

Basic example - single record

The following example illustrates how to use the component in its simplest form:

import React from 'react';
import { SimpleShowLayout, BooleanField, TextField } from 'react-admin';
import ExternalData from 'ra-external-data';

const ExternalDataExample = () =>
    <ExternalData url="https://jsonplaceholder.typicode.com/todos/1">
        <SimpleShowLayout>            
            <TextField source="title" />
            <BooleanField source="completed" />
        </SimpleShowLayout>
    </ExternalData>;

export default ExternalDataExample

As you can see, inside the ExternalData component you can use React Admin's field components as if you are working with your own API and default provider. This is because the component replaces React Admin's record with the data that was retrieved from the external resource.

Basic example - multiple records

You can also use resources that return a list, using React Admin's ArrayField component:

import React from 'react';
import { SimpleShowLayout, ArrayField, Datagrid, BooleanField, TextField } from 'react-admin';
import ExternalData from 'ra-external-data';

const ExternalDataExample = () =>
    <ExternalData url="https://jsonplaceholder.typicode.com/todos">
        <SimpleShowLayout>  
            <ArrayField source="records">
                <Datagrid>
                    <TextField source="title" />
                    <BooleanField source="completed" />
                </Datagrid>
            </ArrayField>          
        </SimpleShowLayout>
    </ExternalData>;

export default ExternalDataExample

As you can see, if the ExternalData component detects the data returned is an array instead of a single object, the record will contain a records property holding all the returned records.

Handling request states

The ExternalData component will start fetching data at componentDidMount() time, and emit various states while fetching the data:

  • EXTERNAL_DATA_REQUEST_PENDING
  • EXTERNAL_DATA_REQUEST_SUCCEEDED
  • EXTERNAL_DATA_REQUEST_FAILED

You can set the requestState property on children of the ExternalData component to control what is displayed during the various request states:

import React from 'react';
import { SimpleShowLayout, ArrayField, Datagrid, BooleanField, TextField } from 'react-admin';
import ExternalData from 'ra-external-data';

const ExternalDataExample = () =>
    <ExternalData url="https://jsonplaceholder.typicode.com/todos">
        <SimpleShowLayout requestState={EXTERNAL_DATA_REQUEST_PENDING}>
            <span>Processing request...</span>
        </SimpleShowLayout>
        <SimpleShowLayout requestState={EXTERNAL_DATA_REQUEST_SUCCEEDED}>  
            <ArrayField source="records">
                <Datagrid>
                    <TextField source="title" />
                    <BooleanField source="completed" />
                </Datagrid>
            </ArrayField>          
        </SimpleShowLayout>
        <SimpleShowLayout requestState={EXTERNAL_DATA_REQUEST_FAILED}>
            <span>Something went wrong!</span>
        </SimpleShowLayout>
    </ExternalData>;

export default ExternalDataExample

If you do not set the requestState property, the child element will always be displayed.

Note: you can only set the requestState property on direct descendants (children) of the ExternalData component. The ExternalData component does not parse the entire tree for this property.

Using a custom client

By default, the ExternalData uses React-Admin's fetchUtils to fetch the data for you. You might need more control over the request however, for instance, you might need to pass a security token or api key as a header in the request. For these scenarios, the ExternalData component allows you to pass a custom http client using the client property. The following example shows how to pass a custom client to the ExternalData component that adds a Authorization header to the request containing a token that was stored in localStorage earlier:

import React from 'react';
import { fetchUtils, SimpleShowLayout, BooleanField, TextField } from 'react-admin';
import ExternalData from 'ra-external-data';

const httpClient = (url, options = {}) => {
    if (!options.headers) {
        options.headers = new Headers({ Accept: 'application/json' });
    }
    const token = localStorage.getItem('token');
    options.headers.set('Authorization', `Bearer ${token}`);
    return fetchUtils.fetchJson(url, options);
  }

const ExternalDataExample = () =>
    <ExternalData url="https://jsonplaceholder.typicode.com/todos/1" client={httpClient}>        
        <SimpleShowLayout>  
            <TextField source="title" />
            <BooleanField source="completed" />
        </SimpleShowLayout>
    </ExternalData>;

export default ExternalDataExample

Known issues

  • You cannot use multiple ExternalData components on a single page, unless you intend them all to display the same data.