{"id":330211,"date":"2020-12-09T15:29:07","date_gmt":"2020-12-09T23:29:07","guid":{"rendered":"https:\/\/css-tricks.com\/?page_id=330211"},"modified":"2020-12-11T17:19:46","modified_gmt":"2020-12-12T01:19:46","slug":"199-messing-with-jsx","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/video-screencasts\/199-messing-with-jsx\/","title":{"rendered":"#199: Messing with JSX"},"content":{"rendered":"\n

I probably should have learned this long ago, but alas, here we are. Turns out you can tell what function you want JSX to use. Yep, JSX really only has one primary transformation that it does. It turns angle brackets in JavaScript into a function call. So, if you write a line like this in JavaScript<\/em>:<\/p>\n\n\n\n

<div class=\"big\">Hello<\/div><\/code><\/pre>\n\n\n\n

After processing (probably with Babel and the JSX plugin), you’ll get, by default:<\/p>\n\n\n\n

React.createElement(\"div\", { class: \"big\" }, \"Hello\");<\/code><\/pre>\n\n\n\n\n\n\n\n

But if you include a directive comment telling JSX you want to use your own function, you can change that output:<\/p>\n\n\n\n

\/* @jsx myFunction *\/\n<div class=\"big\">Hello<\/div><\/code><\/pre>\n\n\n\n

Turns into:<\/p>\n\n\n\n

\/* @jsx myFunction *\/\nmyFunction(\"div\", { class: \"big\" }, \"Hello\");<\/code><\/pre>\n\n\n\n

That means we can write our own function. Kinda weird, but OK.<\/p>\n\n\n\n