<?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: MikeKetterling</title>
    <description>The latest articles on DEV Community by MikeKetterling (@mikeketterling).</description>
    <link>https://dev.to/mikeketterling</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%2F742765%2Fca18637d-395d-4a17-8392-ad066f5e4394.png</url>
      <title>DEV Community: MikeKetterling</title>
      <link>https://dev.to/mikeketterling</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mikeketterling"/>
    <language>en</language>
    <item>
      <title>Writing Policies Around Multiple Roles with Ruby on Rails</title>
      <dc:creator>MikeKetterling</dc:creator>
      <pubDate>Wed, 05 Jan 2022 05:04:46 +0000</pubDate>
      <link>https://dev.to/mikeketterling/writing-policies-around-roles-with-ruby-on-rails-433c</link>
      <guid>https://dev.to/mikeketterling/writing-policies-around-roles-with-ruby-on-rails-433c</guid>
      <description>&lt;p&gt;When you are introduced to ruby on rails or any backend language your being exposed to a different view of applications.  Most the time when individuals think about applications they think about the user interface and what it looks like.  Well backend focused languages, or Ruby on Rails in this case, gives you the visibility into how the user interface receives and sends any data to offsite servers hosting large databases.  For me, this part of software engineering is the most interesting.  Lots of tables, routes, validations, security, and authorization.  As applications grow larger and more complex you may have to build additional rules around security and authorization to protect the integrity of your application.  An interesting problem that id like to focus on and talk about is role assignment for users.&lt;/p&gt;

&lt;p&gt;Sure, we can assign a user an admin role, but what about other roles?  What if your application had specific security settings for certain individuals, and you wanted to guard against visibility into certain data.  Well Rails has a handy solution to this problem allowing developers more customization around how users can interact with applications.&lt;/p&gt;

&lt;p&gt;Lets talk about my problem first and map some unique security roles around it.  I have an application that will have many users, and yes a couple will have admin rights, but some of my users will be able to post certain data that others cannot post and vice versa.  We will need to set up a few different roles to accomplish this and restrict access based on the roles.  Lets start to build out a solution in our newly created rails application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating our Models
&lt;/h2&gt;

&lt;p&gt;Lets make our users first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails g model User username password_digest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then lets make a Role model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails g model Role name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally lets make an assignment model. This will be our join table between User and Role.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails g model Assignment user:references role:references
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have set up the model migrations we’ll need to complete the migration by running: rails db:migrate.  You should see both our belongs_to :user and :role already built out in the Assignment model, but now we will need to add some validations to our Role model and make sure we set up our other relationships in our models.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up our Models
&lt;/h2&gt;

&lt;p&gt;First we can quickly verify the Assignment model has the following relationships associated 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;Class Assignment &amp;lt; ActiveRecord::Base
  belongs_to :user
  belongs_to :role
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that we are done with the Assignment model.  Lets jump into the User model and confirm its relationships are complete.  Our User will have a has many relationship with assignments and a has many roles through assignments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Class User &amp;lt; ActiveRecord::Base
  has_secure_password

  has_many :assignments
  has_many :roles, through: :assignments
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that we have completed our User model for our current purpose.  Lets jump into the Role model, just like the User model, our roles will have a has many relationship with assignments and a has many users through assignments.  Our Role model will need some validation steps though.  If we think about these roles, we want to make sure that all roles have a name and that the name is unique.  Our model should look lie so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Role &amp;lt; ActiveRecord::Base
  has_many :assignments
  has_many :users, through: :assignments

  validates :name, presence: true, uniqueness: true
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our models are all set up!  Now comes the cool part that ties everything together.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pundit Gem
&lt;/h2&gt;

&lt;p&gt;We’ll be working with a gem called Pundit.  Pundit is a much larger topic so I wont go to far into it since it can be its own blog topic but I will show you how I will use it in this scenario.  We’ll be using pundit because it allows us to define and enforce policies by using simple Ruby objects.  Lets get pundit added to our gemfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem ‘pundit’
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets install it now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bundle install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the fun part that will be new to everyone! Lets run the below command to generate a new directory called polices directly under app where, you guessed it, we’ll store our policies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails g pundit:install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets start writing some policies for our application!&lt;/p&gt;

