<?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: stemword</title>
    <description>The latest articles on DEV Community by stemword (@codewithtravel).</description>
    <link>https://dev.to/codewithtravel</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%2F727522%2F5490093c-4f27-4b81-a81e-d261143629a7.png</url>
      <title>DEV Community: stemword</title>
      <link>https://dev.to/codewithtravel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codewithtravel"/>
    <language>en</language>
    <item>
      <title>How to Work with the React Context API and React useContext Hook</title>
      <dc:creator>stemword</dc:creator>
      <pubDate>Fri, 24 Dec 2021 05:44:14 +0000</pubDate>
      <link>https://dev.to/codewithtravel/how-to-work-with-the-react-context-api-and-react-usecontext-hook-d1e</link>
      <guid>https://dev.to/codewithtravel/how-to-work-with-the-react-context-api-and-react-usecontext-hook-d1e</guid>
      <description>&lt;p&gt;React context is primarily used when some data needs to be accessible by many components at different nesting levels. Context provides a way to pass data through the component tree without having to pass props down manually at every level.&lt;/p&gt;

&lt;p&gt;IN react we can pass data in top-down via props, but this will be hard to manage in some cases. So, context provides the best way to share value between components without passing props. You can use context to get the currently authenticated users, themes, etc.&lt;/p&gt;

