<?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>Forem: Shivam</title>
    <description>The latest articles on Forem by Shivam (@smileyshivam).</description>
    <link>https://forem.com/smileyshivam</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%2F502430%2F5ebd3e9c-4d12-4884-8983-72e66932698b.png</url>
      <title>Forem: Shivam</title>
      <link>https://forem.com/smileyshivam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/smileyshivam"/>
    <language>en</language>
    <item>
      <title>SSR: clerk with hasura</title>
      <dc:creator>Shivam</dc:creator>
      <pubDate>Thu, 21 Jul 2022 13:50:00 +0000</pubDate>
      <link>https://forem.com/smileyshivam/ssr-clerk-with-hasura-4kh0</link>
      <guid>https://forem.com/smileyshivam/ssr-clerk-with-hasura-4kh0</guid>
      <description>&lt;p&gt;This article will help you in achieving server side rendering(authentication) with clerk(withServerSideAuth) in next.js&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://clerk.dev/"&gt;Clerk&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
Multiple authentication strategies are available through Clerk so that legitimate users can access your application and make authenticated requests&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Into SSR with Nextjs and Clerk&lt;/strong&gt;&lt;br&gt;
To enable server-side rendering, Nextjs uses a special export named getServerSideProps. WithServerSideAuth helper from clerk is populated with the auth singleton.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;steps:&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;setup an apollo client &lt;/li&gt;
&lt;li&gt;use getServerSideProps and withServerSideAuth for ssr &lt;/li&gt;
&lt;li&gt;use graphql query&lt;/li&gt;
&lt;li&gt;test for both authenticated and non-authenticated user&lt;/li&gt;
&lt;li&gt;if there is a condition where your query is meant to response only after login then please do not return anything otherwise it will lead to issues&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;code snippet for same:&lt;/em&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  1.apolloclient setup:
&lt;/h6&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useMemo } from 'react';
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client';
import merge from 'deepmerge';
import isEqual from 'lodash/isEqual';

let apolloClient;
const uri = process.env.NEXT_PUBLIC_HASURA_URI;
function createApolloClient(headers) {
  return new ApolloClient({
    ssrMode: typeof window === 'undefined', // set to true for SSR
    link: createHttpLink({
      uri,
      credentials: 'same-origin',
      headers: {
        Apitoken: process.env.YOUR_API_TOKEN,
      },
    }),
    cache: new InMemoryCache({
      typePolicies: {
        Query: {
          fields: {
            feed: {
              keyArgs: ['strategy', 'limit', 'gt_token'],
              // Concatenate the incoming list items with
              // the existing list items.
              merge(existing = [], incoming) {
                const result = Object.assign({}, incoming);
                if (existing?.hits) {
                  result.hits = [...existing.hits, ...incoming.hits];
                }
                return result;
              },
            },
            experts: {
              keyArgs: ['limit'],
              // Concatenate the incoming list items with
              // the existing list items.
              merge(existing = [], incoming) {
                const result = Object.assign({}, incoming);
                if (existing?.hits) {
                  result.hits = [...existing.hits, ...incoming.hits];
                }
                return result;
              },
            },
          },
        },
      },
    }),
  });
}

export function initializeApollo(initialState = null, ctx) {
  const headers = ctx?.headers ?? {};
  const _apolloClient = apolloClient ?? createApolloClient(headers);

  // If your page has Next.js data fetching methods that use Apollo Client,
  // the initial state gets hydrated here
  if (initialState) {
    // Get existing cache, loaded during client side data fetching
    const existingCache = _apolloClient.extract();

    // Restore the cache using the data passed from
    // getStaticProps/getServerSideProps combined with the existing cached data
    const data = merge(initialState, existingCache, {
      // combine arrays using object equality (like in sets)
      arrayMerge: (destinationArray, sourceArray) =&amp;gt; [
        ...sourceArray,
        ...destinationArray.filter((d) =&amp;gt;
          sourceArray.every((s) =&amp;gt; !isEqual(d, s))
        ),
      ],
    });
    _apolloClient.cache.restore(data);
  }

  // For SSG and SSR always create a new Apollo Client
  if (typeof window === 'undefined') return _apolloClient;

  // Create the Apollo Client once in the client
  if (!apolloClient) apolloClient = _apolloClient;
  return _apolloClient;
}

