<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tayte Stokes</title>
    <description>The latest articles on DEV Community by Tayte Stokes (@taytestokes).</description>
    <link>https://dev.to/taytestokes</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F308437%2F08f172f3-6d8e-4534-adfc-c1f158249298.png</url>
      <title>DEV Community: Tayte Stokes</title>
      <link>https://dev.to/taytestokes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/taytestokes"/>
    <language>en</language>
    <item>
      <title>React and Webpack with Phoenix 1.6</title>
      <dc:creator>Tayte Stokes</dc:creator>
      <pubDate>Tue, 23 Aug 2022 19:12:32 +0000</pubDate>
      <link>https://dev.to/taytestokes/react-and-webpack-with-phoenix-16-3le6</link>
      <guid>https://dev.to/taytestokes/react-and-webpack-with-phoenix-16-3le6</guid>
      <description>&lt;p&gt;This post is originally posted at &lt;a href="https://www.afterthoughts.dev/react-and-webpack-setup-with-phoenix-1.6"&gt;afterthoughts.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recently as of Phoenix v1.6, the framework replaced bootstrapping the application assets using Node, Npm, and Webpack with Esbuild. There are many good reasons for this change and this &lt;a href="https://fly.io/blog/phoenix-moves-to-esbuild-for-assets/"&gt;blog post&lt;/a&gt; authored by Mark Ericksen is an easy read that highlights why this change was made.&lt;/p&gt;

&lt;p&gt;However, if you are like me and have been using Webpack as the choice of bundler for some time now and aren't ready to give it up just yet, then you're in the right place.&lt;/p&gt;

&lt;p&gt;In this post I'll be going over how to setup a new application using Phoenix v1.6 that will render a single page application for the frontend that utilizes Webpack as the asset bundler, Babel as the Javascript compiler, and React as the view library.&lt;/p&gt;

&lt;p&gt;We will also go a little further and demonstrate how to configure client side routing using React Router as well as how to have the frontend React application communicate with the backend Phoenix server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating A New Phoenix Application
&lt;/h2&gt;

&lt;p&gt;I'm assuming that your machine is already setup with Node, Elixir, and Phoenix so we won't be covering how to get your machine setup for development.&lt;/p&gt;

&lt;p&gt;First, let's create a new Phoenix project using a mix task and provide the flag telling Phoenix not to generate frontend assets.&lt;/p&gt;

&lt;p&gt;If you are interested, you can read more about what options are available to pass with this command &lt;a href="https://hexdocs.pm/phoenix/Mix.Tasks.Phx.New.html"&gt;here&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mix phx.new --no-assets
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now with the new Phoenix application created, we are ready to get started building out the frontend portion of our application using Webpack, React, and Babel.&lt;/p&gt;

&lt;p&gt;However, before we start building out the frontend, I think it's important to understand how Phoenix serves the static assets that make up the frontend.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Phoenix Serves Static Assets
&lt;/h2&gt;

&lt;p&gt;Phoenix handles serving static assets by using the Plug.Static plug. This plug is be found inside the Endpoint module that every request that is made to the server goes through.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## lib/example_web_app/endpoint.ex

plug Plug.Static,
    at: "/",
    from: :example_app,
    gzip: false,
    only: ~w(assets fonts images favicon.ico robots.txt)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Plug.Static plug will serve static assets from the &lt;code&gt;priv/static&lt;/code&gt; directory of the application.&lt;/p&gt;

&lt;p&gt;If you look at the snippit above, the Plug.Static plug has a few configuration options that are being set by default that are important to call out.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;at&lt;/code&gt; configuration defines where to reach for static assets, in our case this will be the default request path at &lt;code&gt;/&lt;/code&gt;. This needs to be a string.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;from&lt;/code&gt; option defines the file path to read the static assets from. This will be an atom that represents that applications name where assets will be served from the &lt;code&gt;priv/static&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;only&lt;/code&gt; option is used to filter which requests to serve. This is useful to prevent file system access on every request when the &lt;code&gt;at&lt;/code&gt; option is set to the default path of &lt;code&gt;/&lt;/code&gt;. This will take a list of folder and file names that exist inside the &lt;code&gt;priv/static&lt;/code&gt; folder that will only be served by the Plug.Static plug.&lt;/p&gt;