&lt;h2&gt;
  
  
  Policies
&lt;/h2&gt;

&lt;p&gt;For this scenario I want my trainers that are using my application to adjust a workout for their clients.  I have a Workout model that has already been created, and now it is time to add some policies around the workouts.  We will need to create a WorkoutPolicy file in our policies directory.  This policy will inherit from ApplicationPolicy allowing our resource object to be referenced with “record”, this will take some additional code off out plate.  The file would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class WorkoutPolicy &amp;lt; ApplicationPolicy
  def update?
    user.role? :trainer or not record.published?
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You have the ability to define roles for each action you create for a resource.  For this example, I am only showing an update action.  This policy is stating that I am allowing users with the role as trainers to update workouts if they have already been published.&lt;/p&gt;

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

&lt;p&gt;Roles and permissions are important in many applications, and businesses tend to use very large applications that have a lot of security controls so being able to understand and write polices like this will allow you to show off a bit.  Your now able to lock down data manipulation to specific types of users, giving you more flexibility and control in your application.  Now go ahead and experiment more with it, enjoy!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/varvet/pundit"&gt;Pundit Gem on GitHub&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/@russell.sutter/how-to-create-different-permissions-across-users-using-pundit-ruby-on-rails-b5d5b888ab93"&gt;How to Create Different Permissions Across Users Using Pundit — Ruby on Rails&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.culttt.com/2016/01/20/implementing-roles-and-permissions-in-ruby-on-rails"&gt;Implementing Roles and Permissions in Ruby on Rails&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>security</category>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Rails Generators</title>
      <dc:creator>MikeKetterling</dc:creator>
      <pubDate>Wed, 15 Dec 2021 05:19:37 +0000</pubDate>
      <link>https://dev.to/mikeketterling/rails-generators-2l4o</link>
      <guid>https://dev.to/mikeketterling/rails-generators-2l4o</guid>
      <description>&lt;p&gt;When Ruby on Rails is mentioned in conversation or in a learning curriculum its quickly referenced as an efficient way to code for developers.  It was created to make a lot of the mundane and nitty gritty tasks automatic, so developers can get into the important logic within their application quicker.  This is accomplished in rails by continuing to build upon other Ruby gems and refactoring code into a lot of reusable pieces.  A major tool that rails uses is Generators.  Generators can move code mountains for developers very quickly and very accurately.&lt;/p&gt;

&lt;p&gt;There are generators that come with rail gems and logic that most rail developers will use pretty frequently, especially as developers start to learn ruby.  These common generators help you create models, add columns to tables, create controllers, and many more.  Then there are some generators that can create multiple pieces of large code or all the code items you need to run a full application.  Generators like scaffold and reference can move these large mountains of code, but another very useful use of generators is the ability to create your own custom generator to use, or edit already existing ones.&lt;/p&gt;

&lt;p&gt;After creating a rails application you can make edits within the lib/generators file to any generator that exists.  You may be thinking to yourself why would I edit a generator? Well maybe you didn’t want to generate a specific file every time or you wanted an adjustment to that file, this can be accomplished within the generator specific file.&lt;/p&gt;

&lt;p&gt;Along the same lines as editing existing generators in the generator specific file, we can create new generators by adding additional generator files and building out their functionality within those files.  I’ll walk you through the basic steps of this so if you want to experiment a bit more on your own you are free to do so!&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Generator
&lt;/h3&gt;

&lt;p&gt;Let’s say you wanted a generator to create a file within your application.  The first step would be to create a file within your lib directory, make or access a generators directory and inside that create your file for your generator.  The file path would look like this: lib/generators/generatorName_generator.rb.&lt;/p&gt;