export function useApolloHasura(initialState) {
  return useMemo(
    () =&amp;gt; initializeApollo(initialState, undefined),
    [initialState]
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h6&gt;
  
  
  2.use getServerSideProps and withServerSideAuth for ssr:
&lt;/h6&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { initializeApollo } from '../apolloClient';
import { withServerSideAuth } from '@clerk/nextjs/ssr';

export const getServerSideProps = withServerSideAuth(
  async (ctx) =&amp;gt; {
   const { getToken } = ctx.req.auth;
    const token = await getToken({ template: 'hasura' });
    const apolloClient = initializeApollo(null, ctx);

   const client = initialApollo(null, ctx);
    const sub =
      (await client.query({
        query: YOUR_QUERY,
        context: {
          headers: {
            authorization: 'YOUR_AUTHORIZATION_TOKEN',
          },
        },
      }));

    const status = sub?.data;

       return {
      props: {
        isStatus: status,
      },
    };
  },
  { loadUser: true }
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h6&gt;
  
  
  3. condition where your query is meant to response only after logged in:
&lt;/h6&gt;

&lt;p&gt;here is the snippet for query where it gives response only for authenticated users example subscription query... you need to make small changes on above code. the status will return undefined if token(hasuraToken) will return nothing for non-authenticated user, so on that case you need to return null as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const sub =
      token !== null &amp;amp;&amp;amp;
      (await client.query({
        query: PAYMENTS,
        context: {
          headers: {
            authorization: `Bearer ${token}`,
          },
        },
      }));

   const status = sub?.data;

   return {
      props: {
        isStatus: status === undefined ? null : status
      },
    };


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

&lt;/div&gt;



&lt;p&gt;For more info please refer to &lt;a href="https://clerk.dev/docs/quickstarts/get-started-with-nextjs"&gt;Clerk&lt;/a&gt; and &lt;a href="https://github.com/vercel/next.js/tree/canary/examples/with-apollo"&gt;Nextjs with SSR example&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What do you think about this? Is there any other way that I haven't mentioned? Please please please let me know by commenting down below. I can't wait to hear that. Thanks&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Happy Coding!!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>clerk</category>
      <category>hasura</category>
      <category>graphql</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>Use Hooks + Context, not React + Redux</title>
      <dc:creator>Shivam</dc:creator>
      <pubDate>Fri, 08 Jan 2021 17:52:46 +0000</pubDate>
      <link>https://forem.com/smileyshivam/use-hooks-context-not-react-redux-35bg</link>
      <guid>https://forem.com/smileyshivam/use-hooks-context-not-react-redux-35bg</guid>
      <description>&lt;p&gt;&lt;strong&gt;Redux introduces a lot of complexity to our codebase with the excessive amount of code it requires. This makes it an imperfect solution for state management in React applications.&lt;/strong&gt;&lt;br&gt;
I will introduce the &lt;strong&gt;React Context API&lt;/strong&gt; for state management and show you what makes &lt;strong&gt;React Hooks plus the Context API a better solution than Redux&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The React Context API&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Context provides a way to pass data through the component tree without having to pass props down manually at every level.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The React Context API is React’s way of managing state in multiple components that are not directly connected.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;To create a context, we’ll use the &lt;code&gt;createContext&lt;/code&gt; method from React, which accepts a parameter for its default value:&lt;/em&gt;&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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;const&lt;/span&gt; &lt;span class="nx"&gt;newContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;black&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;The &lt;code&gt;createContext&lt;/code&gt; method returns an object with a Provider and a Consumer component:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Consumer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newContext&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Provider&lt;/code&gt; component is what makes the state available to all child components, no matter how deeply nested they are within the component hierarchy. The &lt;code&gt;Provider&lt;/code&gt; component receives a value &lt;strong&gt;prop&lt;/strong&gt;. This is where we’ll pass our current value:&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;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&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;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Consumer&lt;/code&gt;, as its name implies, consumes the data from the &lt;code&gt;Provider&lt;/code&gt; without any need for &lt;em&gt;prop drilling(threading)&lt;/em&gt;:&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;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Consumer&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;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&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;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&amp;gt;}&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Consumer&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without Hooks, the Context API might not seem like much when compared to Redux, but combined with the &lt;code&gt;useReducer&lt;/code&gt; Hook, we have a solution that finally solves the state management problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;useReducer&lt;/code&gt; Hook&lt;/strong&gt;&lt;br&gt;
he &lt;code&gt;useReducer&lt;/code&gt; Hook came with React 16.8. Just like the reduce() method in JavaScript, the &lt;code&gt;useReducer&lt;/code&gt; Hook receives two values as its argument — &lt;em&gt;a reducer function&lt;/em&gt; and &lt;em&gt;an initial state&lt;/em&gt; — and then &lt;em&gt;returns a new state&lt;/em&gt;:&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useReducer&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;action description&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;newState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c1"&gt;// do something with the action&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newState&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&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 the above block, we’ve defined our state and a corresponding method, &lt;code&gt;dispatch&lt;/code&gt;, handling it. When we call the &lt;code&gt;dispatch&lt;/code&gt; method, the &lt;code&gt;useReducer()&lt;/code&gt; Hook will perform an action based on the &lt;code&gt;type&lt;/code&gt; that our method receives in its action argument:&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="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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&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="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;action type&lt;/span&gt;&lt;span class="dl"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&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;&lt;strong&gt;The &lt;code&gt;useReducer&lt;/code&gt; Hook plus the Context API&lt;/strong&gt;&lt;br&gt;
let’s see what happens when we combine &lt;code&gt;useReducer&lt;/code&gt; with &lt;code&gt;Context API&lt;/code&gt; in order to get the ideal global state management solution for our application.&lt;br&gt;
We’ll create our global state in a store.js file:&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="c1"&gt;// store.js&lt;/span&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;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useReducer&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;const&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&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;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;);&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;Provider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;store&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;StateProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useReducer&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&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="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;action description&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;newState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c1"&gt;// do something with the action&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newState&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatch&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;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Provider&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;StateProvider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our store.js file, we used the &lt;code&gt;createContext()&lt;/code&gt; method from React that is explained earlier to create a new context.&lt;br&gt;
Remember that the &lt;code&gt;createContext()&lt;/code&gt; method returns an object with a &lt;code&gt;Provider&lt;/code&gt; and &lt;code&gt;Consumer&lt;/code&gt; component. This time, we’ll be using only the &lt;code&gt;Provider&lt;/code&gt; component and then the &lt;code&gt;useContext&lt;/code&gt; Hook when we need to access our state.&lt;br&gt;
When we need to manipulate our state, we’ll call the &lt;code&gt;dispatch&lt;/code&gt; method and pass in an object with the desired &lt;code&gt;type&lt;/code&gt; as its argument.&lt;br&gt;
In our &lt;code&gt;StateProvider&lt;/code&gt;, we returned our &lt;code&gt;Provider&lt;/code&gt; component with a value &lt;code&gt;prop&lt;/code&gt; of state and &lt;code&gt;dispatch&lt;/code&gt; from the &lt;code&gt;useReducer&lt;/code&gt; Hook.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accessing our state globally&lt;/strong&gt;&lt;br&gt;
In order to access our state globally, we’ll need to wrap our root &lt;code&gt;&amp;lt;App/&amp;gt;&lt;/code&gt; component in our &lt;code&gt;StoreProvider&lt;/code&gt; before rendering it in our &lt;code&gt;ReactDOM.render()&lt;/code&gt; function:&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="c1"&gt;// root index.js file&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&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-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&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;./App&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;StateProvider&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;./store.js&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;app&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;lt;&lt;/span&gt;&lt;span class="nx"&gt;StateProvider&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/StateProvider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;ReactDOM&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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&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;root&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;Now, our &lt;code&gt;store&lt;/code&gt; context can be accessed from any component in the component tree. To do this, we’ll import the &lt;code&gt;useContext&lt;/code&gt; Hook from &lt;code&gt;react&lt;/code&gt; and the &lt;code&gt;store&lt;/code&gt; from our ./store.js file:&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="c1"&gt;// exampleComponent.js&lt;/span&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;useContext&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;store&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;./store.js&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;ExampleComponent&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;globalState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&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="nx"&gt;globalState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// this will return { color: red }&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Adding and removing data from our state&lt;/strong&gt;&lt;br&gt;
We’ve seen how we can access our global state.&lt;br&gt;
In order to add and remove data from our state, we’ll need the &lt;code&gt;dispatch&lt;/code&gt; method from our &lt;code&gt;store&lt;/code&gt; context. We only need to call the &lt;code&gt;dispatch&lt;/code&gt; method and pass in an object with type (the &lt;code&gt;action&lt;/code&gt; description as defined in our &lt;code&gt;StateProvider&lt;/code&gt; component) as its parameter:&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="c1"&gt;// exampleComponent.js&lt;/span&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;useContext&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;store&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;./store.js&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;ExampleComponent&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;globalState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;);&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;dispatch&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;globalState&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;action description&lt;/span&gt;&lt;span class="dl"&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;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Redux works for state management in React applications and has a few advantages, but its verbosity makes it really difficult to pick up, and the ton of extra code needed to get it working in our application introduces a lot of unnecessary complexity.&lt;/p&gt;

&lt;p&gt;On the other hand, with the &lt;code&gt;useContext&lt;/code&gt; API and React Hooks, there is no need to install external libraries or add a bunch of files and folders in order to get our app working. This makes it a much simpler, more straightforward way to handle global state management in React applications.&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>hooks</category>
      <category>contextapi</category>
    </item>
    <item>
      <title>JavaScript Callback Functions – What are Callbacks in JS and How to Use Them</title>
      <dc:creator>Shivam</dc:creator>
      <pubDate>Fri, 04 Dec 2020 04:54:08 +0000</pubDate>
      <link>https://forem.com/smileyshivam/javascript-callback-functions-what-are-callbacks-in-js-and-how-to-use-them-123o</link>
      <guid>https://forem.com/smileyshivam/javascript-callback-functions-what-are-callbacks-in-js-and-how-to-use-them-123o</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is a Callback Function?&lt;/strong&gt;&lt;br&gt;
In JavaScript, functions are objects. Can we pass objects to functions as parameters? Yes.&lt;br&gt;
So, we can also pass functions as parameters to other functions and call them inside the outer functions. Sounds complicated? Let me show that in an example below:&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;function&lt;/span&gt; &lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="nx"&gt;callback&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;The print( ) function takes another function as a parameter and calls it inside. This is valid in JavaScript and we call it a “callback”. So a function that is passed to another function as a parameter is a callback function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why do we need Callback Functions?&lt;/strong&gt;&lt;br&gt;
JavaScript runs code sequentially in top-down order. However, there are some cases that code runs (or must run) after something else happens and also not sequentially. This is called asynchronous programming.&lt;/p&gt;

&lt;p&gt;Callbacks make sure that a function is not going to run before a task is completed but will run right after the task has completed. It helps us develop asynchronous JavaScript code and keeps us safe from problems and errors.&lt;/p&gt;

&lt;p&gt;In JavaScript, the way to create a callback function is to pass it as a parameter to another function, and then to call it back right after something has happened or some task is completed. Let’s see how…&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to create a Callback&lt;/strong&gt;&lt;br&gt;
To understand what I’ve explained above, let me start with a simple example. We want to log a message to the console but it should be there after 3 seconds.&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;message&lt;/span&gt; &lt;span class="o"&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="s2"&gt;This message is shown after 3 seconds&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a built-in method in JavaScript called “setTimeout”, which calls a function or evaluates an expression after a given period of time (in milliseconds). So here, the “message” function is being called after 3 seconds have passed. (1 second = 1000 milliseconds)&lt;br&gt;
In other words, the message function is being called after something happened (after 3 seconds passed for this example), but not before. So the message function is an example of a callback function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is an Anonymous Function?&lt;/strong&gt;&lt;br&gt;
Alternatively, we can define a function directly inside another function, instead of calling it. It will look like this:&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="nx"&gt;setTimeout&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="s2"&gt;This message is shown after 3 seconds&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, the callback function here has no name and a function definition without a name in JavaScript is called as an “anonymous function”. This does exactly the same task as the example above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Callback as an Arrow Function&lt;/strong&gt;&lt;br&gt;
If you prefer, you can also write the same callback function as an ES6 arrow function, which is a newer type of function in JavaScript:&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="nx"&gt;setTimeout&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;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="s2"&gt;This message is shown after 3 seconds&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What about Events?&lt;/strong&gt;&lt;br&gt;
JavaScript is an event-driven programming language. We also use callback functions for event declarations. For example, let’s say we want users to click on a button:&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;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;callback-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;here&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time we will see a message on the console only when the user clicks on the button:&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;queryselector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#callback-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&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="s2"&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="s2"&gt;User has clicked on the button!&lt;/span&gt;&lt;span class="dl"&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;So here we select the button first with its id, and then we add an event listener with the addEventListener method. It takes 2 parameters. The first one is its type, “click”, and the second parameter is a callback function, which logs the message when the button is clicked.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;As you can see, callback functions are also used for event declarations in JavaScript.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;---------------Thank you for reading!------------&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>callbacks</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Tips For Freshers</title>
      <dc:creator>Shivam</dc:creator>
      <pubDate>Sun, 29 Nov 2020 10:03:11 +0000</pubDate>
      <link>https://forem.com/smileyshivam/tips-for-freshers-292g</link>
      <guid>https://forem.com/smileyshivam/tips-for-freshers-292g</guid>
      <description>&lt;h1&gt;
  
  
  Tips
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Always try to keep improving yourself&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn New tech things, compete with yourself, aalas mat khao, you should learn to push yourself everyday.&lt;/li&gt;
&lt;li&gt;Everyday wake up and set your mind to work on your skills, try even if you are not able to concentrate..so that after sometime or somedays your interest will be created automatically.&lt;/li&gt;
&lt;li&gt;yaad rakho, sunna sab ka hai magar karna wahi hai jo tumhe thik lagta ho.&lt;/li&gt;
&lt;li&gt;Waqt barbaad mat karo warna ek din waqt tumhe barbaad kardega.&lt;/li&gt;
&lt;li&gt;Apne career pe hamesha concentrate karo, &lt;strong&gt;Dost&lt;/strong&gt;, &lt;strong&gt;Ladki/Ladka&lt;/strong&gt;, &lt;strong&gt;Gaadi&lt;/strong&gt; wagera sab khud-ba-khud tumhare pas aajayegi ek din.&lt;/li&gt;
&lt;li&gt;In Simple Terms &lt;strong&gt;INVEST IN YOUR SELF&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Always Explore early: Do mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When yo start exploring things early in your life, you can figure out where your passion lies.&lt;/li&gt;
&lt;li&gt;Committing mistakes is good but repeating the same in not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remember:&lt;/strong&gt; Jitni Galti Karoge sikhne waqt utni he aage badhoge, aane walle samay mai.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;MONEY or PASSION&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always Follow your passion rather than money, because money is not a big deal, what matters is you &lt;strong&gt;vision&lt;/strong&gt; about your future.&lt;/li&gt;
&lt;li&gt;Your vision for yourself should be the main agenda.&lt;/li&gt;
&lt;li&gt;Skills, knowledge pe hamesha dhyan dena, paise apne aap aajayega.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Opportunities&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always look to grab the opportunities, It is good to follow your dream but never ignore the opportunities that come in between.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"If somebody offers you an amazing opportunity but you are not sure you can do it, say yes – then learn how to do it later!"&lt;/strong&gt; &lt;strong&gt;-Richard Branson&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Stick to professionalism&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The IT sector is that kind of an industry which requires you to be a professional to the core.&lt;/li&gt;
&lt;li&gt;If the task given by them are not able to solve then talk to them about it and tell them the issue you are facing in the task.&lt;/li&gt;
&lt;li&gt;Communicate &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Expectations&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remember one thing don't believe and Faith anyone who is saying that I'll help you out to get a job or I have a good source, you just shared me your resume. &lt;/li&gt;
&lt;li&gt;They all are useless, it's better to take your own responsibility. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;College ke us paar tum duniya ko nachate the aur college ke is paar duniya tumhe nachayegi&lt;/strong&gt; isliye zaruri hai ki jitna jaldi sudhar jaoge utna he behetar rahega aane walle dino mai.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So welcome to this new cycle and remember one thing - “The more value you give to this world the more the world will pay you.”
&lt;em&gt;So start adding more values in yourself!!&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;It’s good to have intelligence but if you don’t know how to use your emotional quotient you’ll lose the race. Offices are made up of people, who make policies, systems and rules.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Last but not the least&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Work Smartly&lt;/em&gt;, &lt;em&gt;Grab opportunities&lt;/em&gt;, &lt;em&gt;Make a Career Plan&lt;/em&gt;, &lt;em&gt;Learn&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; &lt;strong&gt;Time Ek Chiz Lati Hai Aur Woh Hai Experience, Aur Experience Ka Bohot Value Hai!!&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;--------------✌️ Thank You For Reading 😄--------------&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>success</category>
      <category>freshers</category>
      <category>hindi</category>
      <category>english</category>
    </item>
    <item>
      <title>Deploy a Full Stack MERN App using Netlify and Heroku.</title>
      <dc:creator>Shivam</dc:creator>
      <pubDate>Fri, 13 Nov 2020 19:00:39 +0000</pubDate>
      <link>https://forem.com/smileyshivam/deploy-a-full-stack-mern-app-using-netlify-and-heroku-9da</link>
      <guid>https://forem.com/smileyshivam/deploy-a-full-stack-mern-app-using-netlify-and-heroku-9da</guid>
      <description>&lt;p&gt;&lt;em&gt;A guide for newbie to deploy a Full stack MERN app using Netlify and Heroku&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is MERN?&lt;/strong&gt;&lt;br&gt;
MERN stands for MongoDB - Express - React - Node.&lt;/p&gt;

&lt;p&gt;MongoDB for the database&lt;br&gt;
Node &amp;amp; Express for the server-side&lt;br&gt;
React for the client-side&lt;br&gt;
There's also the MEAN stack, which uses Angular instead of React, and the... MEVN(?) stack... whatever, it uses Vue instead of React or Angular.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project Structure:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&amp;gt;.vscode&lt;/em&gt; : VS Code settings and extensions JSON files.&lt;br&gt;
&lt;em&gt;&amp;gt;client&lt;/em&gt; : Client-Side Code&lt;br&gt;
&lt;em&gt;&amp;gt;server&lt;/em&gt; : Server-Side Code&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configuration of Project&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;client and server, both needs: .gitignore and package.json files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;.gitignore:&lt;/strong&gt; &lt;em&gt;To ignore the files and directories we don't want stored in our repo&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;package.json:&lt;/strong&gt; &lt;em&gt;To specify our separate dependencies and devDependencies&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Process of Deployment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What we're going to do specifically is host our server code on Heroku and our client code on Netlify. So our React App hosted on Netlify will make API requests to our Express API hosted on Heroku.&lt;/p&gt;

&lt;p&gt;This will assume that you have both client and server running correctly and that you have already connected your app to a hosted database on MongoDB.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create both netlify and heroku account and login.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install both netlify and heroku CLI to your pc(check on web) or in your project directory as &lt;em&gt;npm i netlify-cli&lt;/em&gt; and &lt;em&gt;npm i heroku-cli&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Go to netlify login in client side and heroku login in server side. * In heroku after login, This will prompt you to press any key, once you do it will take you to your browser where you will just need to click 'Log In'.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once this is successful you can close that browser window and navigate to your server folder of your project.&lt;/li&gt;
&lt;li&gt;In netlify just type &lt;em&gt;netlify login&lt;/em&gt; in terminal and it will navigate to netlify account automatically and ask you to authorize it and once you click on authorize and you will be logged in and then you can close the browser window and navigate to your client folder in your project.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In MongoDB, We need to allow access to your MongoDB database now from a new IP address. For simplicity, I added all IP addresses to be allowed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to MongoDB site.&lt;/li&gt;
&lt;li&gt;Navigate to the Project you're deploying using the dropdown on the top left.&lt;/li&gt;
&lt;li&gt;Then click the Network Access tab on the left side bar.&lt;/li&gt;
&lt;li&gt;Click the green button on the right of the screen that says Add IP Address.&lt;/li&gt;
&lt;li&gt;A modal will popup. Click the button that says Allow Access from Anywhere. This will place 0.0.0.0/0 in the Whitelist Entry input. Then click Confirm.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Coming Back to project folder in server directory, If you are using &lt;em&gt;Express&lt;/em&gt; the We must change what this Express server will listen for to app.listen(process.env.PORT || 5000) and add a file called Procfile and type &lt;em&gt;web: npm run build&lt;/em&gt; and next edit index.js file as &lt;em&gt;app.get('/', (req, res) =&amp;gt; { res.send('Hello from Express!')&lt;/em&gt; because after deployment we can see this message.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a Heroku new app in Heroku.com and follow the steps whatever is displayed after creating a new app, like git init, git remote..., git add., etc. and then after successful deployment navigate to open app on top right corner there you can see the message which we have edited in index.js file in step.5 &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After step.6 Our server code is officially deployed and configured correctly. Now continue to client code with netlify.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After login to netlify from terminal in step.3 push your code to git repository(If you don't know how to push please go through YouTube), and open your netlify account click New site from Git and through GitHub proceed with deployment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After deployment navigate to &lt;em&gt;build and deploy&lt;/em&gt; option on left side of your screen and then click on edit settings and Change your Base directory to client, change your Build command to npm run build, and change your Publish directory to client/build. Then click the Save button on the bottom left.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After that, Near the top in the tabs located right under your team name and site name click on Deploys, Then click the Trigger deploy button which has a drop down with two options. Always use clear cache and deploy site to ensure a fresh build that has all changes we have made.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Lastly after we deploy our front-end React code we must ensure any requests we are sending from the client-side is changed to use our Heroku URL now instead of localhost.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In my structure the requests were being made from client/api/index.js so navigate to that file and any request that contains &lt;a href="http://localhost:5000"&gt;http://localhost:5000&lt;/a&gt; must be replaced by your Heroku URL, where we got a message on the browser after deployment.(step.5)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;12.Ensure that you push these changes up to GitHub. netlify will trigger a redeploy when they detect changes to your master branch. So for this to work you must make those changes apparent to netlify essentially.&lt;/p&gt;

&lt;p&gt;-------Successfully Deployed a MERN App------------&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank You For Your Time.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;if you get stuck anywhere feel free to comment below I'll help you.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>heroku</category>
      <category>javascript</category>
      <category>netlify</category>
      <category>mern</category>
    </item>
    <item>
      <title>10 Things to Do After College</title>
      <dc:creator>Shivam</dc:creator>
      <pubDate>Fri, 06 Nov 2020 12:58:41 +0000</pubDate>
      <link>https://forem.com/smileyshivam/15-things-to-do-after-college-instead-of-getting-a-real-job-340m</link>
      <guid>https://forem.com/smileyshivam/15-things-to-do-after-college-instead-of-getting-a-real-job-340m</guid>
      <description>&lt;p&gt;More and more college students are starting to take advantage of the few years of freedom post-graduation, choosing to pursue other paths instead of jumping right into a nine-to-five, full-time career.&lt;br&gt;
If you're a recent graduate who's hesitant to jump into the "real" work world, consider doing something else with your time, like interning, volunteering, or traveling. However, whatever you choose to do, make sure you think hard about your decision, as you want to be sure you have quality experiences to list on your resume when you do choose to begin the job search for a more permanent career.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Adventure Guide&lt;/strong&gt;&lt;br&gt;
     Are you the adventurous type? Whether you're into white water rafting, skydiving, rock climbing, abseiling, kayaking, snorkeling, bungee jumping—turn it into a part-time or even a full-time job. Many adventure companies look for young people to hire.&lt;br&gt;
     There are opportunities both within the United States and also internationally, as many companies look to hire eager, English-speaking guides due to the popularity of these types of activities among college students who are abroad.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Invest in Your Passion&lt;/strong&gt;&lt;br&gt;
     Do you love yoga or pilates? Can't spend enough time on the ski slopes? Are you a rock-climbing pro? Love to paint? If you have a passion that you can turn into a part-time job, consider investing in a training course and get certified, or even teach it on your own.&lt;br&gt;
     Whatever you love to do, look into how you can make a job out of it. Not only will you have a blast at work, but you'll gain the experience and skills you need to turn your hobby into a job that you can always fall back on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Pursue a Part-Time Job That You're Passionate About&lt;/strong&gt;&lt;br&gt;
     If you don't feel you're ready to jump into a nine-to-five career, take the year off working a part-time job in an area you're passionate about, or somewhere you've always thought about working, like at a flower shop, for example, but never had the time to try it out.&lt;br&gt;
     Not only does this give you the opportunity to pad your bank account post-college, but, it could develop into more promising opportunities down the road.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Work Seasonal Jobs&lt;/strong&gt;&lt;br&gt;
     If you want a range of colorful, varied positions to add to your resume, pursuing a few seasonal jobs for a year is a fun way to get diverse work experience while also enjoying yourself. There are many types of seasonal jobs - positions at ski resorts, beaches, resorts, and cruise ships. If you plan wisely, you can find employment for a full year in this way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Travel Recreationally&lt;/strong&gt;&lt;br&gt;
     The best time to travel is after college when you are free of obligations to a job. If you have the opportunity to travel, you should do so when you can, as it broadens your perspective and is an important life experience.&lt;br&gt;
     If you decide to travel, make the most of it and take photos, or blog about it. You can then share whatever you come up with as an example of your multimedia, writing, or web design skills.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Do a Gap Year Program&lt;/strong&gt;&lt;br&gt;
     If you're interested in putting your time towards a good career, consider a gap year program like the Peace Corps, AmeriCorps, or City Year.&lt;br&gt;
     Not only do these programs provide essential life lessons, but they also strengthen your resume and will undoubtedly give you a lot of material to discuss during an interview. Moreover, having participated in a Gap Year program links you with other alumni who may be valuable resources later on in your job search.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Work at Your College&lt;/strong&gt;&lt;br&gt;
     If you decide to continue living close to your college, consider pursuing a job there, whether it is in administration or education. There are many jobs to be had on college campuses. &lt;br&gt;
     These types of jobs can be especially convenient if you still live in your college town, and can also lead to further opportunities, like the funding of post-graduate classes or even a degree, as many universities will allow their employees to take classes for free.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Take a Continuing-Education Class&lt;/strong&gt;&lt;br&gt;
     Whether you're interested in pursuing a field other than what you majored in or want to develop career-specific skills further, consider taking continuing-education classes at a community college or an adult-learning center in your area.&lt;br&gt;
     For example, if you were an English major but want to pursue a different field, like computer programming or graphic design, taking classes gives you a foundation to begin looking for entry-level jobs in that field.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. Start your own business&lt;/strong&gt;&lt;br&gt;
     If you have an idea for a great product or a strong passion, starting your own business can be the best way to create your dream job. For example, if you're passionate and knowledgeable about physical fitness and earned a degree in business, you are well situated to start your own personal training company.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. Start a career in Youtuber, Vlogger, Blogger, but remember one thing where ever you go what ever you do do it passion, Work Hard&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;---------Hope, Hum Sabki Wishes Pori Hojaye-------&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>live</category>
      <category>life</category>
      <category>love</category>
      <category>lyf</category>
    </item>
    <item>
      <title>Sass</title>
      <dc:creator>Shivam</dc:creator>
      <pubDate>Fri, 30 Oct 2020 18:16:46 +0000</pubDate>
      <link>https://forem.com/smileyshivam/saas-5ead</link>
      <guid>https://forem.com/smileyshivam/saas-5ead</guid>
      <description>&lt;p&gt;&lt;strong&gt;SASS (Syntactically Awesome Stylesheet)&lt;/strong&gt; is a CSS pre-processor, which helps to reduce repetition with CSS and saves time. It is more stable and powerful CSS extension language that describes the style of document structurally.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why..??&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is a pre-processing language which provides indented syntax (its own syntax) for CSS.&lt;/li&gt;
&lt;li&gt;It provides some features, which are used for creating stylesheets that allows writing code more efficiently and is easy to maintain.&lt;/li&gt;
&lt;li&gt;It is a super set of CSS, which means it contains all the features of CSS and is an open source pre-processor, coded in Ruby.&lt;/li&gt;
&lt;li&gt;It provides the document style in a good, structured format than flat CSS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Architecture:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;[Link] &lt;a href="https://hackernoon.com/saas-software-as-a-service-platform-architecture-757a432270f5"&gt;https://hackernoon.com/saas-software-as-a-service-platform-architecture-757a432270f5&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The SaaS provider hosts the application and data centrally — deploying patches .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is more stable, powerful, and compatible with versions of CSS.&lt;/li&gt;
&lt;li&gt;It is an open source pre-processor, which is interpreted into CSS.&lt;/li&gt;
&lt;li&gt;It uses its own syntax and compiles to readable CSS.&lt;/li&gt;
&lt;li&gt;It is a super set of CSS and is based on JavaScript.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;SASS supports two syntaxes namely &lt;em&gt;SCSS&lt;/em&gt; and &lt;em&gt;Indented syntax&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The SCSS (Sassy CSS) is an extension of CSS syntax.&lt;/li&gt;
&lt;li&gt;Indented-Using this form of syntax, CSS can be written concisely. SASS files use the extension .sass&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Deprecated Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;=&lt;/strong&gt; - It was used instead of : when setting variables and properties to values of Sass Script.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;||=&lt;/strong&gt; - It was used instead of : whenever you are assigning default value of a variable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;!&lt;/strong&gt; - Instead of $, ! was used as variable prefix. Functionality will not be changed when it is used instead of $.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;SASS is more powerful and stable that provides power to the basic language by using extension of CSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You can use SASS in three different ways-&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;As a command line tool&lt;/li&gt;
&lt;li&gt;As a Ruby module&lt;/li&gt;
&lt;li&gt;As a plugin for Rack enable framework&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The following lists are some of the CSS extensions used in SASS −&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Nested Rules:&lt;/em&gt; It is a way of combining multiple CSS rules within one another.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Referencing Parent Selectors:&lt;/em&gt;  It is the process of selecting parent selector by using the &amp;amp; character.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Nested Properties:&lt;/em&gt; It allows nesting of properties into other properties which leads to grouping of another related code.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Placeholder Selectors:&lt;/em&gt;  Sass supports placeholder selector using class or id selector by making use of @extend directive.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;SaaS is defined as the software distribution model deployed on the internet in which a cloud service provider provides applications. It is also known as "on-demand software" or "pay-as-you-go application". &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sass Variable:&lt;/strong&gt;&lt;br&gt;
The basic syntax for defining a variable is simple: Just use a $ before the variable name and treat its definition like a CSS rule:&lt;br&gt;
      &lt;em&gt;$:;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Once it's been declared, you can use the variable just as though you were typing the variable:&lt;br&gt;
      $large-font: 50px;&lt;br&gt;
       p {&lt;br&gt;
          font-size: $large-font;&lt;br&gt;
         }&lt;br&gt;
&lt;strong&gt;Sass - Data Types Eg:&lt;/strong&gt;&lt;br&gt;
SaaS Script includes seven different data types: numbers, strings, colors, Booleans, null, lists &amp;amp; maps.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Number:&lt;/em&gt;&lt;br&gt;
test: 10 + 3;    //"plain" numbers added together&lt;br&gt;
test: 10px + 3;  //"plain" number added to a pixel value&lt;br&gt;
test: 10em + 3em; //two "em" values added together&lt;/p&gt;

&lt;p&gt;&lt;em&gt;String:&lt;/em&gt;&lt;br&gt;
$default-font: 'Lucida';&lt;br&gt;
p {&lt;br&gt;
    font-family: $default-font, "Ariel", sans-serif;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Color:&lt;/em&gt;&lt;br&gt;
SaaS Script supports the same color expressions that are supported by CSS: hex values such as #0000, named values such as red, rgb expressions like rgb(100, 39, 153), rgba expressions such as rgba(100, 39, 153, 75), hsl expressions such as hsl(0, 30%, 100%), and hsla expressions such as hsla(0, 30%, 100%, 0.3)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;List:&lt;/em&gt;&lt;br&gt;
A SaaS Script list is a series of values separated by either spaces or commas. &lt;br&gt;
$body-font: Helvetica, Arial, sans-serif;&lt;br&gt;
$body-margin: 0 0 10px 15px;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Map:&lt;/em&gt;&lt;br&gt;
In SaaS Script, maps are key-value pairs. Their syntax is slightly different from a list: They must be comma-separated, and the entire map must be wrapped in parentheses:&lt;br&gt;
$red-map: (light: #e57373, medium: #f44336, dark: #b71c1c);&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Booleans &amp;amp; Nulls:&lt;/em&gt;&lt;br&gt;
SaaS Script supports two Boolean values, true and false, as well as the null value, null. These have no direct meaning in CSS, but they're useful in combination with the control directives.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sass - Control Directives&lt;/strong&gt;&lt;br&gt;
SaaS is a declarative scripting language, an extension to CSS, not a procedural programming language like JavaScript. Despite that, it does have some limited procedural capabilities via its control directives. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Conditional Execution - @if&lt;/em&gt;&lt;br&gt;
As you'd expect, the Sass @if directive and its companions &lt;a class="comment-mentioned-user" href="https://dev.to/else"&gt;@else&lt;/a&gt;
 if and &lt;a class="comment-mentioned-user" href="https://dev.to/else"&gt;@else&lt;/a&gt;
, allow you to include Sass code in the CSS output only if certain conditions are met. The basic syntax is simple:&lt;/p&gt;

&lt;p&gt;@if  { &lt;br&gt;
     &lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;em&gt;sccs: if Conditions&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;$test: 3;&lt;br&gt;
p {&lt;br&gt;
   @if $test &amp;lt; 5 { &lt;br&gt;
       color: blue;&lt;br&gt;&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;em&gt;sccs: Nested if Conditions&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;$test: 3;&lt;br&gt;
p {&lt;br&gt;
   @if $test &amp;lt; 5 { &lt;br&gt;
       color: blue;&lt;br&gt;
       @if $test == 3 {&lt;br&gt;
          text-color: white;&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Conditional Looping - @while&lt;/em&gt;&lt;br&gt;
The syntax of the @while directive is very similar to that of @if; just replace the keyword:&lt;/p&gt;

&lt;p&gt;@while  { &lt;br&gt;
    &lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Unconditional Looping - @for&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;@for  from  through  {&lt;br&gt;
    &lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;em&gt;@each&lt;/em&gt;&lt;br&gt;
Finally, the @each directive will execute a set of items in either a list or a map. For such a powerful structure, the syntax is quite straightforward:&lt;/p&gt;

&lt;p&gt;@each  in  {&lt;br&gt;
   &lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4 reasons leading you to React.js for SaaS applications&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reusability of code

&lt;ul&gt;
&lt;li&gt;React.js creates components on single-page applications that 
eventually function independently. &lt;/li&gt;
&lt;li&gt;The components can be further reused for other projects by 
allowing the SaaS developers to create the base that can be 
used to build instead of building from the beginning.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Fast Rendering

&lt;ul&gt;
&lt;li&gt;The virtual DOM model enables faster rendering. The virtual 
DOM is an updated version of the original DOM which leads to 
superior performance, great SEO and easy maintainability.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;SEO-friendly

&lt;ul&gt;
&lt;li&gt;SaaS applications also need effective Search Engine 
optimization apart from the other set of features. Google 
rankings and user experiences should be on the top to let 
people stay ahead of the competition.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Effortless transfer from web page to mobile app

&lt;ul&gt;
&lt;li&gt;This also can be done smoothly as React.js can enable the 
easy transfer of a single-page application to a mobile app.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The full potential of React.js is being realized with companies and their SaaS applications continuously preferring React.js to either build it from scratch or modify the existing version. React.js goes down as a lightweight and simple library that is tailored to meet the modern needs of the developers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The SaaS provides various applications such as:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;CRM applications&lt;/li&gt;
&lt;li&gt;Solution to Human Resource (HR)&lt;/li&gt;
&lt;li&gt;Pre-existing Billing &amp;amp; Invoicing systems&lt;/li&gt;
&lt;li&gt;Other daily usable application suites&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Open SaaS:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open SaaS uses those SaaS applications, which are developed using open source programming language. These SaaS applications can run on any open source operating system and database. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open SaaS has several benefits listed below:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No License Required&lt;/li&gt;
&lt;li&gt;Low Deployment Cost&lt;/li&gt;
&lt;li&gt;Less Vendor Lock-in&lt;/li&gt;
&lt;li&gt;More portable applications&lt;/li&gt;
&lt;li&gt;More Robust Solution&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;--------Thanks for the read.---------&lt;/em&gt;&lt;/p&gt;

</description>
      <category>saas</category>
      <category>frontend</category>
      <category>styling</category>
      <category>css</category>
    </item>
    <item>
      <title>React-Hooks basics</title>
      <dc:creator>Shivam</dc:creator>
      <pubDate>Fri, 30 Oct 2020 14:54:04 +0000</pubDate>
      <link>https://forem.com/smileyshivam/react-hooks-basics-3n71</link>
      <guid>https://forem.com/smileyshivam/react-hooks-basics-3n71</guid>
      <description>&lt;p&gt;Hooks are the new feature introduced in the React 16.8 version. It allows you to use state and other React features without writing a class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to use a Hooks:&lt;/strong&gt;&lt;br&gt;
 If you write a function component, and then you want to add some state to it, previously you do this by converting it to a class. But, now you can do it by using a Hook inside the existing function component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rules to use Hooks:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Only call Hooks from React functions,&lt;/li&gt;
&lt;li&gt;Only Call Hooks at the Top Level.&lt;/li&gt;
&lt;li&gt;Hooks can call other Hooks&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Don't call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function. By following this rule, you ensure that Hooks are called in the same order each time a component renders. That's what allows React to correctly preserve the state of Hooks between multiple useState and useEffect calls. &lt;/p&gt;

&lt;p&gt;Don’t call Hooks from regular JavaScript functions. Instead, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Call Hooks from React function components.&lt;/li&gt;
&lt;li&gt;Call Hooks from custom Hooks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;Hooks States with: *&lt;/em&gt;&lt;br&gt;
Hook state is the new way of declaring a state in React app. Hook uses useState() functional component for setting and retrieving state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hooks Effect:&lt;/strong&gt; &lt;br&gt;
The Effect Hook allows us to perform side effects in the function components. It does not use components lifecycle methods which are available in class components. In other words, Effects Hooks are equivalent to componentDidMount(), componentDidUpdate(), and componentWillUnmount() lifecycle methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Built-in Hooks:&lt;/strong&gt;&lt;br&gt;
Here, we describe the APIs for the built-in Hooks in React. The built-in Hooks can be divided into two parts, which are given below:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Basic Hooks&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useState&lt;/li&gt;
&lt;li&gt;useEffect&lt;/li&gt;
&lt;li&gt;useContext&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Additional Hooks&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useReducer&lt;/li&gt;
&lt;li&gt;useCallback&lt;/li&gt;
&lt;li&gt;useMemo&lt;/li&gt;
&lt;li&gt;useRef&lt;/li&gt;
&lt;li&gt;useImperativeHandle&lt;/li&gt;
&lt;li&gt;useLayoutEffect&lt;/li&gt;
&lt;li&gt;useDebugValue &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;eg:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;useState eg:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;import React, {&lt;br&gt;
  useState&lt;br&gt;
} from 'react';&lt;/p&gt;

&lt;p&gt;function Demo1() {&lt;br&gt;
  const [count, setCount] = useState(0);&lt;br&gt;
  return (&lt;br&gt;
    &lt;/p&gt;
&lt;br&gt;
      Count: {count}&lt;br&gt;
       setCount(0)}&amp;gt;Reset&lt;br&gt;
       setCount(count + 1)}&amp;gt;+&lt;br&gt;
       setCount(count - 1)}&amp;gt;-&lt;br&gt;
    &lt;br&gt;
  );&lt;br&gt;
}&lt;br&gt;
export default Demo1;

&lt;p&gt;&lt;em&gt;useEffect eg:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;function Demo2() {&lt;br&gt;
  const [count, setCount] = useState(0);&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    document.title = &lt;code&gt;You clicked ${count} times&lt;/code&gt;;&lt;br&gt;
  });&lt;/p&gt;

&lt;p&gt;return (&lt;br&gt;
    &lt;/p&gt;
&lt;br&gt;
      &lt;p&gt;You clicked {count} times&lt;/p&gt;
&lt;br&gt;
       setCount(count + 1)}&amp;gt;Click me&lt;br&gt;
    &lt;br&gt;
  );&lt;br&gt;
}

&lt;p&gt;&lt;em&gt;useContext eg:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;const TestContext = React.createContext();&lt;/p&gt;

&lt;p&gt;function Display() {&lt;br&gt;
  const value = useContext(TestContext);&lt;br&gt;
  return &lt;/p&gt;{value}, I am learning react hooks.;&lt;br&gt;
}

&lt;p&gt;function App() {&lt;br&gt;
  return (&lt;br&gt;
    &lt;br&gt;
      &lt;br&gt;
    &lt;br&gt;
  );&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;em&gt;useRef eg:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;function App() {&lt;br&gt;
  let [name, setName] = useState("Nate");&lt;/p&gt;

&lt;p&gt;let nameRef = useRef();&lt;/p&gt;

&lt;p&gt;const submitButton = () =&amp;gt; {&lt;br&gt;
    setName(nameRef.current.value);&lt;br&gt;
  };&lt;/p&gt;

&lt;p&gt;return (&lt;br&gt;
    &lt;/p&gt;
&lt;br&gt;
      &lt;p&gt;{name}&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;lt;div&amp;gt;
    &amp;lt;input ref={nameRef} type="text" /&amp;gt;
    &amp;lt;button type="button" onClick={submitButton}&amp;gt;
      Submit
    &amp;lt;/button&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;More advanced hooks:&lt;/strong&gt;&lt;br&gt;
       The 3 hooks mentioned above are considered to be the basic hooks. It’s possible to write entire applications using only useState, useEffect, and useContext, you could get away with just the first two. The hooks that follow offer optimizations and increasingly niche utility that you may never encounter in your applications.&lt;br&gt;
&lt;em&gt;useCallback:&lt;/em&gt;&lt;br&gt;
     React has a number of optimizations that rely on props remaining the same across renders. One of the simplest ways to break this is by defining callback functions inline. That’s not to say that defining functions inline will cause performance problems — in many cases, it has no impact. However, as you begin to optimize and identify what’s causing frequent re-renders, you may find inline function definitions to be the cause of many of your unnecessary prop change.&lt;br&gt;
      import doSomething from "./doSomething";&lt;br&gt;
      const FrequentlyRerenders = ({ id }) =&amp;gt; {&lt;br&gt;
             return (&lt;br&gt;
              
                onEvent={useCallback(() =&amp;gt; doSomething(id), [id])}&lt;br&gt;
              /&amp;gt;&lt;br&gt;
             );&lt;br&gt;
      };&lt;br&gt;
&lt;em&gt;useMemo:&lt;/em&gt;&lt;br&gt;
     It's closely related to useCallback, but for optimizing data processing. It has the same API for defining what values it depends on as useEffect and useCallback.&lt;br&gt;
       const ExpensiveComputation = ({&lt;br&gt;
                 data,sortComparator, filterPredicate}) =&amp;gt; {&lt;br&gt;
       const transformedData = useMemo(() =&amp;gt; {&lt;br&gt;
              return data&lt;br&gt;
              .filter(filterPredicate)&lt;br&gt;
              .sort(sortComparator);&lt;br&gt;
       },[data, sortComparator, filterPredicate]);&lt;br&gt;
       return &lt;/p&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;&lt;/table&gt;&lt;/div&gt;;&lt;br&gt;
       };&lt;br&gt;
&lt;em&gt;useRef:&lt;/em&gt;&lt;br&gt;
   useRef provides a mechanism for these cases. It creates an object that exists for as long as the component is mounted, exposing the value assigned as a .current property.&lt;br&gt;
      // DOM node ref example&lt;br&gt;
      function TextInputWithFocusButton() {&lt;br&gt;
               const inputEl = useRef(null);&lt;br&gt;
               const onButtonClick = () =&amp;gt; {&lt;br&gt;
            // &lt;code&gt;current&lt;/code&gt; points to the mounted text input element&lt;br&gt;
                  inputEl.current.focus();&lt;br&gt;
               };&lt;br&gt;
               return (&lt;br&gt;
                &amp;lt;&amp;gt;&lt;br&gt;
                  &lt;br&gt;
                  &lt;br&gt;
                      Focus the input&lt;br&gt;
                  &lt;br&gt;
                &amp;lt;/&amp;gt;&lt;br&gt;
              );&lt;br&gt;
      }// An arbitrary instance property

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function Timer() {
           const intervalRef = useRef();
           useEffect(() =&amp;gt; {
              const id = setInterval(() =&amp;gt; {
                // ...
              });
           intervalRef.current = id;
           return () =&amp;gt; {
              clearInterval(intervalRef.current);
           };
         });
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;useReducer:&lt;/em&gt;&lt;br&gt;
         This hook has interesting implications for the ecosystem. The reducer/action pattern is one of the most powerful benefits of Redux. It encourages modeling UI as a state machine, with clearly defined states and transitions. One of the challenges to using Redux, however, is gluing it all together. Action creators, which components to connect(), mapStateToProps, using selectors, coordinating asynchronous behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rarely used hooks:&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_useLayoutEffect:_If I use any of these 3, I anticipate it will be useLayoutEffect. This is the hook recommended when you need to read computed styles after the DOM has been mutated, but before the browser has painted the new layout. This gives you an opportunity to apply animations with the least chance of visual artifacts or browser rendering performance problems. This is the method currently used by react-flip-move

_useMutationEffect:_This is the hook I’m having the hardest time wrapping my head around. It’s run immediately before React mutates the DOM with the results from render, but useLayoutEffect is the better choice when you have to read computed styles. The docs specify that it runs before sibling components are updated and that it should be used to perform custom DOM mutations. This is the only hook that I can't picture a use case for, but it might be useful for cases like when you want a different tool (like D3, or perhaps a canvas or WebGL renderer)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;React Hooks Tutorial for Beginners: setting up the project&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;npx create-react-app exploring-hooks&lt;/em&gt;&lt;br&gt;
(You should have one of the latest version of Node.js for running npx).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In React component, there are two types of side effects:&lt;/strong&gt;&lt;br&gt;
1.Effects Without Cleanup&lt;br&gt;
2.Effects With Cleanup&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantage of React.js :&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Easy to Learn and Use&lt;/li&gt;
&lt;li&gt;Creating Dynamic Web Applications Becomes Easier&lt;/li&gt;
&lt;li&gt;Reusable Components&lt;/li&gt;
&lt;li&gt;Performance Enhancement&lt;/li&gt;
&lt;li&gt;The Support of Handy Tools&lt;/li&gt;
&lt;li&gt;Known to be SEO Friendly&lt;/li&gt;
&lt;li&gt;The Benefit of Having JavaScript Library&lt;/li&gt;
&lt;li&gt;Scope for Testing the Codes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Disadvantage of React.js&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The high pace of development&lt;/li&gt;
&lt;li&gt;Poor Documentation&lt;/li&gt;
&lt;li&gt;View Part&lt;/li&gt;
&lt;li&gt;JSX as a barrier&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;In conclusion&lt;/strong&gt;&lt;br&gt;
   Hooks have me excited about the future of React all over again. I’ve been using this tool since 2014, and it has continually introduced new changes that convince me that it’s the future of web development. These hooks are no different, and yet again substantially raise the bar for developer experience, enabling me to write durable code, and improve my productivity by extracting reused functionality.&lt;br&gt;
   I expect that React applications will set a new bar for end-user experience and code stability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Questions:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Q. Which versions of React include Hooks?&lt;/strong&gt;&lt;br&gt;
   Starting with 16.8.0, React includes a stable implementation of React Hooks for:&lt;br&gt;
       * React DOM&lt;br&gt;
       * React Native&lt;br&gt;
       * React DOM Server&lt;br&gt;
       * React Test Renderer&lt;br&gt;
       * React Shallow Renderer&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q. Do I need to rewrite all my class components?&lt;/strong&gt;&lt;br&gt;
   No. There are no plans to remove classes from React.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q. What can I do with Hooks that I couldn't with classes?&lt;/strong&gt;&lt;br&gt;
   Hooks offer a powerful and expressive new way to reuse functionality between components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q. How much of my React knowledge stays relevant?&lt;/strong&gt; &lt;br&gt;
   Hooks are a more direct way to use the React features you already know — such as state, lifecycle, context, and refs. They don't fundamentally change how React works, and your knowledge of components, props, and top-down data flow is just as relevant.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q. How to test components that use Hooks?&lt;/strong&gt;&lt;br&gt;
   From React point of view, a component using Hooks is just a regular component. If your testing solution doesn't rely on React internals, testing components with Hooks shouldn't be different from how you normally test components.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;------Thanks for the read.---------&lt;/em&gt;&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>react</category>
      <category>frameworks</category>
    </item>
  </channel>
</rss>