&lt;p&gt;To use context we need&lt;br&gt;
1.) Context Provider&lt;br&gt;
2.) Context Consumer. (To get data whenever you need).&lt;br&gt;
So let’s take 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;const Expenses = () =&amp;gt; {
  const [expenses] = useState([
    {
      title: "First expense",
      description: "This is my first expense",
      status: false
    }
  ]);
  return (
    &amp;lt;div&amp;gt;
    &amp;lt;h1&amp;gt;Expenses&amp;lt;/h1&amp;gt;
      {expenses.map(note =&amp;gt; {
        return (
        &amp;lt;div&amp;gt;
          &amp;lt;h1&amp;gt;{expense.title}&amp;lt;/h1&amp;gt;
          &amp;lt;h3&amp;gt;{expense.description}&amp;lt;/h3&amp;gt;
          &amp;lt;p&amp;gt;{expense.status? "Paid!" : "Unpaid"}&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
        );
      })}
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just looking at this code, we can notice that we can break this component into smaller components, making our code cleaner and more maintainable. For example, we could create a component called Expense and inside that component, we would have three more components: Title, Description and Status.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Expenses = () =&amp;gt; {
  const [expenses] = useState([
    {
      title: "First expense",
      description: "This is my first expense",
      status: false
    }
  ]);
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Expenses&amp;lt;/h1&amp;gt;
      {expenses.map(({ title, description, done }) =&amp;gt; {
        return &amp;lt;Expense title={title} description={description} status={status} /&amp;gt;;
      })}
    &amp;lt;/div&amp;gt;
  );
};

const Expense = ({ title, description, status}) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Title title={title} /&amp;gt;
      &amp;lt;Description description={description} /&amp;gt;
      &amp;lt;status done={status} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

const Title = ({ title }) =&amp;gt; {
  return &amp;lt;h1&amp;gt;{title}&amp;lt;/h1&amp;gt;;
};

const Description = ({ description }) =&amp;gt; {
  return &amp;lt;h3&amp;gt;{description}&amp;lt;/h3&amp;gt;;
};

const Status= ({ status}) =&amp;gt; {
  return &amp;lt;h3&amp;gt;{status}&amp;lt;/h3&amp;gt;;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now have a few components, and So we can maintain the reusability and maintainability of our example application. But, in the future, if this application grows in size and we feel the need to break these components into smaller components, we might have a problem.&lt;/p&gt;

&lt;p&gt;Passing data through props over and over can cause problems for your application. Sometimes you might pass more props than you need or even forget to pass props that you do need, rename props through the components without notice, etc. If you’re passing data through props from the parent component to a fourth- or fifth-level component, you’re not reusing and writing maintainable code, and this might prejudice your application in the future.&lt;/p&gt;

&lt;p&gt;This is what we call “prop-drilling”.That’s one of the main problems that Context API came to solve for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  createContext
&lt;/h2&gt;

&lt;p&gt;To start with the Context API, the first thing we need to do is create a context using the createContext function from React.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ExpensesContext = createContext([]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The createContext the function accepts an initial value, but this initial value is not required.&lt;/p&gt;

&lt;p&gt;After creating your context, that context now has two React components that are going to be used: Provider and Consumer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Provider
&lt;/h2&gt;

&lt;p&gt;The Provider the component is going to be used to wrap the components that are going to have access to our context.&lt;br&gt;
Let’s take an example. Make store/auth-context.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
const AuthContext = React.createContext({
  isLoggedIn: false
});
export default AuthContext;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now provide context to the component. so in App.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';
import Login from './components/Login/Login';
import Home from './components/Home/Home';
import MainHeader from './components/MainHeader/MainHeader';
import AuthContext from './store/auth-context';
function App() {
  const [isLoggedIn, setIsLoggedIn] = useState(false);
return (
    &amp;lt;AuthContext.Provider
      value={{
        isLoggedIn: isLoggedIn,
      }}
    &amp;gt;
      &amp;lt;MainHeader onLogout={logoutHandler} /&amp;gt;
      &amp;lt;main&amp;gt;
        {!isLoggedIn &amp;amp;&amp;amp; &amp;lt;Login onLogin={loginHandler} /&amp;gt;}
        {isLoggedIn &amp;amp;&amp;amp; &amp;lt;Home onLogout={logoutHandler} /&amp;gt;}
      &amp;lt;/main&amp;gt;
    &amp;lt;/AuthContext.Provider&amp;gt;
  );
}
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can consume it using consumer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import AuthContext from '../../store/auth-context';
const Navigation = (props) =&amp;gt; {
  return (
    &amp;lt;AuthContext.Consumer&amp;gt;
      {(ctx) =&amp;gt; {
        return (
          &amp;lt;nav className={classes.nav}&amp;gt;
            &amp;lt;ul&amp;gt;
              {ctx.isLoggedIn &amp;amp;&amp;amp; (
                &amp;lt;li&amp;gt;
                  &amp;lt;a href="/"&amp;gt;Users&amp;lt;/a&amp;gt;
                &amp;lt;/li&amp;gt;
              )}
              {ctx.isLoggedIn &amp;amp;&amp;amp; (
                &amp;lt;li&amp;gt;
                  &amp;lt;a href="/"&amp;gt;Admin&amp;lt;/a&amp;gt;
                &amp;lt;/li&amp;gt;
              )}
              {ctx.isLoggedIn &amp;amp;&amp;amp; (
                &amp;lt;li&amp;gt;
                  &amp;lt;button onClick={props.onLogout}&amp;gt;Logout&amp;lt;/button&amp;gt;
                &amp;lt;/li&amp;gt;
              )}
            &amp;lt;/ul&amp;gt;
          &amp;lt;/nav&amp;gt;
        );
      }}
    &amp;lt;/AuthContext.Consumer&amp;gt;
  );
};
export default Navigation;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is the useContext hook?
&lt;/h2&gt;

&lt;p&gt;Looking at the example above, the syntax for consuming context may look a bit strange to you.&lt;/p&gt;

&lt;p&gt;Another way of consuming context became available in React 16.8 with the arrival of React hooks. We can now consume context with the useContext hook.&lt;/p&gt;

&lt;p&gt;Instead of using render props, we can pass the entire context object to React.useContext() to consume context at the top of our component.&lt;/p&gt;

&lt;p&gt;Here is the example above using the useContext hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useContext } from 'react';
const Navigation = (props) =&amp;gt; {
  const ctx = useContext(AuthContext);
return (
    &amp;lt;nav className={classes.nav}&amp;gt;
      &amp;lt;ul&amp;gt;
        {ctx.isLoggedIn &amp;amp;&amp;amp; (
          &amp;lt;li&amp;gt;
            &amp;lt;a href="/"&amp;gt;Users&amp;lt;/a&amp;gt;
          &amp;lt;/li&amp;gt;
        )}
        {ctx.isLoggedIn &amp;amp;&amp;amp; (
          &amp;lt;li&amp;gt;
            &amp;lt;a href="/"&amp;gt;Admin&amp;lt;/a&amp;gt;
          &amp;lt;/li&amp;gt;
        )}
        {ctx.isLoggedIn &amp;amp;&amp;amp; (
          &amp;lt;li&amp;gt;
            &amp;lt;button onClick={props.onLogout}&amp;gt;Logout&amp;lt;/button&amp;gt;
          &amp;lt;/li&amp;gt;
        )}
      &amp;lt;/ul&amp;gt;
    &amp;lt;/nav&amp;gt;
  );
};
export default Navigation;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;So, basically, context is sharing data “globally” for react component and using it when you need it.&lt;/p&gt;

&lt;p&gt;I hope this gave you a better understanding of how to use React context. For more such articles, videos, and traveling vlogs you can subscribe to my YouTube channel. &lt;br&gt;
&lt;a href="https://www.youtube.com/c/CodeWithTravel"&gt;https://www.youtube.com/c/CodeWithTravel&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to implement React Table Project Setup and useTable</title>
      <dc:creator>stemword</dc:creator>
      <pubDate>Tue, 14 Dec 2021 04:32:35 +0000</pubDate>
      <link>https://dev.to/codewithtravel/how-to-implement-react-table-project-setup-and-usetable-2ei8</link>
      <guid>https://dev.to/codewithtravel/how-to-implement-react-table-project-setup-and-usetable-2ei8</guid>
      <description>&lt;p&gt;In this article We will learn how to implement react table in our project and understand each and every thing.&lt;br&gt;
So let's install react in our system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app rtdemo
cd rtdemo
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will use this module for displaying the table. React Table is a collection of hooks for building powerful tables and Datagrid experiences. These hooks are lightweight, composable, and ultra-extensible, but do not render any markup or styles for you. This effectively means that React Table is a "headless" UI library.&lt;/p&gt;

&lt;p&gt;React Table is a headless utility, which means out of the box, it doesn't render or supply any actual UI elements. You are in charge of utilizing the state and callbacks of the hooks provided by this library to render your own table 0markup.&lt;/p&gt;

&lt;p&gt;Now install react table module. React Table is compatible with React v16.8+ and works with ReactDOM and React Native.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-table --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit APP.js file and add following code.&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, useMemo } from "react";
import { useTable } from "react-table";
const App = (props) =&amp;gt; {
const [tutorials, setTutorials] = useState([]);
const retrieveTutorials = () =&amp;gt; {
const { tutorials, totalPages } = {
tutorials: [
{
invoice_date: "18 Oct 2021",
company: "ABC Enterprise",
invoice_no: "INV/ABC/21-22/109",
},
{
invoice_date: "19 Oct 2021",
company: "ABC Enterprise",
invoice_no: "INV/ABC/21-22/109",
},
{
invoice_date: "20 Oct 2021",
company: "ABC Enterprise",
invoice_no: "INV/ABC/21-22/109",
},
{
invoice_date: "21 Oct 2021",
company: "ABC Enterprise",
invoice_no: "INV/ABC/21-22/109",
},
{
invoice_date: "22 Oct 2021",
company: "ABC Enterprise",
invoice_no: "INV/ABC/21-22/109",
},
{
invoice_date: "23 Oct 2021",
company: "ABC Enterprise",
invoice_no: "INV/ABC/21-22/109",
},
],
totalPages: 1,
};
setTutorials(tutorials);
};
useEffect(retrieveTutorials, []);
const columns = useMemo(
() =&amp;gt; [
{
Header: "Date",
accessor: "invoice_date",
},
{
Header: "Vendor",
accessor: "company",
},
{
Header: "Invoice No.",
accessor: "invoice_no",
},
],
[]
);
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable({
columns,
data: tutorials,
});
return (
&amp;lt;div&amp;gt;
&amp;lt;table {...getTableProps()}&amp;gt;
&amp;lt;thead&amp;gt;
{headerGroups.map((headerGroup) =&amp;gt; (
&amp;lt;tr {...headerGroup.getHeaderGroupProps()}&amp;gt;
{headerGroup.headers.map((column) =&amp;gt; (
&amp;lt;th {...column.getHeaderProps()}&amp;gt;{column.render("Header")}&amp;lt;/th&amp;gt;
))}
&amp;lt;/tr&amp;gt;
))}
&amp;lt;/thead&amp;gt;
{tutorials.length &amp;gt; 0 ? (
&amp;lt;tbody {...getTableBodyProps()}&amp;gt;
{rows.map((row, i) =&amp;gt; {
prepareRow(row);
return (
&amp;lt;tr {...row.getRowProps()}&amp;gt;
{row.cells.map((cell) =&amp;gt; {
return (
&amp;lt;td {...cell.getCellProps()}&amp;gt;{cell.render("Cell")}&amp;lt;/td&amp;gt;
);
})}
&amp;lt;/tr&amp;gt;
);
})}
&amp;lt;/tbody&amp;gt;
) : (
&amp;lt;tbody&amp;gt;
&amp;lt;tr&amp;gt;
&amp;lt;td colSpan="8"&amp;gt;
&amp;lt;figure className="noRecord-found"&amp;gt;&amp;lt;/figure&amp;gt;
&amp;lt;span className="norecord-text"&amp;gt;No records found&amp;lt;/span&amp;gt;
&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/tbody&amp;gt;
)}
&amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
);
};
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are using useState, useEffect, useMemo hooks.&lt;/p&gt;

&lt;p&gt;useState : The useState hook is a special function that takes the initial state as an argument and returns an array of two entries. Syntax: The first element is the initial state and the second one is a function that is used for updating the state. … And the value returned by the function will be used as the initial state.&lt;/p&gt;

&lt;p&gt;useEffect : What does useEffect do? By using this Hook, you tell React that your component needs to do something after render. React will remember the function you passed (we'll refer to it as our "effect"), and call it later after performing the DOM updates.&lt;/p&gt;

&lt;p&gt;useMemo : React has a built-in hook called useMemo that allows you to memoize expensive functions so that you can avoid calling them on every render. You simple pass in a function and an array of inputs and useMemo will only recompute the memoized value when one of the inputs has changed.&lt;/p&gt;

&lt;p&gt;useTable : useTable is the root hook for React Table. To use it, pass it with an options object with at least a columns and data value, followed by any React Table compatible hooks you want to use.&lt;/p&gt;

&lt;p&gt;getTableProps : This function is used to resolve any props needed for your table wrapper.&lt;/p&gt;

&lt;p&gt;getTableBodyProps : This function is used to resolve any props needed for your table body wrapper.&lt;/p&gt;

&lt;p&gt;headerGroups : An array of normalized header groups, each containing a flattened array of final column objects for that row.&lt;/p&gt;

&lt;p&gt;rows : An array of materialized row objects from the original data array and columns passed into the table options.&lt;/p&gt;

&lt;p&gt;prepareRow : This function is responsible for lazily preparing a row for rendering. Any row that you intend to render in your table needs to be passed to this function before every render.&lt;/p&gt;

&lt;p&gt;Why? Since table data could potentially be very large, it can become very expensive to compute all of the necessary states for every row to be rendered regardless if it actually is rendered or not (for example if you are paginating or virtualizing the rows, you may only have a few rows visible at any given moment). This function allows only the rows you intend to display to be computed and prepped with the correct state.&lt;/p&gt;

&lt;p&gt;So this was all about implementing the basics of React Table using useTable. I hope you have understood the example.&lt;/p&gt;

&lt;p&gt;You can also check out YouTube channel: &lt;a href="https://www.youtube.com/channel/UCOWT2JvRnSMVSboxvccZBFQ"&gt;https://www.youtube.com/channel/UCOWT2JvRnSMVSboxvccZBFQ&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Encrypt and Decrypt in Node JS using Crypto</title>
      <dc:creator>stemword</dc:creator>
      <pubDate>Sat, 16 Oct 2021 17:41:49 +0000</pubDate>
      <link>https://dev.to/codewithtravel/how-to-encrypt-and-decrypt-in-node-js-using-crypto-3fm</link>
      <guid>https://dev.to/codewithtravel/how-to-encrypt-and-decrypt-in-node-js-using-crypto-3fm</guid>
      <description>&lt;p&gt;In this article, you’ll learn how to use the Node.js crypto module to perform cryptographic operations on data. I'll show you how to encrypt data with a secret key and then decrypt it using the same secret key when required.&lt;br&gt;
You can also check video on YouTube. &lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=9PL7EK4jXLM"&gt;https://www.youtube.com/watch?v=9PL7EK4jXLM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a new project&lt;/p&gt;

&lt;p&gt;Create a new directory in your local file system and switch to it by typing the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir encr_decr &amp;amp;&amp;amp; cd encr_decr
npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now install the crypto module&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Now make app.js.&lt;/p&gt;

&lt;p&gt;Add following code to app.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var Crypto = require('crypto');
var secret_key = 'fd85b494-aaaa';
var secret_iv = 'smslt';
var encryptionMethod = 'AES-256-CBC';
var key = Crypto.createHash('sha512').update(secret_key, 'utf-8').digest('hex').substr(0, 32);
var iv = Crypto.createHash('sha512').update(secret_iv, 'utf-8').digest('hex').substr(0, 16);
var encryptedMessage = encrypt_string("hello", encryptionMethod, key, iv);
console.log(encryptedMessage); 
// output : L2dOZjlDVmxoSDNWdmpVMkNGd0JEdz09
var decrptMessage = decrypt_string(encryptedMessage, encryptionMethod, key , iv);
console.log(decrptMessage);  
//output : hello

function encrypt_string(plain_text, encryptionMethod, secret, iv) {
  var encryptor = Crypto.createCipheriv(encryptionMethod, secret, iv);
  var aes_encrypted = encryptor.update(plain_text, 'utf8', 'base64') + encryptor.final('base64');
  return Buffer.from(aes_encrypted).toString('base64');
};

function decrypt_string(encryptedMessage, encryptionMethod, secret, iv) {
  const buff = Buffer.from(encryptedMessage, 'base64');
  encryptedMessage = buff.toString('utf-8');
  var decryptor = Crypto.createDecipheriv(encryptionMethod, secret, iv);
  return decryptor.update(encryptedMessage, 'base64', 'utf8') + decryptor.final('utf8');
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In this article, we looked at how to perform cryptographic operations on a text by using Node.js built-in crypto module.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>crypto</category>
      <category>security</category>
    </item>
  </channel>
</rss>
