<?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: Alef Lewitt</title>
    <description>The latest articles on DEV Community by Alef Lewitt (@aleflewitt).</description>
    <link>https://dev.to/aleflewitt</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%2F1008524%2F3d46cdc8-d6cf-4b7c-9d3e-d72c0274817d.png</url>
      <title>DEV Community: Alef Lewitt</title>
      <link>https://dev.to/aleflewitt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aleflewitt"/>
    <language>en</language>
    <item>
      <title>Bootcamping 02: Named exports and default exports - does it really matter?</title>
      <dc:creator>Alef Lewitt</dc:creator>
      <pubDate>Sun, 24 Nov 2024 14:54:55 +0000</pubDate>
      <link>https://dev.to/aleflewitt/bootcamping-02-named-exports-and-default-exports-does-it-really-matter-15p1</link>
      <guid>https://dev.to/aleflewitt/bootcamping-02-named-exports-and-default-exports-does-it-really-matter-15p1</guid>
      <description>&lt;p&gt;&lt;strong&gt;INTRO&lt;/strong&gt;&lt;br&gt;
One of the cool parts of writing code files, is the abstraction of different components of logic, and ultimate cooporation of those components as one organic unit, one application.&lt;/p&gt;

&lt;p&gt;One of the syntaxes characteristic of ES6 is the ability to export units of logic from one file, and import them in multiple other files, making functions, data - and in React, JSX components - reusable. Write them once, and use them in many instances.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NAMED DEFAULT&lt;/strong&gt;&lt;br&gt;
Now, sometimes it's typical to have a &lt;code&gt;utilities.js&lt;/code&gt; file, or any file for that matter, where the developer stores a host of logic. A bunch of functions, or pieces of data. A classic example is an &lt;code&gt;api.js&lt;/code&gt; file which exports the various CRUD actions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function getData() {
//fetch data by making a GET request
}

export function createData() {
//create data by making a POST request
}

export function updateData() {
//update data by making a PUT request
}

