<?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: Ben D.</title>
    <description>The latest articles on DEV Community by Ben D. (@foghill).</description>
    <link>https://dev.to/foghill</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%2F241305%2Fb141b0f9-049e-44d6-a7c4-df52638a707e.png</url>
      <title>DEV Community: Ben D.</title>
      <link>https://dev.to/foghill</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/foghill"/>
    <language>en</language>
    <item>
      <title>Creating and Using Migrations in Ruby/Sinatra/ActiveRecord</title>
      <dc:creator>Ben D.</dc:creator>
      <pubDate>Mon, 20 Mar 2023 07:02:02 +0000</pubDate>
      <link>https://dev.to/foghill/creating-and-using-migrations-in-rubysinatraactiverecord-4kpo</link>
      <guid>https://dev.to/foghill/creating-and-using-migrations-in-rubysinatraactiverecord-4kpo</guid>
      <description>&lt;p&gt;As a web developer, it's essential to have a strong understanding of databases and how to manipulate them to create and store data. In Ruby, Sinatra, and ActiveRecord, creating and using migrations is a critical aspect of building robust, scalable applications. Migrations allow you to manage your database schema, define relationships between your tables, and modify data stored in your database. In this blog post, we'll dive into the basics of migrations and provide examples of how to create and use them effectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Migration?
&lt;/h2&gt;

&lt;p&gt;A migration is a Ruby file that defines a set of changes to your database schema. It specifies what changes should be made to your database, such as adding a new column, renaming a table, or creating a new table. Migrations can also specify any necessary changes to the data in your database, such as populating a new column with default values or modifying existing data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Migrations?
&lt;/h2&gt;

&lt;p&gt;Migrations are an essential tool for managing your database schema because they allow you to version control your database structure. This means you can keep track of changes to your database over time and roll back to a previous version if needed. Migrations also make it easier to collaborate with other developers on the same project by providing a standardized way of updating and modifying the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Migration
&lt;/h2&gt;

&lt;p&gt;To create a migration in Ruby/Sinatra/ActiveRecord, you'll need to use the rake command-line tool. The rake tool is a build automation tool that's commonly used in Ruby applications to automate tasks. Here are the steps to create a migration:&lt;/p&gt;

&lt;p&gt;Open your terminal and navigate to your project directory.&lt;br&gt;
Enter the following command to create a new migration file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rake db:create_migration &lt;span class="nv"&gt;NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;migration_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace "migration_name" with the name of your migration. The rake tool will create a new file in the db/migrations directory with the name timestamp_migration_name.rb, where timestamp is the current timestamp.&lt;/p&gt;

&lt;p&gt;Open the new migration file in your preferred text editor. The migration file will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MigrationName&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;6.1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="c1"&gt;# Add your migration code here&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modify the change method to define your migration. For example, if you wanted to add a new column to a table, your migration might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AddNewColumnToTable&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;6.1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;add_column&lt;/span&gt; &lt;span class="ss"&gt;:table_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:column_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:string&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, :table_name is the name of the table you want to modify, :column_name is the name of the new column you want to add, and :string is the data type of the new column.&lt;/p&gt;

&lt;p&gt;Save your changes and exit the text editor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Running a Migration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you've created a migration, you'll need to run it to apply the changes to your database. Here are the steps to run a migration:&lt;/p&gt;

&lt;p&gt;Open your terminal and navigate to your project directory.&lt;br&gt;
Enter the following command to run your migration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rake db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will apply all the pending migrations to your database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rolling Back a Migration
&lt;/h2&gt;

&lt;p&gt;If you need to roll back a migration, you can use the rake command-line tool. Here are the steps to roll back a migration:&lt;/p&gt;

&lt;p&gt;Open your terminal and navigate to your project directory.&lt;br&gt;
Enter the following command to roll back the last migration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rake db:rollback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ruby</category>
      <category>sinatra</category>
      <category>activerecord</category>
      <category>webdev</category>
    </item>
    <item>
      <title>React state and useState hook</title>
      <dc:creator>Ben D.</dc:creator>
      <pubDate>Tue, 20 Dec 2022 16:21:03 +0000</pubDate>
      <link>https://dev.to/foghill/react-state-and-usestate-hook-34aj</link>
      <guid>https://dev.to/foghill/react-state-and-usestate-hook-34aj</guid>
      <description>&lt;p&gt;State is an important concept in the React JavaScript framework. In React, &lt;code&gt;state&lt;/code&gt; refers to a set of values that determine a component's behavior and render information to the user. &lt;/p&gt;

