Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-react-layouts-data-loader

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-react-layouts-data-loader

[![npm](https://img.shields.io/npm/v/json-react-layouts-data-loader)](https://www.npmjs.com/package/json-react-layouts-data-loader)

  • 4.0.0-beta.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
25
decreased by-30.56%
Maintainers
1
Weekly downloads
 
Created
Source

JSON React layouts data loader

npm

Component middleware for JSON React layouts which enables data loading via the React SSR Data Loader library.

Usage

import { init } from 'json-react-layouts-data-loader'
import { DataLoaderResources, DataProvider } from 'react-ssr-data-loader'

interface MyServices {
    // Put the services you want available to components
}

const resources = new DataLoaderResources<MyServices>()
const { middleware, createRegisterableComponentWithData } = init<MyServices>(resources)

const componentRegistrar = new ComponentRegistrar()
    // Register your components, then register the component data loading middleware
    .registerMiddleware(middleware)

export const testComponentWithDataRegistration = createRegisterableComponentWithData(
    'test-with-data',
    {
        // You provide this function to load the data
        loadData: props => {},
    },
    (props, data) => {
        if (!data.loaded) {
            return <div>Loading...</div>
        }

        return <TestComponentWithData data={data.result} />
    },
)

FAQ

My data load function references global variables and does not update when they change

If you reference global variables in your data load function the data will not be re-fetched when that variable changes. This is because the data loader assumes if the arguments are the same, the result of the load function will be the same as the current data and do nothing.

You can use the useRuntimeParams function to merge additional varibles to the data loader props when it re-renders so it can fetch the updated data as expected. For example if you had state stored in redux.

React hooks are supported inside this function.

import { init } from 'json-react-layouts-data-loader'
import { DataLoaderResources, DataProvider } from 'react-ssr-data-loader'

export const testComponentWithDataRegistration = createRegisterableComponentWithData(
    'test-with-data',
    {
        getRuntimeParams: (props, services) => services.store.getState().myAdditionalState
        // You provide this function to load the data
        loadData: props => {
            // Now the global state is visible to the data loader and will make up the cache key so changes to myAdditionalState will cause the data to be re-loaded
            props.myAdditionalState
        },
    },
    (props, data) => {
        if (!data.loaded) {
            return <div>Loading...</div>
        }

        return <TestComponentWithData data={data.result} />
    },
)

FAQs

Package last updated on 21 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc