{"id":11823,"date":"2025-01-22T07:03:02","date_gmt":"2025-01-22T07:03:02","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11823"},"modified":"2025-01-22T07:03:02","modified_gmt":"2025-01-22T07:03:02","slug":"render-react-components-without-jsx-format","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react\/render-react-components-without-jsx-format","title":{"rendered":"How to Render React Component Without JSX Format?"},"content":{"rendered":"<p>To render React components dynamically without relying directly on JSX, you can utilize React&#8217;s createElement API. Your approach to maintaining a mapping (popupContent) of component names to components is a good start. Here&#8217;s how you can adjust your Popup component to work properly:<\/p>\n<p><strong>Sample Code:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nimport React, { Component } from 'react';\r\nimport Login from '..\/Login\/login.js';\r\nimport Logout from '..\/Logout\/logout.js';\r\n\r\nconst popupContent = {\r\n    Login: Login,\r\n    Logout: Logout,\r\n};\r\n\r\nclass Popup extends Component {\r\n    constructor(props) {\r\n        super(props);\r\n        this.state = {\r\n            componentName: null, \/\/ To hold the name of the component to render\r\n        };\r\n    }\r\n\r\n    componentDidUpdate(prevProps, prevState) {\r\n        const nameOfComponent = \"Login\"; \/\/ Example: This would come from Redux in your case\r\n\r\n        if (prevState.componentName !== nameOfComponent) {\r\n            this.setBodyPopup(nameOfComponent);\r\n        }\r\n    }\r\n    setBodyPopup(nameOfComponent) {\r\n        \/\/ Update state with the component name\r\n        this.setState({ componentName: nameOfComponent });\r\n    }\r\n    render() {\r\n        const { componentName } = this.state;\r\n\r\n        \/\/ Fetch the component class from popupContent using the componentName\r\n        const ComponentToRender = popupContent[componentName];\r\n\r\n        return (\r\n            &lt;div&gt;\r\n                {ComponentToRender \r\n                    ? React.createElement(ComponentToRender) \r\n                    : &lt;p&gt;No component to display&lt;\/p&gt;}\r\n            &lt;\/div&gt;\r\n        );\r\n    }\r\n}\r\nexport default Popup;\r\n<\/pre>\n<h3>Alternative with Functional Component and Redux<\/h3>\n<p>If you&#8217;re using Redux, you could achieve a cleaner implementation using a functional component and the useSelector hook:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nimport React from 'react';\r\nimport { useSelector } from 'react-redux';\r\nimport Login from '..\/Login\/login.js';\r\nimport Logout from '..\/Logout\/logout.js';\r\n\r\nconst popupContent = {\r\n    Login: Login,\r\n    Logout: Logout,\r\n};\r\n\r\nconst Popup = () => {\r\n    const componentName = useSelector((state) => state.popup.componentName); \/\/ Redux state\r\n    const ComponentToRender = popupContent[componentName];\r\n\r\n    return (\r\n        <div>\r\n            {ComponentToRender \r\n                ? <ComponentToRender \/> \r\n                : <p>No component to display<\/p>}\r\n        <\/div>\r\n    );\r\n};\r\nexport default Popup;\r\n<\/pre>\n<h2>Benefits of This Functional Approach:<\/h2>\n<ul>\n<li>Simpler and more concise.<\/li>\n<li>Redux integration directly into the component without lifecycle methods.<\/li>\n<li>Automatically re-renders when Redux state changes.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>To render React components dynamically without relying directly on JSX, you can utilize React&#8217;s createElement API. Your approach to maintaining a mapping (popupContent) of component names to components is a good start. Here&#8217;s how you can adjust your Popup component to work properly: Sample Code:- import React, { Component } from &#8216;react&#8217;; import Login from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11824,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11823"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11823"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11823\/revisions"}],"predecessor-version":[{"id":11825,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11823\/revisions\/11825"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11824"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}