&lt;p&gt;In React, &lt;code&gt;state&lt;/code&gt; is used to manage information that is specific to a particular component and that can change over time. State values can be updated by user actions, network responses, or other events, and changes to state values can trigger a re-render of the component's view to reflect the updated state.&lt;/p&gt;

&lt;p&gt;For example, a simple to-do list app might have a &lt;code&gt;state&lt;/code&gt; value that tracks the current items in the list, and this &lt;code&gt;state&lt;/code&gt; value would be updated every time a new item is added or removed from the list. In this way, &lt;code&gt;state&lt;/code&gt; allows React components to manage and respond to changing information in a predictable and efficient manner.&lt;/p&gt;

&lt;p&gt;State is an important concept in React because it allows components to manage and control their own behavior and data, rather than relying on external sources of information. This makes it easier to build complex and interactive applications using React, because individual components can manage their own &lt;code&gt;state&lt;/code&gt; and communicate with each other using props (short for properties), which are a way of passing information between components.&lt;/p&gt;

&lt;p&gt;In short, &lt;code&gt;state&lt;/code&gt; in React is a way of managing and controlling information within a component, allowing the component to respond to changes in that information and update its view accordingly. This enables the creation of complex and interactive applications using React, and is an essential concept for anyone working with the framework.&lt;/p&gt;

&lt;p&gt;Let's look at an example:&lt;/p&gt;

&lt;p&gt;To add a state variable, you can import &lt;code&gt;useState&lt;/code&gt; at the top of the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To update a component with new data, two things need to happen:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Retain the data between renders.&lt;/li&gt;
&lt;li&gt;Trigger React to render the component with new data (re-rendering).
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&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="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Use the useState hook to define the initial state&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Function to increment the count by 1&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Function to decrement the count by 1&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decrement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Increment&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;decrement&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Decrement&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this example, we use the useState hook to define the initial state of the count variable. We then use functions to increment and decrement the count value in response to user actions. The updated count value is then used to render the current value to the screen. &lt;/p&gt;

&lt;p&gt;At its core, &lt;code&gt;state&lt;/code&gt; is a JavaScript object used by React to represent an information about the component’s current situation. It is managed in the component just like any variable declared in a function.&lt;/p&gt;

&lt;p&gt;The React &lt;code&gt;state&lt;/code&gt; holds the data for a component. The component, in turn, returns the data contained within the &lt;code&gt;state&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;state&lt;/code&gt; object is where you store property values that belongs to the component and when the &lt;code&gt;state&lt;/code&gt; object changes, the component re-renders.&lt;/p&gt;

&lt;p&gt;ReactJS docs says:&lt;/p&gt;

&lt;p&gt;Components need to “remember” things: the current input value, the current image, the shopping cart. In React, this kind of component-specific memory is called state.&lt;/p&gt;

&lt;p&gt;You can add &lt;code&gt;state&lt;/code&gt; to a component with a &lt;code&gt;useState&lt;/code&gt; Hook. Hooks are special functions that let your components use React features (&lt;code&gt;state&lt;/code&gt; is one of those features). The &lt;code&gt;useState&lt;/code&gt; Hook lets you declare a &lt;code&gt;state&lt;/code&gt; variable. It takes the initial &lt;code&gt;state&lt;/code&gt; and returns a pair of values: the current &lt;code&gt;state&lt;/code&gt;, and a &lt;code&gt;state&lt;/code&gt; setter function that lets you update it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The useState Hook
&lt;/h2&gt;

&lt;p&gt;useState is technically a function, but other functions that start with &lt;code&gt;use&lt;/code&gt; in React are called Hooks.&lt;/p&gt;