export function (deleteData) {
//delete data by making a DELETE request
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in our React component for example, in order to use one of these functions, we import it using &lt;code&gt;{ }&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;import {getData, createData} from "../../src/api.js"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;EXPORT DEFAULT&lt;/strong&gt;&lt;br&gt;
In other instances, such as when creating a separate file for each React component, it's common to export the component in a "default" manner. This means that the logic being exported is the &lt;em&gt;only&lt;/em&gt; export from that file, period.&lt;/p&gt;

&lt;p&gt;For example, in a React App:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyComponent = () =&amp;gt; &amp;lt;div&amp;gt;Hello, World!&amp;lt;/div&amp;gt;;
export default MyComponent;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, when utilizing this component inside the parent component, there is no need for the &lt;code&gt;{ }&lt;/code&gt; in the import statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import MyComponent from "./MyComponent"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apparently, the lack or presence of &lt;code&gt;{ }&lt;/code&gt; around the import, let the application know if it can expect or allow additional imports from the file, or if this import will be the only one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REDUX REDUCERS&lt;/strong&gt;&lt;br&gt;
Is this the only reason though, for including the &lt;code&gt;{ }&lt;/code&gt; when importing a named import? Thanks to one of my students' questions today, we discovered another reason, at least when it comes to Redux reducer functions. We discovered that neglecting to place &lt;code&gt;{ }&lt;/code&gt; around a named import of a reducer function, serves to effectively "trick" the code into expecting a regular function, instead of a special redux reducer function.&lt;/p&gt;

&lt;p&gt;Let's take a step back and describe one unique ability of Redux reducer functions. Here's an example of a standard slice file in a redux store, where we've created a reducer function called &lt;code&gt;updateItemQuantity&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;export const CartSlice = createSlice({
  name: "cart",
  initialState,
  reducers: {
    updateItemQuantity: (
      state,
      action: PayloadAction&amp;lt;IAddItemToCartPayload&amp;gt;
    ) =&amp;gt; {
      const item = state.items.find((item) =&amp;gt; item.id === 
      action.payload.id);
      if (item) {
        item.quantity += action.payload.quantity;
      }

      state.total = state.items.reduce(
        (sum, item) =&amp;gt; sum + item.price * item.quantity,
        0
      );
    },
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how in the reducer function declaration, we've entered 2 parameters - &lt;code&gt;state&lt;/code&gt; and &lt;code&gt;action&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, when calling this function in a component, we only pass it ONE parameter, and surprisingly Redux knows what to do with it!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const handleAddToCart = (itemToAdd: IAddItemToCartPayload) =&amp;gt; {
    dispatch(updateItemQuantity(itemToAdd));
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why is this?&lt;/p&gt;

&lt;p&gt;The answer lies in the behind-the-scenes work that Redux does when we call &lt;code&gt;dispatch&lt;/code&gt;. It automatically handles passing parameter 1 as the current state, and parameter 2 as an &lt;code&gt;action&lt;/code&gt; &lt;em&gt;object&lt;/em&gt;, and then takes the SINGLE parameter passed and injects it into the &lt;code&gt;action&lt;/code&gt; object as a payload.&lt;/p&gt;

&lt;p&gt;We discovered, that this all is well and good, as long as the export and import methods are consistent, as follows:&lt;br&gt;
The reducer function is a &lt;em&gt;named&lt;/em&gt; export:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const { updateItemQuantity } = CartSlice.actions;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and is also imported accordingly, complete with &lt;code&gt;{ }&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;import { updateItemQuantity } from "../store/features/cartSlice";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, if the curly parentheses are left out, like such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import updateItemQuantity from "../store/features/cartSlice";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then what happens is as follows. Although the code editor recognizes the import, it displays an error that it expects 2 arguments for the function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F09oi7esm4cvshyh2pruq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F09oi7esm4cvshyh2pruq.png" alt="Image description" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The error recorded is: &lt;code&gt;Expected 2 arguments, but got 1.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The reason goes back to what we explained earlier - Redux knows how to handle the single parameter and inject it into the reducer function's second parameter (the action object). However, Redux only knows to do this if the exact reducer function exported is the same reducer function imported! When we left out the &lt;code&gt;{ }&lt;/code&gt; from the import statement, the code editor treated the updateItemQuantity as a &lt;strong&gt;regular javascript function&lt;/strong&gt; and hence expected 2 parameters, just as there were 2 parameters when this (javascript) function was defined. However, only when we accurately imported it as the actual REDUX REDUCER function which was in-fact exported (by being precise with the &lt;code&gt;{ }&lt;/code&gt;) did the code editor recognize that this in-fact is NOT a regular JS function, but is rather a Redux reducer function, and hence &lt;em&gt;is&lt;/em&gt; allowed to recieve only one parameter and leave the formulation of that parmater into the action object up to Redux to handle behind-the-scenes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CONCLUSION&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you are learing how to code, you may find it tedious to stick to the details, like including curly parentheses versus leaving them out, but here's just another example of how a deeper understanding of what is going on behind the code, serves to help us appreciate the need for such attention to detail.&lt;/p&gt;

&lt;p&gt;Happy learning!&lt;/p&gt;

</description>
      <category>redux</category>
      <category>es6</category>
      <category>import</category>
      <category>reducer</category>
    </item>
    <item>
      <title>Bootcamping 01: An Unexpected Behavior of Mongoose</title>
      <dc:creator>Alef Lewitt</dc:creator>
      <pubDate>Thu, 10 Oct 2024 08:48:23 +0000</pubDate>
      <link>https://dev.to/aleflewitt/bootcamping-01-an-unexpected-behavior-of-mongoose-3824</link>
      <guid>https://dev.to/aleflewitt/bootcamping-01-an-unexpected-behavior-of-mongoose-3824</guid>
      <description>&lt;p&gt;When you’re a student in a bootcamp, you’re introduced to so many new &lt;br&gt;
technologies all at once, and sometimes it can be overwhelming. One approach that I’ve found helpful is to try and identify what each technology is adding. Ask yourself - what problem is this technology coming to solve? Why do we need it in the first place?&lt;/p&gt;

&lt;p&gt;The tendency to not ask these questions, may stem from a feeling of professionalism that comes with using a new package or technology. It can seem “cool” or “tech-y” to add new jargon to your toolbox, that this can cloud the true professionalism, which is to ask “why do I actually need this package in my project?” Lugging around an unnecessary package can cause more harm than help when deploying and it’s important to attempt to understand the helpfulness of a given package you plan on using in your project.&lt;/p&gt;

&lt;p&gt;In coding bootcamps, when teaching usage of the MongoDB database, it’s common to almost immediately use it with the additional package “Mongoose”. Why? What does this third-party library add to the regular MongoDB?&lt;/p&gt;

&lt;p&gt;Besides the basic advantages of rigid schemas and models (see this fantastic article by Jesse Hall, &lt;a href="https://www.mongodb.com/developer/languages/javascript/getting-started-with-mongodb-and-mongoose/" rel="noopener noreferrer"&gt;Getting Started with MongoDB &amp;amp; Mongoose&lt;/a&gt;), there are some hidden advantages, one of which I recently discovered.&lt;/p&gt;

&lt;p&gt;A student of mine was using the &lt;code&gt;findOne&lt;/code&gt; method, passing it a query value which was “undefined”, (for example, findOne({username}) where username is undefined and in response he was receiving an actual MongoDB document. I was surprised? The MongoDB documentation clearly states that such a case should in-fact respond with “null”!&lt;/p&gt;

&lt;p&gt;The answer I discovered lies in one way that Mongoose is “smarter” than MongoDB. Mongoose, in its attempt to only return documents and avoid returning &lt;code&gt;null&lt;/code&gt;, won’t ‘agree’ as it were, to send a request of findOne with an &lt;code&gt;undefined&lt;/code&gt; query, and so, behind the scenes, when Mongoose’s findOne method is passed an &lt;code&gt;undefined&lt;/code&gt; query value, it doesn’t send that query to MongoDB - it first translates the &lt;code&gt;undefined&lt;/code&gt; into an empty object, and in-turn, generates a response of an actual document instead of &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Food for thought: Is this an advantage or a disadvantage? What are your thoughts?&lt;/p&gt;

&lt;p&gt;Happy learning! Stay curious, and please share your thoughts!&lt;/p&gt;

</description>
      <category>findone</category>
      <category>mongoose</category>
      <category>mongodb</category>
      <category>bootcamp</category>
    </item>
  </channel>
</rss>