&lt;p&gt;Your next step would be to then add logic to this file so that when the generator is called on it will act in the specific way you want it to.  The syntax convention within this file is very important so I’ll provide 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;class GeneratorNameGenerator &amp;lt; Rails::Generators::Base
    def create_file_in_app
        create_file “config/environments/new_file.rb”, “Add content here”
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is most important here is that our file name and class name line up so that later when we invoke the generator it is called on correctly.  We also need to recognize that our class is inheriting from Rails::Generators:Base.  This gives the correct functionality when this generator is called upon, running the methods inside the file sequentially in the order that is defined.  In our single method we are invoking the create_file method that will create a file with the given content at the specified location inside of your app.  Now to invoke this generator, all that needs to be ran in the terminal is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails g  generatorName
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A final step we want to make sure we take is to add a description of our generator within our file so that if we call;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails g generatorName --help
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our output will be a helpful description for future use, rather than traveling back in the file to see what the generator does.  To add a description of the generator we’ll return back into our generator file and add the desc method like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class GeneratorNameGenerator &amp;lt; Rails::Generators::Base
    desc “This generator creates a file at config/environments”
    def create_file_in_app
        create_file “config/environments/new_file.rb”, “Add content here”
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we or any other developer wee to call on this generator with the help flag it would provide a brief description of the actions the generator would take.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This is just a quick example of the versatility and power of the Rails generators.  There are many other use cases for code like this, and ultimately it makes your time coding much more efficient as long as you know you’ll be needing a custom generator for more than just one action.  Tools like these also help keep your code DRY and error free, and at the end of the day error free and efficient code is the name of the game.  Go have fun!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://arsfutura.com/magazine/diy-create-your-own-rails-generator/"&gt;DIY – Create Your Own Rails Generator&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.tutorialspoint.com/ruby-on-rails/rails-generators.htm"&gt;Ruby on Rails - Generators&lt;/a&gt;&lt;br&gt;
&lt;a href="https://guides.rubyonrails.org/generators.html"&gt;Creating and Customizing Rails Generators &amp;amp; Templates&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>productivity</category>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Metaprogramming and Class Inheritance in Ruby</title>
      <dc:creator>MikeKetterling</dc:creator>
      <pubDate>Wed, 01 Dec 2021 05:22:35 +0000</pubDate>
      <link>https://dev.to/mikeketterling/metaprogramming-and-class-inheritance-in-ruby-1ek9</link>
      <guid>https://dev.to/mikeketterling/metaprogramming-and-class-inheritance-in-ruby-1ek9</guid>
      <description>&lt;p&gt;If your new to the developer world and your coming across Metaprogramming for ruby, chances are you are feeling a bit intimidated by just the sound of it, or maybe excited about the potential power such a concept might have?  For me I know it was a little of both, but id have to say mostly I thought about potential use cases for such a concept, and I didn’t even know that much about it yet!&lt;/p&gt;

&lt;p&gt;For myself I have always though I embodies that idea that developers are lazy and do not want to code more than they have to, infact I could say I live my life around this sort of philosophy.  Efficiency is everything to me, I want to get as much doen in as little as possible so I can spend more time learning something new, getting better at something, or just relaxing.  This is where Metaprogramming really comes into play for me.&lt;/p&gt;

&lt;p&gt;I know there are concepts out there with other languages that are either exactly the same as Ruby’s Metaprogramming or very similar to it, but I think this Is just the first time for me where I have the dedicated time to really dive into the concept and the base knowledge to explain it and get through some examples in the hope of helping someone else with the concept and the utilization.&lt;/p&gt;

&lt;p&gt;In the most basic explanation, Metaprogramming is code that has been developed to write additional code, treat code as their data, and be read, generate, analyze or transform other programs or itself while its running.  With this definition alone I hope I’ve peeked some interest, I know this one sentence got me.  If we think about the usability of that concept we could potentially cut down on dozens if not hundreds of lines of code, making our applications much smoother, easy to read and follow great DRY principles.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Ruby call Methods?
&lt;/h2&gt;

&lt;p&gt;Before we can really get into the thick of Metaprogramming we have to lay down some basics of how methods work in Ruby. The below example is a simple method inside a class called Car.  When we call the method #start from our Car class we get an output of “VROOM”, but more importantly is how we get this output.  When we call the start method Ruby first will look for the parent of the station_wagon object, but because this station_wagon object is an instance of the Car class, and has the #start method available it will be called on station_wagon.&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 start
        "VROOM"
    end
end

station_wagon = Car.new
station_wagon.start  =&amp;gt; "VROOM"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can even take a look at a more complicated block of code that has some class inheritance&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Vehicle
    def start
        "VROOM"
    end

    def speed_up
        "SHIFT
    end
end

