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

json-react-layouts-data-loader

v5.0.1

Published

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

Downloads

168

Readme

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 {
    getMiddleware,
    createRegisterableComponentWithData,
    createRegisterableCompositionWithData,
} = init<MyServices>(resources)

const layout = LayoutRegistration()
    .registerComponents(registrar =>
        registrar
            // Register your components, then register the component data loading middleware
            .registerMiddleware(getMiddleware('component')),
    )
    .registerCompositions(registrar =>
        registrar
            // Register your compositions, then register the composition data loading middleware
            .registerMiddleware(getMiddleware('composition')),
    )

// Data-loading component.
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} />
    },
)

// Data-loading composition.
const testCompositionWithDataRegistration = createRegisterableCompositionWithData<'main'>()(
    'test-with-data', //                                Composition content areas ^^^^^^
    {
        // You provide this function to load the data
        loadData: props => {},
    },
    ({ main }, props, data) => {
    // ^^^^ Receive content areas here.
        if (!data.loaded) {
            return <div>Loading...</div>
        }

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

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} />
    },
)