Hi All:
I have run into an error while studying the Kirupa React Book.
In 'JSX Quirks" on p.83 there is a discussion about using <></> as a shortcut for <React.Fragment>. I get a Babel error when I use this syntax. Code is below. Corresponding web page is https://www.kirupa.com/react/meet_jsx_again.htm
Help will be appreciated.
Thanks.
class ShortHandFragment extends React.Component {
render() {
return (
<>
<p>___</p>
<p>I am</p>
<p>returning a Fragmented list</p>
<p>using the shorthand tag.</p>
</>
);
}
};
Thanks for the quick reply.
At this point in the book, everything is in a single HTML doc and the libraries are imported from unpkg.com. There are no individual imports of packages in the tutorial thus far.
I think it is because you are using script tags that import react which makes you not able to import Fragment globally so that you can use the short version.
Because you can do this, you just need to place React.Fragment globally.
<body>
<div id="container">If you see this there is a React Error</div>
<script type="text/babel">
// JSX is treated like JavaScript
// The values returned can be dynamically generated. Expressions are wrapped in curly braces.
// The card component will act as the the container that our Swatch and Label components will live in. Created with js class.
var x =React.Fragment;
class ShortHandFragment extends React.Component {
render() {
return (
<x>
<p>___</p>
<p>I am</p>
<p>returning a Fragmented list</p>
<p>using the shorthand tag.</p>
</x>
);
}
};
ReactDOM.render(
<ShortHandFragment />,
document.querySelector("#container")
);
</script>
</body>
That works!
I presume that as I get further into this I won’t run into this problem, but I hate getting stuck when I’m following a tutorial…
Thanks a lot for taking the time to help!
Yes. That works. The links to the CDN are different in your codepen than in the tutorial. I copied them to my file and they work. Code pasted below.
Thanks!
This has to do with a change in Babel that was fixed after printing.
I will mark this as something to fix in a future printing. Sorry about that. The moving of things inside the libraries is the biggest problem when writing a book on a 3rd party framework like React.
Thank you.
This is the first framework that I’ve studied. I’m only at the beginning of the book and this snafu already provides an example of one of the cons of using these frameworks - maintaince requires keeping up with the changes over which one has no control. I appreciate your discussion on this issue in A Future without React.
The <> syntax is the only major experimental addition I talk about in the book. You shouldn’t run into any mismatches when talking about the more fundamental APIs that haven’t changed in a long time. Also, check this page for any additional issues that may or may not be fixed in the edition of the printing you are looking at: https://www.kirupa.com/react/errata.htm
I do look forward to a world where some of the best ideas from frameworks like React make their way into the core Web APIs.
Thanks,
Kirupa
Creating engaging and entertaining content for designers and developers since 1998.