class Sportscar &amp;lt; Vehicle
    def how_many_tickets
        "Too many"
    end
end

corvette = Sportscar.new
corvette.how_many_tickets  =&amp;gt; "Too many"
corvette.start =&amp;gt; "VROOM"
corvette.thiscarisbroken =&amp;gt; NoMethodError: undefined method...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ruby is doing the same operations as before, Ruby checks the Sportscar class for the #how_many_tickets method and because it exists and finds it, the method will be called.  The process changes a bit when we call the corvette.start method.  It first asks the Sportscar class if it can call the method, but Sportscar does not have that method available. And so because of inheritance it looks up the chain to the Vehicle class.  It located the method in the parent class and calls it.  When we call the last line: “corvette.thiscarisbroken” no method is located so we are returned an error.&lt;/p&gt;

&lt;p&gt;This process can all be boiled down to something like this; Ask the objects parent class if it has a particular method and call on it, if no method is found it will continue to look up the inheritance chain for the method that’s stated, and if it can’t find the method, the method does not exist and an error will be returned.&lt;/p&gt;

&lt;p&gt;Since we’ve seen a basic example of class inheritance and in basic metaprogramming, we can start to take a deeper look into inheritance specifically with the Singleton class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Singleton Class
&lt;/h2&gt;

&lt;p&gt;The Singleton class was designed specifically for single object addition, alteration and deletion it does this by having only one instance of some class. Lets look at the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;greeting = "I like to drive fast!"

class &amp;lt;&amp;lt; greeting
    def greet
        "lets go, " + self
    end
end

greeting.greet =&amp;gt; "lets go, I like to drive fast!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Breaking this down, we created a variable called “greeting”.  Then we call the greeting on the class itself since it is its own instance and by chaining the greet method to it we can return the entire phrase.  The notation on the 3rd line is known as the class &amp;lt;&amp;lt; self notation.&lt;/p&gt;

&lt;p&gt;With the Singleton class, Ruby is adding another class into our inheritance chain.  This class is given the methods and changes instead, making it the parent of the object we are working on.  So essentially we are adding one extra layer of inheritance and locating method calls.  So instead of first asking the objects parent class if the method exists first, we will ask the object if its singleton class can respond to the method if found.&lt;/p&gt;

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

&lt;p&gt;These are just a couple examples of metaprogramming in specific with inheritance and singleton methods.  There is a whole lot more to be uncovered with metaprogramming with Ruby though.  The have books just on this topic, so if your still interested go out and find more information on it!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>ruby</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding React Hooks</title>
      <dc:creator>MikeKetterling</dc:creator>
      <pubDate>Wed, 17 Nov 2021 06:05:28 +0000</pubDate>
      <link>https://dev.to/mikeketterling/understanding-react-hooks-39gj</link>
      <guid>https://dev.to/mikeketterling/understanding-react-hooks-39gj</guid>
      <description>&lt;p&gt;One of the great tools that was made available to React developers in the 16.8 release was Hooks.  I'll go into greater detail about what hooks are, why they were developed and how to use them.  Hopefully by the end of this read you will feel more comfortable with the concepts and use-cases of Hooks, allowing you to dive deeper into coding problems, and solving these more complex problems you may run across.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Hooks Were Implemented
&lt;/h2&gt;

&lt;p&gt;Hooks allow developers to use state and other react features without writing classes.  Hooks are able to solve a lot of different problems in React.  You can take stateful logic from components so the logic can be reused and implemented independently.  This allows developers to reuse this logic without changing your component order or hierarchy, making it pretty seamless to share hooks with multiple components.&lt;/p&gt;

&lt;p&gt;Another problem that React developers run into is dealing with large, complex components with lots of stateful logic and side effects.  Without hooks related code can be changed together, gets split apart, and completely unrelated code can end up combines in single methods causing lots of bugs and inconsistencies.  With large components like these examples, the stateful logic is all over the place making it hard to refactor or break down components to more manageable sizes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Hooks do
&lt;/h2&gt;

&lt;p&gt;Hooks allow you to split a component into smaller functions within the component based on which code blocks are related.  Hooks also allow developers to use more React features without using classes.  Classes have been known to confuse newer developers and over-complicate code.  Classes are much different then other languages handle classes, unstable syntax and verbose code makes classes a struggle for most developers, especially if they are new to JavaScript.&lt;/p&gt;

