JSON React layouts data loader
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 {
}
const resources = new DataLoaderResources<MyServices>()
const { middleware, createRegisterableComponentWithData } = init<MyServices>(resources)
const componentRegistrar = new ComponentRegistrar()
.registerMiddleware(middleware)
export const testComponentWithDataRegistration = createRegisterableComponentWithData(
'test-with-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
loadData: props => {
props.myAdditionalState
},
},
(props, data) => {
if (!data.loaded) {
return <div>Loading...</div>
}
return <TestComponentWithData data={data.result} />
},
)