&lt;p&gt;When you call useState, you are telling React that you want this component to remember something.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useState&lt;/code&gt; hook is a powerful feature of the React library that allows developers to manage &lt;code&gt;state&lt;/code&gt; within their application. Through the &lt;code&gt;useState&lt;/code&gt; hook, developers can create and maintain &lt;code&gt;state&lt;/code&gt; variables and easily hook them up to components to allow for stateful behavior.&lt;/p&gt;

&lt;p&gt;State is a concept that is used in programming to refer to the current &lt;code&gt;state&lt;/code&gt; of a program or application. It can be used to track the current &lt;code&gt;state&lt;/code&gt; of a user’s input, the current &lt;code&gt;state&lt;/code&gt; of a component’s properties, or the current &lt;code&gt;state&lt;/code&gt; of the application as a whole. State is essential to many applications, as it allows developers to track important information and have it persist between program executions or component renders. &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/4pO-HcG2igk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useState&lt;/code&gt; hook is a way to manage state within React components. It is an imperative hook, meaning that it needs to be called in order to set or change state. When the &lt;code&gt;useState&lt;/code&gt; hook is called, it will return an array containing two items: the current state and a setter function. The setter function can be used to set the &lt;code&gt;state&lt;/code&gt; to a new value, which will trigger a re-render of the component with the new &lt;code&gt;state&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useState&lt;/code&gt; hook provides a simple, yet powerful, way to manage &lt;code&gt;state&lt;/code&gt; within React components. It offers an easy way to create and maintain &lt;code&gt;state&lt;/code&gt; variables, as well as hook them up to components for stateful behavior. With the &lt;code&gt;useState&lt;/code&gt; hook, developers can easily track and update the current &lt;code&gt;state&lt;/code&gt; of their application, allowing for dynamic and interactive user experiences.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useState&lt;/code&gt; hook is a great addition to the React library, and it has become an integral part of many applications. It is easy to use, and it provides a simple way to manage &lt;code&gt;state&lt;/code&gt; within components. With the &lt;code&gt;useState&lt;/code&gt; hook, developers can quickly and easily create and maintain &lt;code&gt;state&lt;/code&gt; variables, as well as hook them up to components to allow for stateful behavior. &lt;/p&gt;

&lt;p&gt;In conclusion, the &lt;code&gt;useState&lt;/code&gt; hook is an incredibly useful feature of the React library. It provides a simple and effective way to manage &lt;code&gt;state&lt;/code&gt; within components, allowing developers to track and update the current &lt;code&gt;state&lt;/code&gt; of their applications. With the &lt;code&gt;useState&lt;/code&gt; hook, developers can create dynamic and interactive user experiences, and it has become an essential part of many applications.&lt;/p&gt;

</description>
      <category>react</category>
      <category>state</category>
    </item>
    <item>
      <title>React Component Props</title>
      <dc:creator>Ben D.</dc:creator>
      <pubDate>Wed, 29 Jun 2022 23:41:50 +0000</pubDate>
      <link>https://dev.to/foghill/react-component-props-2ce8</link>
      <guid>https://dev.to/foghill/react-component-props-2ce8</guid>
      <description>&lt;p&gt;The most important concepts to understand React are components, props, state and hooks.&lt;/p&gt;

&lt;p&gt;I will explain 'props', short for 'properties' here. Props are accessed as parameters in the Component function. Often times, they are destructured to keep the code cleaner.&lt;/p&gt;

&lt;p&gt;I will assume that you know how to import and export the components in your project hierarchy but if not, you can check out the create-react-app &lt;a href="https://create-react-app.dev/docs/importing-a-component/"&gt;docs&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//in the App.js file you would render the prop:&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Hello&lt;/span&gt; &lt;span class="na"&gt;person&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;benjamin&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;//and in the Hello.js file, the person prop would be passed down:&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//which would ouput:&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, benjamin&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;//alternatively, you could destructure the prop for cleaner code:&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; Hello,&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can have as many props as needed. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Weather&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; It will be &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;temperature&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; degrees on &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Weather&lt;/span&gt; &lt;span class="na"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'84'&lt;/span&gt; &lt;span class="na"&gt;day&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Tuesday'&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;//Which results in:&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; It will be 84 degrees on Tuesday &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Class components
&lt;/h2&gt;

