Professional Documents
Culture Documents
COSC4220 Lec3 ReactWJSX
COSC4220 Lec3 ReactWJSX
JSX is just another way of creating React elements, avoiding the need to stress
over a missing comma in a create Element call
React elements as JSX
JSX was released by facebook with React to provide concise syntax for creating
complex DOM trees with attributes
An element’s children can be added between the opening and closing tags
● Either a string
● A JavaScript expression
But, there are a few considerations that need to be understood when working with
JSX
● Nested components
● Classname
● Javascript expressions
● Evaluation
Nested components
JSX allows components to be added as
children of other components
As the browser interprets the code the JS doesn’t need compiled, though not
every browser supports the latest JS syntax and none support JSX
To convert, or compile, cutting edge JS and JSX into something the browser can
actually interpret a tool called Babel exists
Babel
Babel is used in production at Facebook, Netflix, PayPal, Airbnb and more
There are many ways of working with Babel, the easiest being to include a link to
the Babel CDN directly in HTML
This will compile any code in script blocks that have a type “text/babel”
Babel will compile the source code on the client before running
While not the most ideal of production solutions, it’s a great to start with JSX
Babel
Recipes as JSX
JSX provides a nice, clean way to
express react elements in code
It will add each field of the recipe object as a property of the Recipe component
This shortcut will provide all the properties to the recipe component
While this may be what you want, it might also be more than you want
Recipes as JSX
The Menu function can stand further
improvements as well
Suppose you want to create a component for a cat that renders to the DOM at root
React fragments
The previous renders as one would hope
● create-react-app
● Gatsby
● Code sandbox
● Code splitting
● Minification
● Feature flagging
● Hot Module Replacement (HMR)
The recipes app from earlier ahs some issues, webpack can help to alleviate them
Using a tool like webpack allows teams to work together on large-scale projects
Intro to webpack
In addition to the previous, using the webpack module bundler can add the
following benefits:
● Modularity
● Composition
● Speed
● Consistency
Creating a project
While create-react-app helps to automate the process, it’s good to understand the
basics first
Next, the project needs to be initialized and the package.json created with npm
● npm init -y
● npm install react react-dom serve --save
Creating a project
Next you need to create the file structure of your
applications
Within you app directory create two more directories src and
components
Within src add:
● recipes.json
Within components add:
● Recipe.js
● Instructions.js
● Ingredients.js
Break components into modules
If you recall from earlier the recipe component
is handling a lot:
A better solution would be to break it down into smaller, more focused function
components and compose them together
The first step would be to pull instructions out to their own component and create a
module in a separate file
The build process will be built with webpack, and that will put everything back
together in a single file
Splitting the files out makes larger projects easier to manager on the developer
end, and makes testing easier
Creating the webpack build
In order to create a static build process with web pack, a few things need to be
installed “npm install --save webpack webpack-cli”
For our modularity to work webpack needs to know how to bundle the source code
into a single file
Wherever webpack finds an import statement it will find the associated module in
the filesystem and include it in the bundle
Webpack will follow this import tree and include all of the necessary modules
Creating the webpack build
The traversal Webpack does going through the import tree is known as a
dependency graph
Each thing needed is a point on a graph, and Webpack draws the paths between
the circles
● babel-loader
● @babel/core
Webpack is run statically, typically bundles are created before the app is deployed
to a server it can be run with “npx webpack --mode development”