<?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: Daniel</title>
    <description>The latest articles on DEV Community by Daniel (@bookmdan).</description>
    <link>https://dev.to/bookmdan</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%2F1152811%2F665d5716-3f89-49a7-a51c-2dd871aa6d6d.jpeg</url>
      <title>DEV Community: Daniel</title>
      <link>https://dev.to/bookmdan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bookmdan"/>
    <language>en</language>
    <item>
      <title>Understanding Redux: A Simplified Guide</title>
      <dc:creator>Daniel</dc:creator>
      <pubDate>Sat, 30 Mar 2024 05:03:46 +0000</pubDate>
      <link>https://dev.to/bookmdan/understanding-redux-a-simplified-guide-1mop</link>
      <guid>https://dev.to/bookmdan/understanding-redux-a-simplified-guide-1mop</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the world of React development, managing state effectively is key to building robust applications. As your projects grow in complexity, you'll encounter challenges like prop drilling. Enter Redux – a powerful state management library that simplifies how you handle data in your React apps. In this guide, we'll focus on the basics of Redux, highlighting its resemblance to useContext, the importance of &lt;strong&gt;&lt;em&gt;useDispatch&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;useSelector&lt;/em&gt;&lt;/strong&gt;, and how it eliminates the need for prop drilling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Basics: What is Redux?&lt;/strong&gt;&lt;br&gt;
Redux is like having a big toy box shared by everyone in your game. Just like how you and your friends can access toys from this shared box without passing them around, Redux provides a central place (the store) for your app's data, making it accessible to all parts of your application without needing to pass data through each component (prop drilling).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F77xwptbarftgebp40rdq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F77xwptbarftgebp40rdq.png" alt="Image description" width="800" height="304"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding Dispatch and Reducers
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Dispatch: The Action Trigger&lt;/strong&gt;&lt;br&gt;
Think of dispatch as a magic button that triggers actions in Redux. When you click this button (dispatch an action), Redux knows what to do with it. It updates the state based on the action type and payload, just like how clicking a button in your game triggers a specific action.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reducers: The State Managers&lt;/strong&gt;&lt;br&gt;
Reducers are like rule books that tell Redux how to update the state. They take the current state and an action, and based on the action type, they return a new state. It's similar to how rules in a game determine what happens next based on the actions players take.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;javascript&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const reducer = (state = initialState, action) =&amp;gt; {
  switch (action.type) {
    case 'INCREMENT':
      return { count: state.count + 1 };
    case 'DECREMENT':
      return { count: state.count - 1 };
    default:
      return state;
  }
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Power of Redux Toolkit
&lt;/h2&gt;

&lt;p&gt;Redux Toolkit simplifies Redux setup by providing utilities like createSlice. It helps you define initial state and reducer functions in a concise way, making your code more readable and maintainable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Connecting Components with Hooks&lt;/strong&gt;&lt;br&gt;
&lt;u&gt;Redux Hooks&lt;/u&gt;&lt;br&gt;
&lt;code&gt;const dispatch = useDispatch();&lt;br&gt;
const followedEvents = useSelector((state) =&amp;gt; state.event.followedEvents);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;useDispatch():&lt;/em&gt;&lt;/strong&gt; This hook gives you access to the dispatch function provided by Redux. With dispatch, you can trigger actions that update the Redux store, leading to state updates across your application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;useSelector((state) =&amp;gt;state.event.followedEvents):&lt;/em&gt;&lt;/strong&gt;This hook allows you to extract specific data from the Redux store state. Here, it selects the &lt;strong&gt;&lt;em&gt;followedEvents&lt;/em&gt;&lt;/strong&gt; array from the event slice of the Redux store. Whenever the selected state changes, the component automatically re-renders, ensuring it stays up-to-date with the latest data from the Redux store.&lt;/p&gt;

&lt;p&gt;To reiterate,&lt;br&gt;
&lt;strong&gt;useDispatch: Triggering Actions&lt;/strong&gt;&lt;br&gt;
useDispatch is like having a direct line to the action dispatcher in Redux. You use it to dispatch actions from any component without passing them down through props. It's as if you can magically trigger actions from anywhere in your app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useSelector: Accessing State&lt;/strong&gt;&lt;br&gt;
useSelector is your window into the global state. It allows you to pluck out specific pieces of state from the Redux store. With useSelector, you can access the shared toy box (global state) and get the toys you need without having to ask anyone. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In essence, Redux simplifies state management by providing a centralized store, eliminating prop drilling, and offering powerful tools like useDispatch and useSelector. Just like understanding the rules of your favorite game, mastering Redux basics ensures smoother development experiences and more efficient React applications.&lt;/p&gt;