&lt;p&gt;Hooks embrace functions which is the basis of what components are made of.  This alignment allows developers to learn react and component functionality without getting caught up with complex functional or reactive programming techniques.&lt;/p&gt;

&lt;p&gt;Hooks are functions that “hook into” React state and features from inside components.  React provides built in hooks, and allow you to create your own hooks for re-usable stateful behavior between components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples of popular Hooks:
&lt;/h2&gt;

&lt;h4&gt;
  
  
  State Hook
&lt;/h4&gt;

&lt;p&gt;The useState hook can be called within a component to add local state to it and protect it during application re-renders.  You can call the setState function from within the component or even passed to other components through props, allowing information and code to be transferred and maintained across components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [count, setCount] = useState(0);

return (
  &amp;lt;div&amp;gt;
    &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;ClickMe&amp;lt;/button&amp;gt;
  &amp;lt;/div&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above is a simple example of setting your const UseState variables; count, and setCount as well as setting your useState to 0.  What this means is that the variable count is currently set to 0 because we have given useState the value of 0.  the way our anonymous function in our button works is that onClick we will re-set our count to "count + 1" by calling setCount, and passing in the current count variable and adding 1 to it.  This will continually update our count variable because we are re-setting this variable with the setCount function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Effect Hook
&lt;/h4&gt;

&lt;p&gt;The useEffect hook adds the ability to perform side effects from within a component.  Side effects can be data fetching, DOM rendering, or actions like subscriptions.  When you call useEffect your basically telling React to run your effect function after sending changes to the DOM.  By default, the effects will be running after every render.  The useEffect also has the ability to clean up after itself by returning a cleanup function.&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; {
  fetch('http://localhost:3000')
  .then(res =&amp;gt; res.json())
},[])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code is a prime example of using the Effect Hook.  when we are fetching data we are telling the component to fetch the data after rendering.  React remembers our fetch function and calls on it after performing DOM updates.&lt;/p&gt;

&lt;h4&gt;
  
  
  Building your own Hooks
&lt;/h4&gt;

&lt;p&gt;Custom Hooks are more of a convention than a specific feature.  The flexibility of custom hooks allows us to cover a wide range of use-Cases from form handling, timers, and many other options.  Its really up to the developer on how they want to implement custom hooks.&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 useFriendStatus(friendID) {
  const [isOnline, setIsOnline] = useState(null);

  function handleStatusChange(status) {
    setIsOnline(status.isOnline);
  }

  useEffect(() =&amp;gt; {
    ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange);
    return () =&amp;gt; {
      ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange);
    };
  });

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

&lt;/div&gt;



