<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sam Shapiro on Medium]]></title>
        <description><![CDATA[Stories by Sam Shapiro on Medium]]></description>
        <link>https://medium.com/@samshapiro?source=rss-90bd6d24099c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*yhpCKN-1l8unJWssenrobw.png</url>
            <title>Stories by Sam Shapiro on Medium</title>
            <link>https://medium.com/@samshapiro?source=rss-90bd6d24099c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 10:25:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samshapiro/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Firebase + React Boilerplate Setup]]></title>
            <link>https://medium.com/@samshapiro/react-firebase-boilerplate-setup-1aa4357eca96?source=rss-90bd6d24099c------2</link>
            <guid isPermaLink="false">https://medium.com/p/1aa4357eca96</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-developer]]></category>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Sam Shapiro]]></dc:creator>
            <pubDate>Sun, 20 Aug 2023 20:07:22 GMT</pubDate>
            <atom:updated>2023-08-20T20:51:28.474Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VJVt2Jjn9D6jXOjQ8VpTuw.png" /></figure><p>Setting up a brand new Firebase project that works seamlessly out of the box with a React front-end was more painful than I anticipated, so I made this list of instructions for spinning up a new Firebase+React project as much for myself as for anyone else.</p><p>This guide also includes settings to allow for local development with hot-reloading and connection to the Firebase Emulators, <a href="https://medium.com/@samshapiro/firebase-local-development-with-firestore-cloud-functions-and-react-c29c188bd60e">as I detailed in this post 3.5 years ago</a>.</p><p><em>Note this uses the Web Modular API, not the Web Namespaced API. As of this writing (August 20th, 2023) both are still documented in Firebase docs, so for building on top of this boilerplate you’ll want to make sure to follow Web Modular API documentation.</em></p><ol><li>Initialize the React App &amp; Firebase project.</li></ol><pre>npx create-react-app .<br>firebase init<br># Select: firestore, functions, hosting<br># Select: Create new project</pre><p>2. Enable Firestore:</p><p>Open up the <a href="https://console.firebase.google.com/">Firebase Console UI</a>, select your newly created project, navigate to Firestore and click Create Database (start in test mode).</p><p>3. Create the Firebase Web App for your project:</p><pre>firebase init # need to re-run after enabling database<br># Select: firestore, functions, hosting<br># Accept all defaults<br># Do you want to install dependencies with npm now? Yes<br># What do you want to use as your public directory? build<br># Configure as a single-page app (rewrite all urls to /index.html)? No<br><br>firebase apps:create<br># Please choose the platform of the app: Web<br># This will spit out a new command out at the end, which you should then run:<br><br>firebase apps:sdkconfig WEB 1:abcdefghijk…<br># This will spit out a firebase config to use in the next step below.<br><br>npm install firebase</pre><p>4. In the src directory, create a file called firebase.js using the config that was spat out from the last firebase command in the previous step.</p><pre>import { initializeApp } from &quot;firebase/app&quot;;<br><br>// https://firebase.google.com/docs/functions/local-emulator#instrument-functions<br>import { getFunctions, connectFunctionsEmulator } from &quot;firebase/functions&quot;;<br><br>// https://firebase.google.com/docs/emulator-suite/connect_firestore#android_apple_platforms_and_web_sdks<br>import { getFirestore, connectFirestoreEmulator } from &quot;firebase/firestore&quot;;<br><br>const firebase = initializeApp({<br>  projectId: &quot;&quot;,<br>  appId: &quot;&quot;,<br>  storageBucket: &quot;&quot;,<br>  apiKey: &quot;&quot;,<br>  authDomain: &quot;&quot;,<br>  messagingSenderId: &quot;&quot;,<br>});<br>const functions = getFunctions(firebase);<br>const firestore = getFirestore(firebase);<br><br>if (window.location.hostname === &quot;localhost&quot;) {<br>  console.log(<br>    &quot;Testing locally: hitting local functions and firestore emulators.&quot;<br>  );<br>  connectFunctionsEmulator(functions, &quot;127.0.0.1&quot;, 5001);<br>  connectFirestoreEmulator(firestore, &quot;127.0.0.1&quot;, 8080);<br>}<br><br>export { functions, firestore };</pre><p>5. In firebase.json update the hosting.public value to build so that the deployed app serves the compiled build files outputted from react build .</p><pre>{<br>  // ...<br>  &quot;hosting&quot;: {<br>    &quot;public&quot;: &quot;build&quot;,<br>    // ...<br>  }<br>}</pre><p>6. Update functions/index.js with a boilerplate function to test and verify the front-end can properly call functions and interact with the database through them.</p><pre>const functions = require(&quot;firebase-functions&quot;);<br>const admin = require(&quot;firebase-admin&quot;);<br>const { FieldValue } = require(&quot;firebase-admin/firestore&quot;);<br><br>admin.initializeApp();<br><br>exports.addUser = functions.https.onCall((data, context) =&gt; {<br>  const { name } = data;<br><br>  const db = admin.firestore();<br><br>  return db<br>    .collection(&quot;users&quot;)<br>    .add({<br>      name,<br>      signupTimestamp: FieldValue.serverTimestamp(),<br>    })<br>    .then(() =&gt; {<br>      return { message: &quot;successfully added user&quot;, success: true };<br>    })<br>    .catch((error) =&gt; {<br>      throw new functions.https.HttpsError(&quot;unknown&quot;, error.message, error);<br>    });<br>});</pre><p>7. Update App.js to a simple React app that allows you to add users to the database.</p><pre>import logo from &quot;./logo.svg&quot;;<br>import &quot;./App.css&quot;;<br><br>import { useState } from &quot;react&quot;;<br>import { functions } from &quot;./firebase&quot;;<br>import { httpsCallable } from &quot;firebase/functions&quot;;<br><br>function App() {<br>  const [name, setName] = useState(&quot;&quot;); // State to hold the input value<br>  const [response, setResponse] = useState(&quot;&quot;); // State to hold the response message<br><br>  const handleAddUser = async () =&gt; {<br>    try {<br>      const addUserFunction = httpsCallable(functions, &quot;addUser&quot;);<br>      const result = await addUserFunction({ name });<br><br>      if (result.data.success) {<br>        setResponse(result.data.message);<br>        setName(&quot;&quot;); // Clear the name field on successful addition<br>      } else {<br>        setResponse(&quot;Failed to add user.&quot;);<br>      }<br>    } catch (error) {<br>      setResponse(&quot;An error occurred.&quot;);<br>      console.error(<br>        &quot;There was an error calling the addUser Firebase function&quot;,<br>        error<br>      );<br>    }<br>  };<br><br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;header className=&quot;App-header&quot;&gt;<br>        &lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;<br>        &lt;p&gt;<br>          Edit &lt;code&gt;src/App.js&lt;/code&gt; and save to reload.<br>        &lt;/p&gt;<br>        &lt;a<br>          className=&quot;App-link&quot;<br>          href=&quot;https://reactjs.org&quot;<br>          target=&quot;_blank&quot;<br>          rel=&quot;noopener noreferrer&quot;<br>        &gt;<br>          Learn React<br>        &lt;/a&gt;<br><br>        &lt;div className=&quot;user-input-section&quot;&gt;<br>          &lt;input<br>            type=&quot;text&quot;<br>            placeholder=&quot;Enter name...&quot;<br>            value={name}<br>            onChange={(e) =&gt; setName(e.target.value)}<br>          /&gt;<br>          &lt;button onClick={handleAddUser} disabled={!name.trim()}&gt;<br>            Add User<br>          &lt;/button&gt;<br>        &lt;/div&gt;<br><br>        &lt;h2&gt;{response}&lt;/h2&gt;<br>      &lt;/header&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>8. Test Locally:</p><p>In one terminal, run firebase emulators:start</p><p>In a second terminal, run npm start</p><p>Now your local web app should open up, and you should be able to verify the users were added in the Firebase Emulator Suite UI: <a href="http://127.0.0.1:4000/firestore/data/users/">http://127.0.0.1:4000/firestore/data/users/</a></p><p>9. Deploy and test in production:</p><pre># Compile build files in the build directory.<br>npm run build<br><br># Deploy<br>firebase deploy</pre><p>Now go to the deployed web app and test adding a few users, then verify they are added in your production Firestore database from the Firebase web console.</p><p>Hope this helps!</p><p><em>Note: the code above can also be found in a boilerplate repo I made here: </em><a href="https://github.com/sambshapiro/firebase-react-boilerplate">https://github.com/sambshapiro/firebase-react-boilerplate</a>. <em>However, everything you need to accomplish this is included in this article.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1aa4357eca96" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Firebase Local Development with Firestore, Cloud Functions and React]]></title>
            <link>https://medium.com/@samshapiro/firebase-local-development-with-firestore-cloud-functions-and-react-c29c188bd60e?source=rss-90bd6d24099c------2</link>
            <guid isPermaLink="false">https://medium.com/p/c29c188bd60e</guid>
            <category><![CDATA[firestore]]></category>
            <category><![CDATA[firebase-cloud-functions]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Sam Shapiro]]></dc:creator>
            <pubDate>Sun, 08 Mar 2020 20:44:57 GMT</pubDate>
            <atom:updated>2023-08-20T20:54:19.638Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>NEW POST (AUG ’23) FOR FIREBASE+REACT BOILERPLATE PROJECT: </strong><a href="https://medium.com/@samshapiro/react-firebase-boilerplate-setup-1aa4357eca96">https://medium.com/@samshapiro/react-firebase-boilerplate-setup-1aa4357eca96</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VxSE8w9ttk0Uykw_wdwoxA.png" /></figure><p>There’s a lot of commands, tools and articles available about testing Firebase’s Firestore and Cloud Functions locally, many of which are redundant and confusing. For example, there’s proxies, emulators, rewrite rules, the firebase serve command, etc.</p><p>But even with the new Firebase Emulator Suite, which aims to simplify this process, by default you need to build your project into static files for the emulator to work.</p><p>So if you’re using React (or generally a bundler like Webpack) and don’t want to rebuild your project every time you make a change, what should you use for the simplest, fastest local development and testing with Firestore &amp; Firebase Cloud Functions?</p><p>The simplest way of doing local development with Firestore &amp; Firebase Cloud Functions that I’ve found is the following:</p><ol><li>In your firebase.js file, add the following block of code:</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a15196da011ecd9b7f8aed5d99bda1b5/href">https://medium.com/media/a15196da011ecd9b7f8aed5d99bda1b5/href</a></iframe><p>What this does is effectively tell your Firebase app, while in local development (i.e. the URL is localhost), to automatically reroute Firestore requests and cloud function requests to local Firestore &amp; Cloud Functions emulators instead of the deployed function endpoints.</p><p>For example, instead of a function request hitting the endpoint <a href="https://us-central1-your-app.cloudfunctions.net/myFunction,">https://us-central1-your-app.cloudfunctions.net/myFunction</a> , we want it to hit: <a href="http://localhost:5000/your-app/us-central1/myFunction">http://localhost:5000/your-app/us-central1/myFunction</a> so that you can edit your functions locally and quickly test them locally without needing to deploy them to the cloud. Similar concept to what’s happening with Firestore.</p><p>So now your firebase.js file should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8436801fbc89d55d270e102667e66c7f/href">https://medium.com/media/8436801fbc89d55d270e102667e66c7f/href</a></iframe><p>2. Now we need to run the Cloud Function emulator &amp; Firestore emulator using the following command:</p><p>firebase emulators:start</p><p>What this will do is start up the Firestore emulator &amp; and initialize each of your cloud functions locally (on port 5001 by default).</p><p>Now, what this will also do is start up another local server to serve your app’s front-end (on port 5000 by default). This can be convenient if your app is built into static files that are located in the public hosting directory specified in your firebase.json file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0eecd4d6c5ad9b09574712416ae30771/href">https://medium.com/media/0eecd4d6c5ad9b09574712416ae30771/href</a></iframe><p>The problem with this local server running the app (the one that launches on port 5000 by default) is that it requires static build files, which means that if you are doing local development with React using a command like yarn start or npm start , then this Firebase local server running on port 5000 will not reflect your latest work until you run yarn build or npm build to build your project into static files. So this leads us to an easy solution:</p><p>3. In another terminal window separate from step 2, run your React dev server:</p><p>yarn start or npm start</p><p>That’s right — just the normal React dev server you always use. Now, go to this React dev server in your browser and everything should just work (by default, it should be running on port 3000). If you go do some stuff in your app to access Firestore or a Cloud Function, you’ll notice in the network requests that your app hits the local emulator servers running in your other terminal window and not the cloud endpoints. This works because of what we did to set up Step 1.</p><p>If you’re having any issues, make sure that the port numbers you specified in Step 1 match the port numbers that the Firebase emulators are running on, which you can validate in the terminal output of Step 2.</p><p>That’s it! Soon, I’m planning to write a longer, more in-depth React + Firebase tutorial, so be on the lookout for that.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c29c188bd60e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>