&lt;p&gt;Remember: With Redux, you're not just building apps – you're crafting experiences. So, embrace the simplicity, harness its power, and keep coding forward!&lt;/p&gt;

</description>
      <category>redux</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding createContext in React: A Comprehensive Guide</title>
      <dc:creator>Daniel</dc:creator>
      <pubDate>Sun, 18 Feb 2024 20:58:41 +0000</pubDate>
      <link>https://dev.to/bookmdan/understanding-createcontext-in-react-a-comprehensive-guide-5469</link>
      <guid>https://dev.to/bookmdan/understanding-createcontext-in-react-a-comprehensive-guide-5469</guid>
      <description>&lt;p&gt;&lt;em&gt;createContext&lt;/em&gt; is a powerful feature in React that allows you to manage state and share data between components without having to manually pass props down through each level of the component tree. In this guide, we'll explore how to use createContext by walking through an example implementation and discussing its benefits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is createContext?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;createContext is a function provided by React that creates a context object. This context object comes with two components: Provider and Consumer (or alternatively, the useContext hook). It enables you to pass data to components deep within the component tree without having to explicitly pass props through each level.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Implementation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's consider an example where we want to manage user authentication state and cost-related data for a shopping application. We'll use createContext to create two context objects: UserContext and CostContext.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// UserContext.js
import { createContext, useState } from "react";

const UserContext = createContext({});

const UserProvider = ({ children }) =&amp;gt; {
  const [currentUser, setCurrentUser] = useState(null);
  const [loggedIn, setLoggedIn] = useState(false);

  const login = (user) =&amp;gt; {
    setCurrentUser(user);
    setLoggedIn(true);
  };

  const logout = () =&amp;gt; {
    setCurrentUser(null);
    setLoggedIn(false);
  };

  return (
    &amp;lt;UserContext.Provider value={{ currentUser, loggedIn, login, logout }}&amp;gt;
      {children}
    &amp;lt;/UserContext.Provider&amp;gt;
  );
};

export { UserContext, UserProvider };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we create a UserContext using createContext and provide a UserProvider component. This provider component encapsulates the state related to the current user and authentication status. It exposes this state and relevant functions to its children via the value prop.&lt;/p&gt;

&lt;p&gt;Similarly, let's create a CostContext for managing cost-related data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// CostContext.js
import { createContext, useState } from 'react';

const CostContext = createContext();

export const CostProvider = ({ children }) =&amp;gt; {
  const [selectedRaceCost, setSelectedRaceCost] = useState(0);
  const [shipPacketCost, setShipPacketCost] = useState(0);
  const [cartItemsCost, setCartItemsCost] = useState(0);
  const [tshirtSize, setTshirtSize] = useState('');
  const [couponCode, setCouponCode] = useState('');

  return (
    &amp;lt;CostContext.Provider
      value={{
        selectedRaceCost,
        setSelectedRaceCost,
        shipPacketCost,
        setShipPacketCost,
        cartItemsCost,
        setCartItemsCost,
        tshirtSize,
        setTshirtSize,
        couponCode,
        setCouponCode,
      }}
    &amp;gt;
      {children}
    &amp;lt;/CostContext.Provider&amp;gt;
  );
};

export const useCost = () =&amp;gt; useContext(CostContext);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we create a CostContext using createContext and provide a CostProvider component to manage cost-related state. The useCost hook allows components to access this context.&lt;br&gt;
Consuming Context&lt;/p&gt;

&lt;p&gt;Once we have defined our context and providers, we can consume them in any component using either the Consumer component or the useContext hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// PurchaseSummary.js
import { useCost } from '../../context/CostContext';

