How To Render A Component Dynamically Based On A JSON Config - Pluralsight
How To Render A Component Dynamically Based On A JSON Config - Pluralsight
How to Render a
Gaurav Singhal Component
Dynamically Based
on a JSON Config
Gaurav Singhal
Feb 20, 2020 • 11 Min read • 1,136 Views
Introduction
Introduction
React makes it super easy to create and
3
JSON config
Let's take a look at the JSON config
data and its format.
js
1 const CardConfig = {
2 component: "card",
3 children: [
4 {
5 component: "img",
6 src:
7 "https://images.pexels.com/p
8 },
9 {
10 component: "body",
11 children: [
12 {
13 component: "title",
14 children: "This is a title
15 },
16 {
17 component: "subtitle",
18 children: "This is the sub
19 },
20 {
21 component: "text",
22 children:
23 "Some quick example text
24 },
25 {
26 component: "button",
27 children: "Click Me!"
28 }
29 ] did this guide solve
How well 2
We use cookies to make interactions 30 }
with our websites and services easy and
your problem?
31 ]
meaningful. For more information about Disable cookies Accept cookies and close this message
the cookies we use or to find out how32 };
you can disable cookies, click here.
https://www.pluralsight.com/guides/how-to-render-a-component-dynamically-based-on-a-json-config 2/14
02/05/2020 How to Render a Component Dynamically Based on a JSON Config | Pluralsight
html
1 <div class="card">
2 <img
3 src="https://images.pexels.com/p
4 class="card-img"
5 />
6 <div class="card-body">
7 <div class="card-title">This is
8 <div class="card-subtitle">This
9 <p class="card-text">
10 Some quick example text to bui
11 the card's content.
12 </p>
13 <button class="btn btn-secondary
14 </div>
15 </div>well did this guide solve
How 2
We use cookies to make interactions
with our websites and services easy and
your problem?
meaningful. For more information about Disable cookies Accept cookies and close this message
the cookies we use or to find out how
you can disable cookies, click here.
https://www.pluralsight.com/guides/how-to-render-a-component-dynamically-based-on-a-json-config 3/14
02/05/2020 How to Render a Component Dynamically Based on a JSON Config | Pluralsight
js
1 const KeysToComponentMap = {
2 card: Card,
3 img: CardImg,
4 text: CardText,
5 body: CardBody,
6 title: CardTitle,
7 subtitle: CardSubtitle,
8 button: Button
9 };
jsx
1 import {
2 Card,
3 CardImg,
4 CardText,
5 CardBody,
6 CardTitle,
7 CardSubtitle,
8 Button
9 } from "reactstrap";
KeysToComponentMap object.
jsx
1 const renderer = config => {
2 if (typeof KeysToComponentMap[confi
3 // ..
4 }
5 };
React.createElement()
js
1 React.createElement(
2 type,
3 [props],
4 [...children]
5 );
jsx
1 let WelcomeText = React.createElement
2 "h1",
3 { style: { color: "blue" } },
4 "This is a welcome text"
5 );
html
1 <h1 style="color:blue">
2 This is a welcome text
3 </h1>
jsx
1 function renderer(config) {
2 if (typeof KeysToComponentMap[confi
3 return React.createElement(KeysTo
4 }
5 }
How well did this guide solve 2
We use cookies to make interactions
with our websites and services easy and
your problem?
meaningful. For more information about Disable cookies Accept cookies and close this message
the cookies we use or to find out how
you can disable cookies, click here.
https://www.pluralsight.com/guides/how-to-render-a-component-dynamically-based-on-a-json-config 6/14
02/05/2020 How to Render a Component Dynamically Based on a JSON Config | Pluralsight
html
1 <div class="card-container">
2 <div class="card"></div>
3 </div>
jsx
1 config.children && config.children.ma
jsx
1 function renderer(config) {
2 if (typeof KeysToComponentMap[conf
3 Howreturn
well did this guide solve
React.createElement(
2
We use cookies to make interactions
4
with our websites and services easy and
your problem?
KeysToComponentMap[config.comp
meaningful. For more information about5 Disable cookies
{}, Accept cookies and close this message
the cookies we use or to find out how
you can disable cookies, click here. 6 {
https://www.pluralsight.com/guides/how-to-render-a-component-dynamically-based-on-a-json-config 7/14
02/05/2020 How to Render a Component Dynamically Based on a JSON Config | Pluralsight
jsx
1 function renderer(config) {
2 if (typeof KeysToComponentMap[conf
3 return React.createElement(
4 KeysToComponentMap[config.comp
5 {},
6 {
7 config.children &&
8 (typeof config.children ===
9 ? config.children
10 : config.children.map(c =>
11 }
12 );
13 }
14 }
jsx
1 function renderer(config) {
2 if (typeof KeysToComponentMap[conf
3 return React.createElement(
4 KeysToComponentMap[config.comp
5 {
6 src: config.src
7 },
8 {
9 config.children &&
10 (typeof config.children ===
11 ? config.children
12 : config.children.map(c =>
13 }
14 );
15 }
16 }
index.js
jsx
1 import React from "react";
2 import ReactDOM from "react-dom";
3 How well did this guide solve 2
We use cookies to make interactions 4
with our websites and services easy and
your problem?
import App from "./App";
5
meaningful. For more information about Disable cookies Accept cookies and close this message
the cookies we use or to find out how
you can disable cookies, click here.
https://www.pluralsight.com/guides/how-to-render-a-component-dynamically-based-on-a-json-config 10/14
02/05/2020 How to Render a Component Dynamically Based on a JSON Config | Pluralsight
App.js
jsx
1 import React from "react";
2 import "./styles.css";
3 import RenderCard from "./RenderCard
4 import "bootstrap/dist/css/bootstrap
5
6 const CardConfig = [
7 {
8 component: "card",
9 children: [
10 {
11 component: "img",
12 src:
13 "https://images.pexels.com
14 },
15 {
16 component: "body",
17 children: [
18 {
19 component: "title",
20 children: "This is a tit
21 },
22 {
23 component: "subtitle",
24 children: "This is the s
25 },
26 {
27 component: "text",
28 children:
29 "Some quick example te
30 },
31 {
32 component: "button",
33 children: "Click Me!"
34 }
35 ]
36 }
37 ]
38 }
39 ];How well did this guide solve 2
We use cookies to make interactions 40
with our websites and services easy and
your problem?
41 export default function App() {
meaningful. For more information about Disable cookies Accept cookies and close this message
the cookies we use or to find out how42 return (
you can disable cookies, click here.
https://www.pluralsight.com/guides/how-to-render-a-component-dynamically-based-on-a-json-config 11/14
02/05/2020 How to Render a Component Dynamically Based on a JSON Config | Pluralsight
43 <div className="App">
44 <div className="card-container
45 {CardConfig.map(config => Re
46 </div>
47 </div>
48 );
49 }
RenderCard.js
jsx
1 import React from "react";
2 import {
3 Card,
4 CardImg,
5 CardText,
6 CardBody,
7 CardTitle,
8 CardSubtitle,
9 Button
10 } from "reactstrap";
11
12 const KeysToComponentMap = {
13 card: Card,
14 img: CardImg,
15 text: CardText,
16 body: CardBody,
17 title: CardTitle,
18 subtitle: CardSubtitle,
19 button: Button
20 };
21
22 function renderer(config) {
23 if (typeof KeysToComponentMap[conf
24 return React.createElement(
25 KeysToComponentMap[config.comp
26 {
27 src: config.src
28 },
29 config.children &&
30 (typeof config.children ===
31 ? config.children
32 : config.children.map(c =>
33 );
34 }
How well did this guide solve 2
We use cookies to make interactions 35 }
with our websites and services easy and
your problem?
36
meaningful. For more information about Disable cookies Accept cookies and close this message
the cookies we use or to find out how37 export default renderer;
you can disable cookies, click here.
https://www.pluralsight.com/guides/how-to-render-a-component-dynamically-based-on-a-json-config 12/14
02/05/2020 How to Render a Component Dynamically Based on a JSON Config | Pluralsight
Conclusion
Creating and rendering components
dynamically can be very helpful when
the JSON config data is stored in a
database and retrieved by the app
during runtime. The downside to this
approach is that the client or the
browser must know which components
are going to be used so that it can map
with the React elements. Any change in
the JSON schema will break the
renderer() function.
LEARN MORE
How well did this guide solve 2
We use cookies to make interactions
with our websites and services easy and
your problem?
meaningful. For more information about Disable cookies Accept cookies and close this message
the cookies we use or to find out how
you can disable cookies, click here.
https://www.pluralsight.com/guides/how-to-render-a-component-dynamically-based-on-a-json-config 13/14
02/05/2020 How to Render a Component Dynamically Based on a JSON Config | Pluralsight
SOLUTIONS
Contact
WeSales (/product/contact-sales)
use cookies to make interactions with our websites and services easy and meaningful. For more
information about the cookies we use or to find out how you can disable cookies, click here (/privacy).