&lt;p&gt;This is an example of custom hook that we have called useFriendStatus.  we can use this hook in other components and the state for each component will be completely independent like so...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function FriendStatus(props) {
  const isOnline = useFriendStatus(props.friend.id);

  if (isOnline === null) {
    return 'Loading...';
  }
  return isOnline ? 'Online' : 'Offline';
}
&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;function FriendListItem(props) {
  const isOnline = useFriendStatus(props.friend.id);

  return (
    &amp;lt;li style={{ color: isOnline ? 'green' : 'black' }}&amp;gt;
      {props.friend.name}
    &amp;lt;/li&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It even give us the option to call on a custom Hook twice within the same component, this is because each call to a Hook has a completely isolated state.&lt;/p&gt;

&lt;h4&gt;
  
  
  Other Hooks
&lt;/h4&gt;

&lt;p&gt;There are many other hooks that you will come across like useContext, useRef, and useReducer.  For the purpose of this article, I wont go into all of the many hooks that React has implemented.  The number of hooks that are available, and the flexibility that comes with their use is just another example of how great of a tool hooks are.  &lt;/p&gt;

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

&lt;p&gt;Hooks are part of the new evolvement of code, making code reusable and effective in a  variety of situations.  Technology continues on focusing on problem solving and in some cases addressing problems that have yet to turn up.  the flexibility of hooks allows us as developers to creatively solve problems and situations in new ways.  The greater understanding we have of functionality like hooks will allow us to apply them on a larger scale and discover these new ways to solve problems efficiently. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;React Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Client-side_JavaScript_frameworks/React_interactivity_events_state"&gt;MDN Documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A new life transition</title>
      <dc:creator>MikeKetterling</dc:creator>
      <pubDate>Wed, 03 Nov 2021 02:46:01 +0000</pubDate>
      <link>https://dev.to/mikeketterling/a-new-life-transition-5kd</link>
      <guid>https://dev.to/mikeketterling/a-new-life-transition-5kd</guid>
      <description>&lt;p&gt;Its true that with any transition in life there’s upheaval, fear, and frustration, that’s the point of a transition.  I think that sharing any life transition ultimately helps inspire at least someone out there, and therefore, doing something good for someone.  That’s all I hope for with this and any other posts I’ll be publishing.&lt;/p&gt;

&lt;p&gt;With this being my first post, I thought it only be fitting to write about this transition.  As I first said this has not been an easy experience, and if I can offer my story and maybe some advice for any future transitions, then great.  Everyone’s experiences will be different in a transition into the tech space – it’s important to recognize this, and I hope that my experience resonates with someone.&lt;/p&gt;

&lt;p&gt;I come from a background in Human Resources and healthcare.  Most of my work experience had to do with all the non-technical aspects of most office settings.  So as far as what I had been used to for some years, the jump to learning multiple technical languages was going to be significant - but not out of reach.  In knowing that I was going to be learning many new skills, I had a few strategies in mind.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Three Strategies
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;A healthy routine&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I knew from working the past year or so at home that for me to complete a technical program I would have to have a solid routine to help me through the rough days and keep me on track when I’m ahead in the coursework.  I also knew that a routine would support my mental exhaustion as well, and to be honest this is what I was most concerned about.&lt;/p&gt;

&lt;p&gt;My mental state had gone through a lot in that past year with COVID-19, and my job.  My mental fortitude was stretched so thin to the point I almost completely broke.  It was a rough time for all of us in the world I do believe, and I do not want to take that away from anyone - but my own experience was difficult.  If I had to say what kept me going, it was my wife and dogs.  My wife, so that we can keep driving towards our combined aspirations, and my dogs were the only ones around me when I was at my lowest, to bring me back to rational thinking when I needed it the most.  Taking all this into consideration I started to put together a routine that seemed realistic and that I could follow.&lt;/p&gt;

&lt;p&gt;I wanted to keep my body sharp as well as my mind, a regular morning workout when I wake up, followed up with strong and healthy meals through the day.  Setting time aside every night for homework and much needed time for family and friends.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Prep-work&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most bootcamps can offer you insight into what languages or tech stacks you will be using during the bootcamp.  Some bootcamps will even provide you with pre-work modules, prepping you for your time at the bootcamp of your choosing. For me I unfortunately entered the program at the almost last possible moment, so I couldn’t spend a significant amount of time with the provided pre-work modules, but I would change that if I could go back.  Another option is purchasing or finding free resources to review along with your prep coursework.  This is a strategy I use often when learning something new.      I like to see and hear plenty of different examples of how to complete similar tasks, and if you’re like me, and have adequate time, I think it’s a great strategy to immerse you in content. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Find your motivation&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The last strategy I’ll mention is finding your motivation.  Motivation might be the most important piece to anyone’s success.  It helps drive you towards a finish line especially when the road bumps come, and the path gets tough.  You’ve got to be able to lean on your motivation when things get rough.  I think your motivation needs to be important to you regarding a bootcamp.  The obstacles and trouble you will face during these learning opportunities can be intense, and the stronger the motivation you have, the more likely you will overcome adversity.&lt;br&gt;
For me, my motivation is my family, and giving them as many opportunities as possible.  I know that with a career in tech being something I’m more passionate about, not only will I be happier, but my family will be as well.&lt;/p&gt;

&lt;p&gt;I strongly believe that anyone can learn anything.  Sometimes the understanding will come quickly, and sometimes it won’t, but if you put in the work and continue your path, you can be successful to.  I reflect on this thought almost daily, and especially when the understanding is coming slower than I want.  But I’ll keep digging in and I hope you do too.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>career</category>
    </item>
  </channel>
</rss>