const PurchaseSummary = () =&amp;gt; {
  const { selectedRaceCost, shipPacketCost, cartItemsCost } = useCost();

  // Rest of the component implementation
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the PurchaseSummary component accesses cost-related state from the CostContext using the useCost hook. This allows the component to retrieve and use the cost data without needing to pass it down through props.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In summary, createContext is a powerful tool provided by React for managing and sharing state across components. By creating context objects and providing them through provider components, you can efficiently manage global state and avoid prop drilling in your React applications. Whether you're dealing with user authentication, application themes, or any other shared state, createContext can help streamline your development process and make your code more maintainable.&lt;/p&gt;

&lt;p&gt;*Here is the github respository to see the rest of the code for the Marathon Signup Form: &lt;a href="https://github.com/BookmDan/phase4_project"&gt;Run Your Socks Off&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Python: A Deep Dive into Instances, Class Methods, and Instance Methods</title>
      <dc:creator>Daniel</dc:creator>
      <pubDate>Wed, 06 Dec 2023 20:13:11 +0000</pubDate>
      <link>https://dev.to/bookmdan/python-a-deep-dive-into-instances-class-methods-and-instance-methods-mna</link>
      <guid>https://dev.to/bookmdan/python-a-deep-dive-into-instances-class-methods-and-instance-methods-mna</guid>
      <description>&lt;p&gt;In Python, classes and instances play a fundamental role in object-oriented programming (OOP). In this blog, we’ll explore the concepts of instances, class methods or @classmethod, and instance methods (isinstance) in Python to understand their purposes and use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Instances in Python:
&lt;/h2&gt;

&lt;p&gt;**Definition: **An instance is an individual occurrence of an object created from a class. It represents a specific realization of the class, with its own unique attributes and behaviors.&lt;/p&gt;

&lt;p&gt;**Use Case: **Instances allow you to model and work with specific entities in your program. For example, if you have a Person class, instances of that class could represent individual people, each with their own name, age, and other attributes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person1 = Person("Alice", 25)
person2 = Person("Bob", 30)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, person1 and person2 are instances of the Person class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Instance Methods in Python:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; Instance methods are methods that are bound to an instance of the class. They take self as the first parameter, representing the instance on which the method is called.&lt;br&gt;
&lt;strong&gt;Use Case:&lt;/strong&gt; Instance methods are used for operations that involve a specific instance of the class. They can access and manipulate the attributes of the instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def display_info(self):
        print(f"{self.brand} {self.model}")

car_instance = Car("Toyota", "Camry")
car_instance.display_info()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, display_info is an instance method that accesses and prints information specific to the car_instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Class Methods with @classmethod
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; In Python, a class method is a method bound to the class rather than the instance of the class. Decorated with @classmethod, these methods take the class itself as the first parameter, conventionally named cls. One practical use case for class methods is creating instances of a class based on external data, such as database rows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Class methods are useful for operations that involve the class but not a specific instance. Commonly, they are used for tasks such as creating instances based on external data, such as from an instance_from_db method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practical Example:&lt;/strong&gt; instance_from_db&lt;br&gt;
Check out the following class method named instance_from_db:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@classmethod
def instance_from_db(cls, row):
    user = cls(row[0], row[1]) if row else None
    return user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, instance_from_db is designed to create an instance of the class it belongs to (cls) based on data retrieved from a database. It assumes that the order of data in the row corresponds to the attributes of the class.&lt;/p&gt;

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

&lt;p&gt;Note: maintaining the column name and the order in which it is presented is important&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@classmethod
   def find_by_symbol(cls, symbol):
       sql = """
           SELECT *
           FROM crypto_coins
           WHERE symbol = ?
       """
       row = CURSOR.execute(sql, (symbol,)).fetchone()
       return cls.instance_from_db(row) if row else None
@classmethod
   def instance_from_db(cls, row):
       return cls(row[0], row[1], row[2]) if row else None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method is valuable in scenarios where you retrieve data from a database and want to convert it into instances of your class. It encapsulates the logic of creating an object from raw database data, providing a clean and centralized way to handle such conversions, adhering to rules of reusability and readability. &lt;/p&gt;

&lt;h3&gt;
  
  
  Application in find_by_name
&lt;/h3&gt;

&lt;p&gt;Let’s see how instance_from_db is utilized in a practical scenario. Consider the following method, part of a User class, that retrieves a user from a database based on their username:&lt;br&gt;
python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@classmethod
def find_by_name(cls, username):
    sql = """
        SELECT *
        FROM users
        WHERE username = ?
    """
    row = CURSOR.execute(sql, (username,)).fetchone()
    return cls.instance_from_db(row) if row else None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this method, the find_by_name function executes an SQL query to fetch a user's data from a database. It then uses instance_from_db to convert the retrieved database row into an instance of the User class. If no row is found, it returns None.&lt;/p&gt;

&lt;p&gt;This design choice allows for a clean separation of concerns and promotes code readability. The logic for creating instances from database rows is encapsulated within the class, making the code more modular and maintainable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking Object Types with isinstance
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; The isinstance function is a built-in Python utility used to check if an object is an instance of a particular class or a tuple of classes. It takes two parameters: the object to be checked and the class or tuple of classes to check against.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Object Type Validation&lt;br&gt;
Consider a scenario where you have different classes representing animals, such as Dog and Cat. You can use isinstance to check the type of an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Dog:
    pass

class Cat:
    pass

dog_instance = Dog()
cat_instance = Cat()

print(isinstance(dog_instance, Dog))  # True
print(isinstance(cat_instance, Cat))  # True

print(isinstance(dog_instance, Cat))  # False
print(isinstance(cat_instance, Dog))  # False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, isinstance is employed to verify whether dog_instance is an instance of the Dog class (which it is) and if cat_instance is an instance of the Cat class (which it is). It returns False when checking against classes to which the object does not belong.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling Multiple Types
&lt;/h3&gt;

&lt;p&gt;isinstance can also handle cases where an object might be an instance of any class in a tuple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(isinstance(dog_instance, (Dog, Cat)))  # True
print(isinstance(cat_instance, (Dog, Cat)))  # True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, it returns True for both instances because they are instances of at least one of the classes in the tuple.&lt;/p&gt;

&lt;p&gt;Why Use Class Methods and Instance Methods? (Recap)&lt;br&gt;
&lt;strong&gt;Class Methods:&lt;/strong&gt; Class methods are useful when you need to perform operations related to the class itself rather than a specific instance. For example, class methods are bound to the class rather than an instance.&lt;/p&gt;

&lt;p&gt;It is defined by the “@classmethod” decorator, and taking the ‘cls’ as its first parameter, and is useful for initializing and modifying class attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyClass:
    class_variable = "I am a class variable"

    @classmethod
    def class_method(cls):
        print(cls.class_variable)

MyClass.class_method()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Instance Methods:&lt;/strong&gt; Instance methods are essential for working with and manipulating specific instances of a class. They take “self” as its parameter, and are powerful because they have access to both class-level attributes and instance-specific attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Dog:
    def __init__(self, name):
        self.name = name

    def bark(self):
        print(f"{self.name} says Woof!")

my_dog = Dog("Buddy")
my_dog.bark()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Incorporating @classmethod and isinstance into your Python codebase enhances the clarity and flexibility of your object-oriented design. Class methods simplify the creation of instances based on external data, promoting modular and maintainable code. Meanwhile, isinstance empowers you to perform type checking, allowing for robust and versatile program behavior.&lt;/p&gt;

&lt;p&gt;By using these concepts, you can achieve a more organized structure in your Python projects. Now go code your next Project! &lt;/p&gt;

&lt;p&gt;Here is my Phase 3 Project for your own personal Crypto Portfolio which uses the above concepts, for reference: &lt;br&gt;
&lt;a href="https://dev.tourl"&gt;&lt;br&gt;
https://github.com/BookmDan/phase3_crypto&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you fork, and git clone: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;download &lt;em&gt;install pip&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;“pipenv install’ and “pipenv shell” to start your environment&lt;/li&gt;
&lt;li&gt;and then run “python lib/cli.py” and the command line menu should pop up &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>instances</category>
      <category>backenddevelopment</category>
      <category>classmethods</category>
    </item>
    <item>
      <title>useEffect() vs useState(), props vs callback, Dependency types for useEffect</title>
      <dc:creator>Daniel</dc:creator>
      <pubDate>Fri, 03 Nov 2023 19:49:16 +0000</pubDate>
      <link>https://dev.to/bookmdan/useeffect-vs-usestate-props-vs-callback-dependency-types-for-useeffect-441m</link>
      <guid>https://dev.to/bookmdan/useeffect-vs-usestate-props-vs-callback-dependency-types-for-useeffect-441m</guid>
      <description>&lt;h1&gt;
  
  
  useEffect() vs useState()
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;useState&lt;/strong&gt; is a React hook used to manage and update state within a functional component. It's a way to store data that can change over time and cause re-renders based on any changes. It also allows you to declare and update a piece of local state within a component. This can be useful for when you need to keep track of data that may change over time and want to trigger a re-render when the state changes.&lt;/p&gt;

&lt;p&gt;As a simple example, here is a counter using useState:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  const increment = () =&amp;gt; setCount(count + 1);

  return (
    &amp;lt;div&amp;gt;
    &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;
    &amp;lt;button onClick={increment}&amp;gt;Increment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Other use cases include managing input values, toggling UI elements, or storing and updating data specific to a component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useEffect&lt;/strong&gt; is another React hook used to perform side effects in a functional component. Side effects include data fetching, DOM manipulation, setting up subscriptions, and more. It allows you to run code after the initial render and in response to changes in state or props.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function App() {
  const [data, setData] = useState(null);

  useEffect(() =&amp;gt; {
    fetchApi()
  }, []); // Empty dependency array means this effect runs once after the initial render

  const fetchAPI = () =&amp;gt; {
  // Fetch data from an API
    fetch('https://api.example.com/data')
    .then(response =&amp;gt; response.json())
    .then(data =&amp;gt; setData(data));
   }

  return (
    &amp;lt;div&amp;gt;
    {data ? &amp;lt;p&amp;gt;Data: {data}&amp;lt;/p&amp;gt; : &amp;lt;p&amp;gt;Loading...&amp;lt;/p&amp;gt;}
    &amp;lt;/div&amp;gt;
  );

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;UseEffect is useful for when you need to interact with the outside world, handle asynchronous operations, or perform cleanup tasks when the component is unmounted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useState&lt;/strong&gt; is used to manage component-specific state, while &lt;strong&gt;useEffect&lt;/strong&gt; is used to handle side effects or actions that are not directly related to component state but need to be triggered based on changes in state or props. These two hooks are essential components to build React projects with. &lt;/p&gt;

&lt;h1&gt;
  
  
  props vs Callbacks
&lt;/h1&gt;

&lt;p&gt;Props (short for properties) are used to pass data from a parent component to a child component. Props are read-only; the child component cannot modify its props directly. They are intended for communication and data transfer between components. Props are defined when you render a component and are passed as attributes in JSX elements.The parent component then sets and updates the props of its child components.&lt;/p&gt;

&lt;p&gt;In our PlayerCard.js for example, 'player' is an example of a prop, which is passed down from PayerList.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';

const PlayerItem = ({ player}) =&amp;gt; {
  const offensiveRating = ((player.PTS / player.minutes_played) * 100).toFixed(2);
  const defensiveRating = (player.BLK * 0.6 + player.STL * 0.4).toFixed(2);

  return (
    &amp;lt;div className="card"&amp;gt;
    &amp;lt;div className="card-content"&amp;gt;
        &amp;lt;h2&amp;gt;{player.player_name}&amp;lt;/h2&amp;gt;
        &amp;lt;img src={player.image} alt={player.player_name} className="player-avatar" /&amp;gt;
        &amp;lt;p&amp;gt;Points: {player.PTS}&amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;Offensive Rating: {offensiveRating}&amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;Defensive Rating: {defensiveRating}&amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;Assists: {player.AST}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;/div&amp;gt;
  );
};

export default PlayerItem;

import React, { useState} from 'react';
import PlayerCard from './PlayerCard'; 
import NewPlayerForm from './NewPlayerForm';  

// send props from App.js 
const PlayerList = ({players, isDarkMode, toggleDarkMode, handleAddNewPlayer}) =&amp;gt; {

  return (
    &amp;lt;div className={isDarkMode ? 'dark-mode' : 'light-mode'}&amp;gt;
      &amp;lt;nav&amp;gt;
        &amp;lt;button onClick={toggleDarkMode}&amp;gt;
          {isDarkMode ? 'Light' : 'Dark'} Mode
        &amp;lt;/button&amp;gt;
      &amp;lt;/nav&amp;gt;

      &amp;lt;div&amp;gt;
        {players.map((player) =&amp;gt; (
          &amp;lt;PlayerCard key={player.id} player={player} /&amp;gt;
        ))}
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default PlayerList;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Playerlist loops through a map of props 'players' which is passed down from App.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;PlayerList
   players={players}
   toggleDarkMode={toggleDarkMode}
   isDarkMode={isDarkMode}
 /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Callbacks&lt;/strong&gt;, on the other hand, involves passing functions as props to child components. This allows the child component to trigger functions defined in the parent component, enabling communication and actions to be initiated in the parent component based on events or user interactions in the child component.&lt;/p&gt;

&lt;p&gt;From our App.js, we have toggleDarkMode which is an example of a callback function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
  const toggleDarkMode = () =&amp;gt; {
    setIsDarkMode((prevIsDarkMode) =&amp;gt; !prevIsDarkMode);
  };
return (
    &amp;lt;Router&amp;gt;
      &amp;lt;div&amp;gt;
         ...
       &amp;lt;Route
          path="/"
          element={
            &amp;lt;PlayerList
              players={players}
              toggleDarkMode={toggleDarkMode}
              isDarkMode={isDarkMode}
            /&amp;gt;
          }
        /&amp;gt;
     &amp;lt;/div&amp;gt;
   ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also access toggleDarkMode from PlayerList.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const PlayerList = ({players, isDarkMode, toggleDarkMode, handleAddNewPlayer}) =&amp;gt; {

  return (
    &amp;lt;div className={isDarkMode ? 'dark-mode' : 'light-mode'}&amp;gt;
      &amp;lt;nav&amp;gt;
        &amp;lt;button onClick={toggleDarkMode}&amp;gt;
          {isDarkMode ? 'Light' : 'Dark'} Mode
        &amp;lt;/button&amp;gt;
      &amp;lt;/nav&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Depenedency Types for useEffect()
&lt;/h1&gt;

&lt;p&gt;The useEffect hook in React accepts an optional second argument, which is an array of dependencies. The dependency array controls when the effect should run. There are three primary types of dependencies:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Empty Dependency Array ([]): When the dependency array is empty, as in useEffect(() =&amp;gt; {...}, []), the effect runs only once, similar to componentDidMount in class components. It doesn't re-run when any props or state variables change. This is usually done to fetch data from API when the component mounts. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Specific Props or State Dependencies: You can specify one or more props or state variables in the dependency array, such as [players]. The effect will run whenever the values of those dependencies change. Here when 'players' state changes, it re-renders.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
  // ... (code)
}, [players]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Callbacks as Dependencies: You can also include callback functions in the dependency array. The effect will run whenever those callbacks change, which can be useful for handling side effects based on callback changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
  // ... (code)
}, [someCallback]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above, we have described the use cases for useState() and useEffect(), what the differences between props and callbacks are, as well as to describe the three scenarios for dependency types for useEffect(). Hope you enjoyed the read and find this useful for tackling projects using useState, useEffect, props, and callbacks. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>react</category>
    </item>
    <item>
      <title>I know thee now, Callback Functions</title>
      <dc:creator>Daniel</dc:creator>
      <pubDate>Tue, 19 Sep 2023 20:35:12 +0000</pubDate>
      <link>https://dev.to/bookmdan/wht-are-callback-functions-1fgh</link>
      <guid>https://dev.to/bookmdan/wht-are-callback-functions-1fgh</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--elw8czhx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vmmagk7jsbk2s4i5lc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--elw8czhx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vmmagk7jsbk2s4i5lc4.png" alt="Image description" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NF49f32j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oer6q71kf4gq66yfzv47.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NF49f32j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oer6q71kf4gq66yfzv47.png" alt="Image description" width="800" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our objective is to convert the above eventListener to use a callback function called handleDoubleClick and create a separate handleDoubleClick function. &lt;/p&gt;

&lt;p&gt;My first attempt was this, but there are a few reasons why this does not work, which I will explain in this blog post.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4ur0x0-U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2uwgsuncc00qoeh5yjd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4ur0x0-U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2uwgsuncc00qoeh5yjd.png" alt="Image description" width="746" height="106"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;1. Why does the above instance not work? &lt;/h3&gt;

&lt;p&gt;As an arrow function, the correct format would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image.addEventListener('dblclick', () =&amp;gt; {handleDoubleClick()})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would also work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image.addEventListener('dblclick', (doubleClick) =&amp;gt; handleDoubleClick())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without the arrow function, the correct format would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image.addEventListener('dblclick', handleDoubleClick)
&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;image.addEventListener('dblclick', (handleDoubleClick) =&amp;gt; {handleDoubleClick()})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The statement above is incorrect, because I am overwriting the handleDoubleClick function, whereas if I had some other parameter inside the parenthesis, it is not being used so it is pretty much the same as having it blank, and therefore, just runs the handleDoubleClick(). &lt;/p&gt;

&lt;p&gt;To summarize:&lt;br&gt;
Here I am creating a new arrow function that takes handleDoubleClick as a parameter. However, in our instance, the function handleDoubleClick is a function without parameters, so therefore, we can pass the function as a reference directly to the addEventListener like this: &lt;/p&gt;

&lt;h3&gt;2. Let's refactor this! &lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kg4yflZz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jwd2wh9v9l8dq0k1x56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kg4yflZz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jwd2wh9v9l8dq0k1x56.png" alt="Image description" width="800" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are directly passing the handleDoubleClick function as the event handler for the 'dblclick' event on the 'image' element. In laymen's terms, when we double click the image, the handleDoubleClick function will take place. &lt;/p&gt;

&lt;p&gt;If you were to have this below, you would be calling the handleDoubleClick function and passing its return value as the event handler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image.addEventListener('dblclick', handleDoubleClick())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the addEventListener we want to pass the handleDoubleClick function as an event handler without calling it immediately, which is why we &lt;strong&gt;pass&lt;/strong&gt; the reference to the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image.addEventListener('dblclick', handleDoubleClick)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;3. Define a parameter and callback function &lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;parameter&lt;/strong&gt; is a placeholder and is/are the input values that the function uses to perform its operations. Parameters are passed to the function as ‘x’, where the function then produces a ‘y’ or output value usually by return or console.log().&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;callback function&lt;/strong&gt;  is a function that is passed as an argument to another function, only after the completion of the function to which it is passed. &lt;/p&gt;

&lt;p&gt;In the above example, when an image is double clicked, the handleDoubleClick function is called. &lt;/p&gt;

&lt;h3&gt; 4. Other examples of callback functions &lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jMP0tH9e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prjog56d3yusu35flxr7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jMP0tH9e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prjog56d3yusu35flxr7.png" alt="Image description" width="510" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this fetch example, each of the parts following the .then are each callback functions, aka the ((response) =&amp;gt; response.json(), and the ((data)=&amp;gt; {...}&lt;/p&gt;

&lt;p&gt;The console.log(a) is not correct syntax, however. &lt;/p&gt;

&lt;p&gt;This is because we also want to put the console.log within a callback function. Therefore, the correct syntax would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fetchImages() {
  fetch(apiUrl)
    .then(() =&amp;gt; console.log(a))
    .then((response) =&amp;gt; response.json())
    .then((data) =&amp;gt; {
      // Rest of your code
    })
    .catch((error) =&amp;gt; console.error('Error fetching images:', error));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you for taking the time to read this and stay tuned for more blog posts for Javascript concepts! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>concept</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Manipulating DOM events</title>
      <dc:creator>Daniel</dc:creator>
      <pubDate>Mon, 04 Sep 2023 17:44:13 +0000</pubDate>
      <link>https://dev.to/bookmdan/manipulating-the-dom-events-3dno</link>
      <guid>https://dev.to/bookmdan/manipulating-the-dom-events-3dno</guid>
      <description>&lt;p&gt;What is DOM manipulation? When we click 'send' on an iPhone message, when we click the 'like' button on Instagram, when we search things on Google. These are all examples of DOM manipulation. Basically, the basic fundamental actions that we as users interact with the webpage, almost instinctively. &lt;/p&gt;

&lt;p&gt;Changing the DOM can be broken down to 4 different actions: 1. adding elements to the DOM, 2. updating element content (what the button will display) 3. Getting elements and 4. adding event listeners for click, submit, and more. &lt;/p&gt;

&lt;p&gt;I will be using a forked replit from my FlatIron School Review Session: &lt;a href="https://replit.com/@danieloh/demetrioChallengepracdtice#script.js"&gt;https://replit.com/@danieloh/demetrioChallengepracdtice#script.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we are adding new beverages and displaying which beverage was added to the arraylist &lt;code&gt;beverages&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  1. Adding Elements to the DOM:
&lt;/h2&gt;

&lt;p&gt;You can create new HTML elements using JavaScript and add them to the DOM. The &lt;code&gt;createElement&lt;/code&gt; method is used to create a new element, and the &lt;code&gt;appendChild&lt;/code&gt; or &lt;code&gt;insertBefore&lt;/code&gt; method is used to add it to an existing element.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newDiv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;
   &lt;span class="nx"&gt;newDiv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Newly added div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parentElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;parent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="nx"&gt;parentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newDiv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y9y5M7xz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbjudq1pv4zqvbcold8c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y9y5M7xz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbjudq1pv4zqvbcold8c.png" alt="Image description" width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the above example, createElement is used to add the "Add New Drink" button as well as the bold message at the bottom of the page. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. &lt;strong&gt;Updating Element Content:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You can update the content of existing elements using the &lt;code&gt;innerText&lt;/code&gt;, &lt;code&gt;textContent&lt;/code&gt; or &lt;code&gt;innerHTML&lt;/code&gt; properties.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://tinyurl.com/innerText"&gt;http://tinyurl.com/innerText&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The differences are subtle but the gist of the differences are: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;textContents&lt;/strong&gt; is all text contained by an element and all its children that are for formatting purposes only.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;innerText&lt;/strong&gt; returns all text contained by an element and all its child elements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;innerHtml&lt;/strong&gt; returns all text, including html tags, that is contained by an element.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;elementToUpdate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;content&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="nx"&gt;elementToUpdate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Updated content&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---Ug01BVc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6eyveoadbhdg0mkq5x7q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---Ug01BVc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6eyveoadbhdg0mkq5x7q.png" alt="Image description" width="800" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we are basically inputting the content for textboxes for the counter message "There are currently 6 ..." and the messageH2 "A new beverage called taz was created..."&lt;/p&gt;

&lt;h2&gt;
  
  
  3. &lt;strong&gt;Grabbing Content from the DOM:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You can retrieve content from the DOM using various methods like &lt;code&gt;getElementById&lt;/code&gt;, &lt;code&gt;querySelector&lt;/code&gt;, and &lt;code&gt;querySelectorAll&lt;/code&gt;. These methods return elements or collections of elements that match the specified selectors.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;singleElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myElement&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;
  &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;multipleElements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.myClass&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, of note here is that if it is an id, you use '#'. If you are using a class you can use the '.'. Be sure to understand which form is required for getting the correct element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nQqiR4lE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tb8jy5puatjfda25qegc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nQqiR4lE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tb8jy5puatjfda25qegc.png" alt="Image description" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here the querySelector for the "button" from the html allows us to then append it to #button-spot div id in the form and body of the html. This is basically connecting the user functions to the html tags, like a map router for events to the different parts of the webpage i.e. html.  &lt;/p&gt;

&lt;h2&gt;
  
  
  4. &lt;strong&gt;Adding Event Listeners:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Event listeners allow you to respond to user interactions, such as clicks, inputs, or mouse movements. You can use methods like &lt;code&gt;addEventListener&lt;/code&gt; to attach event listeners to DOM elements.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttonElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myButton&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="nx"&gt;buttonElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Button clicked!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uVcy2hAB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tz1gg2ldhmb946ge652.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uVcy2hAB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tz1gg2ldhmb946ge652.png" alt="Image description" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This event listener allows us to add the "submit" action to the form, which will perform the addNewDrink method, and add the drink object with the user-inputed name of the drink. &lt;/p&gt;

&lt;p&gt;To reiterate, manipulating DOM events includes 1. adding elements to the DOM, 2. updating the DOM element contents, 3. getting content from the DOM by using querySelector or getElemetBy..., 4. adding event listeners. By using these key tools, anyone can build a basic webpage. &lt;/p&gt;

&lt;p&gt;Seeing several examples is really helpful, which can be found through searches on github. But these are just the starting blocks for building interactive DOM events, clicking buttons on a webpage, and displaying messages.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>eventdriven</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
