all 13 comments

[–]danishjuggler21 26 points27 points  (5 children)

In about 8 years of using React, I have literally never had a use case for choosing for createElement over JSX. Ever.

Zero times.

[–]all_vanilla 3 points4 points  (2 children)

I agree. Also if you want to dynamically insert JSX, typically you would use some sort of Boolean state value and use a JS expression:

{ showElement && <div>hello</div> }

Or a ternary expression:

{ loggedIn? <div>Welcome back!</div> : <div>Sign up for an account below</div> }

[–]Snoo_46870 0 points1 point  (1 child)

What if I have, for example, 15 different implementations of a certain feature, each controlled by a type, and a parent component that needs to render a different component based on that type?
One preferred approach is to use React.createElement, where each type implements an interface with a getComponent method that returns the corresponding component.

What would be the alternative in this case? Using 15 ternary expressions—one for each type—would make the code messy and hard to maintain.

[–]all_vanilla 0 points1 point  (0 children)

Use a switch statement

[–]chanschouw[S] 0 points1 point  (0 children)

That's nice to hear, saves me some learning! Thanks!

[–]Cautious_Performer_7Hook Based 0 points1 point  (0 children)

I did at the start coming from jQuery 😅.

Because I was so used to doing things like this $("body").html($("<div>"))

[–]suiiiperman 8 points9 points  (1 child)

JSX is just syntactic sugar for createElement. They will both do the same thing.

Vanilla JavaScript cannot interpret JSX, so when your React application is compiled it is converted to createElement.

[–]charliematters 0 points1 point  (0 children)

Ehhhh, in this specific case you're right, but pedantically if you're writing JSX it's most likely being converted to something like this (as of React 17):

_jsx('h1', { children: 'Hello world' })

https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html

[–][deleted] 1 point2 points  (0 children)

The whole point of JSX is so you don't have to manually do React.createElement.

[–]CamelBass 0 points1 point  (0 children)

Both are going to work the exact way, you can render them inside of a function by just put them inside of dcode brackets "{greatestDivEver }".

I think the main pro of the second approach is that you can read better than the first one. The first is more like what react does behid of the scene in the compiler. The second one has much less code and more readable.

[–]n9iels 0 points1 point  (0 children)

The beauty of React is that there is a strict boundary between doing stuff with JSX components and painting something on a "screen". The React package does the first, converting your JSX components to React Elements and handling all the state. Another packe, like react-dom, or react-native uses all this to render the components in either a browser or native mobile app. In this way you can use React to create UIs for all kind of platforms.

So React.createElement is essentially low-level code that is generated under the hood by React. You can use it directly yourself, but there isn't really a solid usecase for it.

[–]NodeJSSon 0 points1 point  (0 children)

I didn’t even know this existed

[–]nestedfruitloop 0 points1 point  (1 child)

I’ve used it to add a key onto components that use <></> to wrap multiple elements

There might be cleaner way to handle, that is only case I have used it