react-model-component
The component to mapping API data into React model and
Install
npm install --save react-model-component
Usage
This component supports for React rendering only and not yet tested on React-Native.
How to use ModelComponent rendering single object
- import the components of model-component
import RMCModel RMCModelElement RMCGlobalLoader from 'react-model-component';
- Create api handle const which having input field as string of Id and return data as a key-value object.
const OrganizationApiDetail = IDconsolereturnId: IDName: 'Name detail '+IDSurName: 'SurName detail '+ID
- Bind api const into RMCModel component. The parameter modelName can be anything but should be the same in all scope
const OrganizationObject = props <RMCModel ="Organization" = >propschildren</RMCModel>
if your api doesn't use ket name 'id' as a primary key then you can modify with prop name Primarykey.
const OrganizationObject = props <RMCModel ="Organization" = ="Id" >propschildren</RMCModel>
- Create element
const OrganizationField = props <RMCModelElement ="Organization" >propschildren</RMCModelElement>
- To use the model-component you shuold create Object and put Element inside. Beware of difference modelName, The field rendered may be blank.
<OrganizationObject ="1234"> <b><OrganizationField ="Name"/></b> <OrganizationField ="SurName"/> <b><OrganizationField ="Abbv"/></b> </OrganizationObject>
How to use ModelComponent rendering single object
Rendering single object is easiest way to render data from api but the weakness of render in this way, if we have multiple objects with various type of components we want to display in the same page.
Api will call with X times multiply by total objects. In this case we needs to apply GlobalLoader to load whole data as a list.
This example will show the step to apply GlobalLoader into existing components.
- First step define the const to handle what api loader does. Input of the const will be array of id.
const OrganizationApiList = IDs console return Id: '1234' Name: 'Organization Name List: 1234' SurName: 'SurName List: 1234' Abbv: 'ABBv' Id: '1235' Name: 'Organization Name List: 1235' SurName: 'SurName List: 1235' Id: '1236' Name: 'Organization Name List: 1236' SurName: 'SurName List: 1236'
- Create Global config with RMCGlobalLoader Component and set config through props
const GlobalLoader = props <RMCGlobalLoader = >propschildren</RMCGlobalLoader>
- Wrap existings with loader object
<GlobalLoader = > <OrganizationObject ="1234"> <b><OrganizationField ="Name"/></b> <OrganizationField ="SurName"/> <b><OrganizationField ="Abbv"/></b> </OrganizationObject></GlobalLoader>
Input id will pass by props name and the name of props refers with moduleName follow with string 'Id'
Examples: moduleName = Organization , props name of organization's id is OrganizationId
Multiple data tpye can be loaded from single loader also.
<GlobalLoader = => <OrganizationObject ="1234"> Organization 1234 name : <b><OrganizationField ="Name"/></b> <br/> Organization 1234 surname : <OrganizationField ="SurName"/> <br/> </OrganizationObject> <CompanyObject ="asdf"> CompanyName : <CompanyField ="Name"/> <br/> </CompanyObject> <OrganizationObject ="1234"> Organization 1234 name : <b><OrganizationField ="Name"/></b> <br/> Organization 1234 surname : <OrganizationField ="SurName"/> <br/> </OrganizationObject></GlobalLoader>
Example
import React Component from 'react';import RMCModel RMCModelElement RMCGlobalLoader from 'react-model-component'; const GlobalLoader = props <RMCGlobalLoader = >propschildren</RMCGlobalLoader> /* Organization configuration */ const OrganizationObject = props <RMCModel ="Organization" = ="Id" >propschildren</RMCModel>const OrganizationField = props <RMCModelElement ="Organization" >propschildren</RMCModelElement> /* CompanyLoader configuration */ const CompanyObject = props <RMCModel ="Company" = >propschildren</RMCModel>const CompanyField = props <RMCModelElement ="Company" >propschildren</RMCModelElement> const initialState = loading :false { ; thisstate = ...initialState ; } { this } { return <div ="App"> <div>With loader</div> <GlobalLoader = > <OrganizationObject ="1234"> <b><OrganizationField ="Name"/></b> <OrganizationField ="SurName"/> <b><OrganizationField ="Abbv"/></b> </OrganizationObject> <br/> <OrganizationObject ="1236"> <b><OrganizationField ="Name"/></b> <OrganizationField ="SurName"/> <b><OrganizationField ="Name"/></b> </OrganizationObject> </GlobalLoader> <div>Without loader</div> <OrganizationObject ="1235"> <b><OrganizationField ="Name"/></b> <OrganizationField ="SurName"/> </OrganizationObject> <div>Multi loader</div> <GlobalLoader = => <OrganizationObject ="1234"> Organization 1234 name : <b><OrganizationField ="Name"/></b> <br/> Organization 1234 surname : <OrganizationField ="SurName"/> <br/> </OrganizationObject> <CompanyObject ="asdf"> CompanyName : <CompanyField ="Name"/> <br/> </CompanyObject> <OrganizationObject ="1234"> Organization 1234 name : <b><OrganizationField ="Name"/></b> <br/> Organization 1234 surname : <OrganizationField ="SurName"/> <br/> </OrganizationObject> </GlobalLoader> </div> ; } const CompanyLoaderApiList = IDs console return id: 'asdf' Name: 'Company: asdf' id: 'ghjk' Name: 'Company: ghjk' const CompanyApiDetail = ID console return id: 'asdf' Name: 'Company: asdf' const OrganizationApiList = IDs console return Id: '1234' Name: 'Organization Name List: 1234' SurName: 'SurName List: 1234' Abbv: 'ABBv' Id: '1235' Name: 'Organization Name List: 1235' SurName: 'SurName List: 1235' Id: '1236' Name: 'Organization Name List: 1236' SurName: 'SurName List: 1236' const OrganizationApiDetail = ID console return Id: ID Name: 'Name detail '+ID SurName: 'SurName detail '+ID ;
License
Pornprasith Mahasith © jeurboy