&lt;p&gt;If you're interested, you can read more about the configuration options available for the Plug.Static plug &lt;a href="https://hexdocs.pm/plug/Plug.Static.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As of version 1.6, Phoenix uses Esbuild to prepare assets that need to be preprocessed and extract them to the &lt;code&gt;priv/static/assets&lt;/code&gt; directory. This file migration happens during development mode using a watcher and in production by running a deploy script.&lt;/p&gt;

&lt;p&gt;Instead of Esbuild, we will be using Webpack to prepare our assets and migrate the processed assets to the &lt;code&gt;priv/static/assets&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Now with that in mind, let's move on and finally start building our frontend portion of the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Initializing The Frontend Directory
&lt;/h2&gt;

&lt;p&gt;The standard convention for a Phoenix application is to store all frontend code in the &lt;code&gt;assets&lt;/code&gt; folder, so we will be using that folder to build our fronntend application in.&lt;/p&gt;

&lt;p&gt;Now let's set this folder up as it's own project with npm and get a &lt;code&gt;package.json&lt;/code&gt; file created to manage the dependencies and scripts that the frontend application will rely on.&lt;/p&gt;

&lt;p&gt;Inside of the &lt;code&gt;assets&lt;/code&gt; folder, execute the npm command to initialize the folder as it's own project and pass it the &lt;em&gt;-y&lt;/em&gt; flag to accept all of the default configuration for the package.json file that will be created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should now have a &lt;code&gt;package.json&lt;/code&gt; file inside of the &lt;code&gt;assets&lt;/code&gt; folder that looks like the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## assets/package.json