&lt;p&gt;It seems that developers are moving away from class-based components as it's unnecessarily verbose. However, if you are still using class components, the process is very similar, but you need to add a &lt;code&gt;this.props&lt;/code&gt; instead of just &lt;code&gt;props&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Greeting&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;who&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Prop Types
&lt;/h1&gt;

&lt;p&gt;A prop can have any value including strings, numbers, objects, arrays, booleans, variable, function references.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;String literal:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Component prop='this is a string'&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Template literal:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Component prop={`this is a string with a ${variable}`}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Number literal:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Component prop={14} /&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Boolean literal:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Component prop={true} /}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Object literal:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Component pro={{property : 'value'}} /&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Array literal:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Component prop={['item 1','item 2']} /&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JSX
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Component prop={Message who='Batman' /&amp;gt;} /&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variables or function references
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Component prop={functionReference} /&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Javascript Scope</title>
      <dc:creator>Ben D.</dc:creator>
      <pubDate>Fri, 22 Apr 2022 17:57:03 +0000</pubDate>
      <link>https://dev.to/foghill/javascript-scope-346a</link>
      <guid>https://dev.to/foghill/javascript-scope-346a</guid>
      <description>&lt;p&gt;A scope in Javascript means the area of code in which certain variables are able to be accessed.&lt;/p&gt;

&lt;p&gt;Variables are declared using &lt;code&gt;let&lt;/code&gt;,&lt;code&gt;const&lt;/code&gt;, and &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; creates global scoped variables, but this is problematic if this variable is declared in multiple places as overwrites can happen.&lt;/p&gt;

&lt;p&gt;The best reference on the internet in my opinion is Dimitri Pavlutin's &lt;a href="https://dmitripavlutin.com/javascript-scope/" rel="noopener noreferrer"&gt;blog&lt;/a&gt; on Scope.&lt;/p&gt;

&lt;p&gt;This JS Scope Visualized &lt;a href="https://dev.to/lydiahallie/javascript-visualized-scope-chain-13pd"&gt;blog&lt;/a&gt; is also amazing blog for visual learners. It's a 7 part series about JS, and has a section on scope which helped me tremendously&lt;/p&gt;

&lt;p&gt;There are three types of scope: global, function and block.&lt;/p&gt;

&lt;p&gt;Global scope is pretty straightforward, it means that the variables can be accessed from anywhere in the code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ft6hg6fofhirdsrvox089.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ft6hg6fofhirdsrvox089.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Local Scope
&lt;/h2&gt;

&lt;p&gt;if, for, and while statements create a scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fly() {
  // "fly" function scope
  const message = 'We're going to land in the river';
  if (true) {
    // "if" code block scope
    const pilot = 'Sully';
    console.log(message); // 'That seems dangerous'
  }
  console.log(pilot); // pilot is not defined
}
console.log(message); // message is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Standalone code blocks also create a scope:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ 
let scope = 'this is block scoped'
} 

console.log(scope) = this would return an error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Also local scope
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
function localScope() {
    const array = [1, 2, 3, 4, 5];
    let sum = 0;

}

console.log(array); //arr is not defined
console.log(sum); //array is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are nested scopes too, an inner function within an outer function has access to the outer scope.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gyazo.com/fcf1cc3ae47162cf77c1c9518f07b2dd" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.gyazo.com%2Ffcf1cc3ae47162cf77c1c9518f07b2dd.png" alt="Image from Gyazo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In summary,&lt;/p&gt;

&lt;p&gt;The Scope is the area in code from where a variable can be accessed.&lt;/p&gt;

&lt;p&gt;It can be broken down to:&lt;/p&gt;

&lt;p&gt;Global Scope&lt;br&gt;
Local Scope: Function Scope &amp;amp; Block Scope&lt;/p&gt;

&lt;p&gt;For the best explanations, please reference the links further up. If you are interested in copy/pasting your code into a web app for a visual representation of scope, then this is a cool site:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://js-scope-visualizer.firebaseapp.com/#visualizer" rel="noopener noreferrer"&gt;JS Visualizer&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>scope</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
