Dynamically created React Components render as HTML even with capitalization

Jim

I'm trying to render dynamically named React components. I understand the JSX requires the variable name to be capitalized. However, when I map over my state and try to populate components, I get the following error:

Warning: <TextBlock /> is using uppercase HTML. Always use lowercase HTML tags in React.

I understand this, and capitalization seems to work in the child TextBlock if I don't use a map and type out directly in the render of the main class.

Main class:

import React from 'react';
import TextBlock from './Components/TextBlock';

class RenderView extends React.Component {
  constructor() {
    super();

    this.state = {
      blurbs: [
        {
          value: "Text value",
          tag: "h2",
          component: 'TextBlock'
        },
        {
          value: "lorem stuff adnfsaldkfn asdfl lkjasdflkj asdlfk  alskdjflaksdjf ",
          tag: "p",
          component: 'TextBlock'
        }
      ]
    }
  }

  render() {

    const componentArray = this.state.blurbs.map((blurb, index) => {
      const Tag = blurb.component;
      return <Tag obj={blurb} key={index} />;
    })

    return componentArray;
  }
}

Child component TextBlock:

import React from 'react';

export default function TextBox(props) {

  const Tag = props.obj.tag;

  const Output = <Tag>{props.obj.value}</Tag>

  return Output;
}

Checking the react chrome tools, it appears to be rendering as an html element. How do I get react to recognize these two blurbs are jsx elements?

Austin Greco

I'm not sure how dynamic you need it to be (if it has to be a string or it can always be a reference to a react component), but it should work if you use the component reference directly instead of a string:

this.state = {
  blurbs: [
    {
      value: "Text value",
      tag: "h2",
      component: TextBlock
    },

If you really need to do it with strings, then you could add the components to a map and render them that way:

const COMPONENTS = {
  TextBlock,
  // etc..
};

--

const Tag = COMPONENTS[blurb.component];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to dynamically load & render react components?

From Dev

React data components table does not render HTML

From Dev

How to render html dynamically?

From Dev

Dynamically Add React Components

From Dev

Dynamically inserted React Components

From Dev

Dynamically inserted React Components

From Dev

Render multiple components in React Router

From Dev

React render components from string

From Dev

Spread an array of components in render in React

From Dev

How to progressively render react components?

From Dev

Applying css to odd/even rows created dynamically

From Dev

Listen for events from dynamically created components

From Dev

Angular 2 position of dynamically created components

From Java

Dynamically building a render function in React

From Java

Dynamically add child components in React

From Dev

React component with dynamically created function

From Dev

React component with dynamically created function

From Dev

Event listener for dynamically created html

From Dev

CSS not apply for dynamically created html

From Dev

referencing a dynamically created element in html

From Dev

Render HTML in React Native

From Dev

Is there a way to render multiple React components in the React.render() function?

From Dev

Dynamically insert react components into another react component

From Dev

Generate React components in PHP and then render in client

From Java

how to render react components by using map and join

From Java

Rendering React components with promises inside the render method

From Dev

react.js render components at different locations

From Dev

Render two components adjacent to each other in React

From Dev

Render multiple React components into a single DOM element

Related Related

HotTag

Archive