{
  "name": "assets",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We won't worry about modifying the &lt;code&gt;package.json&lt;/code&gt; for now and leave as it is.&lt;/p&gt;

&lt;p&gt;The only thing left to finish initializing our frontend application is to define the entry point file for our frontend application.&lt;/p&gt;

&lt;p&gt;Since we are planning on turning this into a React application, we will follow some common React patterns and store all of our application code in a &lt;code&gt;src&lt;/code&gt; folder that sits next to all of the configuration files. Go ahead and create an &lt;code&gt;index.js&lt;/code&gt; file that will live in the &lt;code&gt;src&lt;/code&gt; folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## assets/src/index.js

console.log("Hello, world!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In tradition with every new web application, it's just a simple script that logs "Hello, world!" to the console. We will be coming back and updating this file later once we start implementing React.&lt;/p&gt;

&lt;p&gt;With that in place, we can start installing the dependencies we will need and configure the build tools for our frontend application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up Webpack and Babel
&lt;/h2&gt;

&lt;p&gt;This isn't a comprehensive guide around Webpack and Babel so I won't be going into too much detail about configuring these tools, but I will be highlighting the important parts that are related to what makes them work with our Phoenix application.&lt;/p&gt;

&lt;p&gt;In order for us to start using Webpack and Babel, we need to install the required dependencies from npm.&lt;/p&gt;

&lt;p&gt;Inside of the &lt;code&gt;assets&lt;/code&gt; directory, go ahead and execute the following command to install the dependencies we need for the Webpack and Babel configurations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install webpack webpack-cli @babel/core @babel/preset-env babel-loader css-loader style-loader url-loader --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we configure Webpack and Babel, let's do some good samaritan work annd make sure that we ignore pushing the node modules to the cloud. Add the file path to the &lt;code&gt;node_modules&lt;/code&gt; folder in the &lt;code&gt;assets&lt;/code&gt; directory to the &lt;code&gt;.gitignore&lt;/code&gt; that exists at the root of the Phoenix application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## .gitignore

/assets/node_modules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's add a &lt;code&gt;.babelrc&lt;/code&gt; and add the appropriate Babel presets that we will need to help compile our Javascript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## assets/.babelrc

{
  "presets": ["@babel/preset-env"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's time to add the config file for Webpack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## assets/webpack.config.js

const path = require("path");

module.exports = {
  entry: {
    main: "./src/index.js",
  },
  output: {
    path: path.resolve(__dirname, "../priv/static/js"),
    filename: "[name].js",
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        },
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.(png|woff|woff2|eot|ttf|svg)$/,
        loader: "url-loader",
        options: { limit: false },
      },
    ],
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That should be good enough to get us going with creating a simple application. If you're absolutely dying to know more about these configurations you can check out the &lt;a href="https://webpack.js.org/"&gt;Webpack&lt;/a&gt; and &lt;a href="https://babeljs.io/"&gt;Babel&lt;/a&gt; docs.&lt;/p&gt;

&lt;p&gt;Above, we define that the entry point file for the frontend application is that one that we created earlier which is &lt;code&gt;assets/src/index.js&lt;/code&gt; and bundle which gets created from Webpack will be called &lt;code&gt;main&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;entry: {
    main: "./src/index.js",
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember earlier how we talked about how Phoenix uses the Plug.Static plug to serve assets for our application from the &lt;code&gt;priv/static&lt;/code&gt; directory?&lt;/p&gt;

&lt;p&gt;If you look at the Webpack config, you can see that we define that the output of the bundled Javascript for our frontend application should be dumped into that directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;output: {
    path: path.resolve(__dirname, "../priv/static/assets/js"),
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this configuration set in place, a &lt;code&gt;priv/static/assets/js/main.js&lt;/code&gt; file will be generated which houses the bundled Javascript code for our frontend application and will allow the Plug.Static to serve our bundled Javascript assets.&lt;/p&gt;

&lt;p&gt;Next we need to actually trigger Webpack to create a bundle when we spin up our development server and watch for any changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running Webpack During Development
&lt;/h2&gt;

&lt;p&gt;We want our frontend application to be bundled and watched for any changes during the development process. We can do this by using a Phoenix Watcher.&lt;/p&gt;

&lt;p&gt;The Phoenix Endpoint can be configured with a set of watchers that will run other processes alongside the main server process.&lt;/p&gt;

&lt;p&gt;You can find the configuration for the Endpoint for the development environment in the &lt;code&gt;config/dev.ex&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## config/dev.ex

config :example_app, ExampleAppWeb.Endpoint,
  http: [ip: {127, 0, 0, 1}, port: 4000],
  check_origin: false,
  code_reloader: true,
  debug_errors: true,
  secret_key_base: "qJGuNZL8BUCEA343B51qWkIVyl1MS7taLsOBamVOacWKR6pwRDTiSRtngLSqRAKR",
  watchers: []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can configure the &lt;code&gt;watchers&lt;/code&gt; option to execute a Node process to watch Webpack for bundling our frontend portion of the application when we run the &lt;code&gt;mix phx.server&lt;/code&gt; command to start our Phoenix server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;watchers: [
    node: [
      "node_modules/webpack/bin/webpack.js",
      "watch",
      "--mode",
      "development",
      cd: Path.expand("../assets", __DIR__)
    ]
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above, we are basically setting up the watcher to run the &lt;code&gt;node node_modules/webpack/bin/webpack.js watch --mode development&lt;/code&gt; script which will run Webpack in development mode and watch for any changes. The last part, which is the &lt;code&gt;cd&lt;/code&gt; option, tells the watcher which directory to run that script from.&lt;/p&gt;

&lt;p&gt;If you're not familiar with the &lt;code&gt;watch&lt;/code&gt; option for Webpack, you find out more about it &lt;a href="https://webpack.js.org/configuration/watch/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now when we start up our Phoenix application using &lt;code&gt;mix phx.server&lt;/code&gt;, we can validate Webpack is also being executed by a the watcher we created by checking the terminal logs and seeing a message that declares Webpack was succesfully compiled.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Generated example_app app
[info] Running ExampleAppWeb.Endpoint with cowboy 2.9.0 at 127.0.0.1:4000 (http)
[info] Access ExampleAppWeb.Endpoint at http://localhost:4000
asset main.js 1.21 KiB [compared for emit] (name: main)
./src/index.js 29 bytes [built] [code generated]
webpack 5.74.0 compiled successfully in 696 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using The Webpack Bundle
&lt;/h2&gt;

&lt;p&gt;Even though we are now generating a bundle for our frontend code, it still isn't being used since it's not being linked to the HTML file that will be delivered to the browser from our Phoenix application.&lt;/p&gt;

&lt;p&gt;When a client makes a request to our Phoenix application, we will return an HTML file that will contain the contents of our web page. We will need to include a &lt;code&gt;script&lt;/code&gt; tag that will reference our bundled Javascript code to embed into the HTML file.&lt;/p&gt;

&lt;p&gt;If you're not familiar with how Phoenix uses views, layouts, and templates to create HTML files, you can find out more about it from the &lt;a href="https://hexdocs.pm/phoenix/views.html#layouts"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We will inject that &lt;code&gt;script&lt;/code&gt; tag into the main layout of the application that wraps every template, which is the &lt;code&gt;root.html.heex&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;At the end of the &lt;code&gt;body&lt;/code&gt; tag in that layout file, go ahead and add this script tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## lib/example_app_web/templates/layout/root.html.heex

&amp;lt;body&amp;gt;
    &amp;lt;%= @inner_content %&amp;gt;

    &amp;lt;script defer type="text/javascript" src={Routes.static_path(@conn, "/assets/js/main.js")}&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are using the Routes path helper function provided by Phoenix to generate a path to the Javascript bundle that is created from Webpack.&lt;/p&gt;

&lt;p&gt;We also want to include this &lt;code&gt;script&lt;/code&gt; tag under the embedded &lt;code&gt;&amp;lt;%= @inner_content %&amp;gt;&lt;/code&gt; tag. This will be important later when we start adding React because we need the content for the template to be rendered first in order to render our React application.&lt;/p&gt;

&lt;p&gt;If you were to visit &lt;code&gt;http://localhost:4000&lt;/code&gt; in your browser and open the developer console, you would see the console log of "Hello, world!" that comes from the &lt;code&gt;assets/src/index.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up React
&lt;/h2&gt;

&lt;p&gt;Now that the bundle created by Webpack is being delivered to the client, let's start setting up the React application.&lt;/p&gt;

&lt;p&gt;Inside of the &lt;code&gt;assets&lt;/code&gt; folder, we need to install a few more dependencies to use React.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also want to get the Babel React presets to compile the React code correctly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install @babel/preset-react --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With those dependencies now installed, we start setting up the React application.&lt;/p&gt;

&lt;p&gt;Let's add the new Babel preset to the &lt;code&gt;assets/.babelrc&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## assets/.babelrc

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's revisit the &lt;code&gt;assets/src/index.js&lt;/code&gt; file and get it to render our React application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## assets/src/index.js

import React from "react";
import ReactDOM from "react-dom";

ReactDOM.render(&amp;lt;div&amp;gt;Hello from React!&amp;lt;/div&amp;gt;, document.getElementById("root"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That should be enough to get a very simple single element React application. However, we haven't actually created the elemennt that will be used as the anchor point to rendwer our React application. Let's get that added.&lt;/p&gt;

&lt;p&gt;Since the &lt;code&gt;lib/example_app_web/templates/page/index.html.heex&lt;/code&gt; tempalte file is the template that will be rendered in place of the &lt;code&gt;&amp;lt;%= @inner_content =&amp;gt;&lt;/code&gt; tag inside of the &lt;code&gt;lib/example_app_web/templates/layout/root.html.heex&lt;/code&gt; when a client makes a request to the default base route &lt;code&gt;/&lt;/code&gt; of the Phoenix application, it would be a good idea to put that element in that template.&lt;/p&gt;

&lt;p&gt;If you are unsure as of why this is the template file that will be rendered at that route, we can check out the Router module of the Phoenix application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## lib/example_app_web/router.ex

scope "/", ExampleAppWeb do
  pipe_through :browser

  get "/", PageController, :index
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the scope of the base route &lt;code&gt;/&lt;/code&gt;, we define that &lt;code&gt;get&lt;/code&gt; request made to the base route should be forwarded to the PageController's index action, which in turn delivers the associated &lt;code&gt;index.html&lt;/code&gt; template.&lt;/p&gt;

&lt;p&gt;Now let's add the element to the HTML template to use to render our React application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## lib/example_app_web/templates/page/index.html.heex

&amp;lt;div id="root" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you visit the application at &lt;code&gt;http://localhost:4000&lt;/code&gt;, you should now see that the string "Welcome from React!" is being rendered to the web page.&lt;/p&gt;

&lt;p&gt;Obviously this is an extremely simple React application, but all of the scaffolding is now in place to start building out a complex React application for the frontend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Client Side Routing
&lt;/h2&gt;

&lt;p&gt;Let's start expanding on the React application and build it out a little more to give it a better SPA experience by adding some client side routing with React Router.&lt;/p&gt;

&lt;p&gt;We need to install &lt;code&gt;react-router-dom&lt;/code&gt; as another dependency for our application. In the &lt;code&gt;assets&lt;/code&gt; folder, run the npm command to install it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install react-router-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now once that's installed, let's create a few more components for our React application to render per route. We'll create these components in a new folder called &lt;code&gt;components&lt;/code&gt;. These will be some simple components that render what page they would reflect in our application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## assets/src/components/Home.js

import React from "react";

export const Home = () =&amp;gt; {
  return &amp;lt;div&amp;gt;Home&amp;lt;/div&amp;gt;;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## assets/src/components/Dashboard.js

import React from "react";

export const Dashboard = () =&amp;gt; {
  return &amp;lt;div&amp;gt;Dashboard&amp;lt;/div&amp;gt;;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With those created, let's hop back to the entry point of our frontend application and setup the router and use those components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## assets/src/index

import React from "react";
import ReactDOM from "react-dom";

import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";

import { Home } from "./components/Home";
import { Dashboard } from "./components/Dashboard";

ReactDOM.render(
  &amp;lt;Router&amp;gt;
    &amp;lt;header&amp;gt;
      &amp;lt;nav role="navigation"&amp;gt;
        &amp;lt;ul&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;Link to="/"&amp;gt;Home&amp;lt;/Link&amp;gt;
          &amp;lt;/li&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;Link to="/dashboard"&amp;gt;Dashboard&amp;lt;/Link&amp;gt;
          &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
      &amp;lt;/nav&amp;gt;
    &amp;lt;/header&amp;gt;
    &amp;lt;Routes&amp;gt;
      &amp;lt;Route path="/" element={&amp;lt;Home /&amp;gt;} /&amp;gt;
      &amp;lt;Route path="/dashboard" element={&amp;lt;Dashboard /&amp;gt;} /&amp;gt;
    &amp;lt;/Routes&amp;gt;
  &amp;lt;/Router&amp;gt;,
  document.getElementById("root")
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now have a very simple navigation menu that allows you to navigate to different pages of the frontend application via client side routing. However, what happesn if you visit the dashboard page and refresh? Or try to visit a specific path through the url bar?&lt;/p&gt;

&lt;p&gt;Phoenix will receive this HTTP get request and try to route the request to a specific route setup in the Phoenix Router. We need to configure the Phoenix router to deliver all requests to our PageController's index action to render our React application which will then manage the client side routing.&lt;/p&gt;

&lt;p&gt;In the Phoenix Router, we can use a wildcard pattern in the route definition to tell the Router to catch all requests and send them to a specific controller action.&lt;/p&gt;

&lt;p&gt;In our case, we will configure Phoenix so that all requests made to the server that get processed through the &lt;code&gt;/&lt;/code&gt; scope, will be sent the Page Controllers index action by setting the route path to &lt;code&gt;/*path&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## lib/example_app_web/router.ex

scope "/", ExampleAppWeb do
  pipe_through :browser

  get "/*page", PageController, :index
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now every request made to the default scope, will render our React application.&lt;/p&gt;

&lt;p&gt;You can learn more about using this pattern in the routing &lt;a href="https://hexdocs.pm/phoenix/Phoenix.Router.html#module-routing"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Validate the refreshes and direct requests through the url bar will render our React application by visiting the &lt;code&gt;/dashboard&lt;/code&gt; route directly or through the navigation in the UI and refresh the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frontend and Backend Communication
&lt;/h2&gt;

&lt;p&gt;The last thing that we should address is the question "How do we send data from our Phoenix application to our frontend?".&lt;/p&gt;

&lt;p&gt;Not only will our Phoenix server deliver our frontend assets to the browser, but it will also act as an API that our frontend React application we can query over the network via HTTP.&lt;/p&gt;

&lt;p&gt;If you're not familiar with the lifecycle of a Phoenix request, then you should check out the &lt;a href="https://hexdocs.pm/phoenix/request_lifecycle.html"&gt;documentation&lt;/a&gt; that explains it.&lt;/p&gt;

&lt;p&gt;The first thing that we need to is modify the Phoenix routes to open a new scope specifically for API requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## lib/example_app_web/router.ex

scope "/api", ExampleAppWeb.Api do
  pipe_through :api

  get "/data", DataController, :index
end

scope "/", ExampleAppWeb do
  pipe_through :browser

  get "/*page", PageController, :index
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code block above, we added a new scope to handle API requests. It use the &lt;code&gt;ExampleAppWeb.Api&lt;/code&gt; module namespace to help create a standard namepsacing convention for creating the other views and controllers that will be used to handle API requests.&lt;/p&gt;

&lt;p&gt;It's important to note that we added this new &lt;code&gt;/api&lt;/code&gt; scope above the &lt;code&gt;/&lt;/code&gt; root scope. This is because if we have a request made to &lt;code&gt;/api&lt;/code&gt; we want to make sure that it get's processed through that scope and not the root scope.&lt;/p&gt;

&lt;p&gt;In the scope, we set up a basic route defined as &lt;code&gt;/data&lt;/code&gt; that will handle incoming &lt;code&gt;get&lt;/code&gt; requests. It uses the DataController's index action to handle the request, however that hasn't been created.&lt;/p&gt;

&lt;p&gt;Let's get that action and the controller created as well as the associated view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## lib/example_app_web/controllers/api/data_controller.ex

defmodule ExampleAppWeb.Api.DataController do
  use ExampleAppWeb, :controller

  @data [
    %{
      id: 1,
      title: "Test Data One",
    },
    %{
      id: 2,
      title: "Test Data Two",
    },
    %{
      id: 3,
      title: "Test Data Three",
    }
  ]

  def index(conn, params) do
    render(conn, "index.json", data: data)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## lib/example_app_web/views/api/data_view.ex

defmodule ExampleAppWeb.Api.DataView do
  use ExampleAppWeb, :view

  def render("index.json", %{data: data}) do
    render_many(data, __MODULE__, "datum.json")
  end

  def render("datum.json", %{data: datum}) do
    %{
      id: datum.id,
      title: datum.title
    }
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the controller and the view connfigured to send a response of data, we can make a HTTP request from our frontend to get that data.&lt;/p&gt;

&lt;p&gt;We will make this request in the &lt;code&gt;Dashboard&lt;/code&gt; component so when the component mounts to the browser, it will make that network request for the data and display it in the UI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## assets/src/components/Dashboard.js

import React from "react";

export const Dashboard = () =&amp;gt; {
  const [data, setData] = React.useState([]);

  React.useEffect(() =&amp;gt; {
    fetch("http://localhost:4000/api/data")
      .then((response) =&amp;gt; response.json())
      .then((data) =&amp;gt; setData(data));
  }, []);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Dashboard&amp;lt;/h1&amp;gt;
      &amp;lt;ul&amp;gt;
        {data.map((datum) =&amp;gt; {
          return &amp;lt;li key={datum.id}&amp;gt;{datum.title}&amp;lt;/li&amp;gt;;
        })}
      &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you start up the Phoenix application and visit &lt;code&gt;http://localhost:4000/dashboard&lt;/code&gt;, the data should be getting fetched and displayed browser.&lt;/p&gt;

&lt;p&gt;Our setup for a Phoenix application that uses Webpack to bundle assets and React as the library to manage the frontend application is now complete. Obviously, this was a pretty simple example of an application, but hopefully it has been helpful enough to get you kick started with building and application with this tech stack.&lt;/p&gt;

&lt;p&gt;You can find all of the code that was included in this post at this &lt;a href="https://github.com/taytestokes/webpack-react-phoenix-example"&gt;repo&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>javascript</category>
      <category>phoenix</category>
      <category>webpack</category>
    </item>
  </channel>
</rss>
