JavaScript Is it bad form to call `createElement` manually?

I'm using JSX everywhere in my React project because of course I am. But there are a few instances where using createElement is particularly helpful.

Here's one such example, which is representative of my typical use case. I have a reasonably-sized string that I want to display in a <pre> tag, except that I want all instances of the \r (0x0D) character to be displayed as <CR> in a different color. I can easily accomplish that as follows

const splitOnCR = stdin.split("\r");

const joinerStyle = {
    color: "#337ab7",  // Bootstrap primary color
}
const joiner = <tt style={joinerStyle}>{"<CR>"}</tt>;

let parts = new Array(splitOnCR.length * 2 - 1);
for (let i = 0; i < parts.length; i++) {
    if (i % 2 === 0) {
        parts[i] = splitOnCR[i / 2];
    } else {
        parts[i] = joiner;
    }
}

const style = {
    fontFamily: "monospace",  // override Menlo or whatever
};
return React.createElement(
    "pre",
    {style},
    ...parts,
    <span>&nbsp;</span>,  // show final newlines, if any
);

which indeed produces

the result of the above code, showing both a correctly processed carriage return and the literal text "<CR>"; the latter is not colored

The alternative that I see here would be to generate an individual <tt> or <span> or whatever for every character in the string and then render them all in an array. I don't want to do that. It would make me sad.

I've inspected the DOM while changing the string, and it looks like React will happily change only the last text node as I append to the string, and it only creates a single extra <tt> when I add a carriage return. This is as I expected (and hoped).

But I can also imagine that this variadic call to createElement might confuse the diffing algorithm in some way—after all, we usually need keys when rendering out arrays. I just don't know what that is or what the effect would be.

So—is this safe? Is it good? Any other approaches?

Answer:1

I have found similar to my issue here. But I have a little bit different scenario. I have string of html rather than just string. So, I wanted to do: Let's suppose MyComponent is just returning h3 ...

I have found similar to my issue here. But I have a little bit different scenario. I have string of html rather than just string. So, I wanted to do: Let's suppose MyComponent is just returning h3 ...

  1. convert react component to html
  2. convert react component to pdf
  3. convert react component to typescript
  4. convert react component to image
  5. convert react component to string
  6. convert react component to vue
  7. convert react component to angular
  8. convert react component to functional component
  9. convert react component to react native
  10. convert react component to web component
  11. convert react component to hooks
  12. convert react component to png
  13. convert react component to json
  14. convert react component to javascript
  15. convert react component to dom element
  16. convert react component to element
  17. convert react component to npm package

I did some research on how to remove logo which removes the Vimeo logo on the bottom right corner. But I want to remove the user logo and author from embedded vimeo video. Is this possible? If yes how ...

I did some research on how to remove logo which removes the Vimeo logo on the bottom right corner. But I want to remove the user logo and author from embedded vimeo video. Is this possible? If yes how ...

  1. vimeo hide user

I have a pretty straightforward page in WordPress, which works fine outside the WordPress framework, but once I integrate it into a WordPress theme it refuses to re-size a element. The rest of the ...

I have a pretty straightforward page in WordPress, which works fine outside the WordPress framework, but once I integrate it into a WordPress theme it refuses to re-size a element. The rest of the ...

  1. dynamic image resizing javascript
  2. javascript dynamic resizing

I am trying to learn Angular JS with an HTML Sample. I would like the user to fill out some basic information, and based on the checkbox they select, it will load a form page using the UI Routing. It ...

I am trying to learn Angular JS with an HTML Sample. I would like the user to fill out some basic information, and based on the checkbox they select, it will load a form page using the UI Routing. It ...

  1. angularjs routing based on condition
  2. angularjs dynamic routing-based approach
  3. angularjs role based routing
  4. angularjs component based routing
  5. angularjs state based routing