<?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: luke9kim8</title>
    <description>The latest articles on DEV Community by luke9kim8 (@luke9kim8).</description>
    <link>https://dev.to/luke9kim8</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%2F300439%2F08708144-15da-47ad-8887-f7a7f99be46c.jpeg</url>
      <title>DEV Community: luke9kim8</title>
      <link>https://dev.to/luke9kim8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luke9kim8"/>
    <language>en</language>
    <item>
      <title>Bits of Good Bootcamp Lecture 3: JavaScript</title>
      <dc:creator>luke9kim8</dc:creator>
      <pubDate>Tue, 14 Sep 2021 04:21:02 +0000</pubDate>
      <link>https://dev.to/luke9kim8/bits-of-good-bootcamp-lecture-3-javascript-1k2d</link>
      <guid>https://dev.to/luke9kim8/bits-of-good-bootcamp-lecture-3-javascript-1k2d</guid>
      <description>&lt;p&gt;So far you have been practicing with HTML and CSS to build simple static web pages. However, most websites (or web apps) are more dynamic. Today, we will take our first step to make our websites dynamic using JavaScript.&lt;/p&gt;

&lt;p&gt;Unfortunately, before we could start using JavaScript on browsers, we need to go over the basics. This lecture will focus on helping you to get comfortable with the JavaScript syntax and get ready to apply it on your HTML/CSS web pages in the next lecture.&lt;/p&gt;

&lt;h2&gt;
  
  
  Language Overview
&lt;/h2&gt;

&lt;p&gt;Here are some things you should know about JavaScript. First, JavaScript is loosely typed, meaning you don’t have to specify the exact type for variables and functions you use. Every modern browser should have some sort of JavaScript Engine that can run your JavaScript code. (include an image of browser - js engine)&lt;/p&gt;

&lt;p&gt;However, JavaScript can also be used as a server side language to build backend APIs. This means that you don’t have to depend on your browser to run your JavaScript. You can just run it on your computer and Node.js will act as the JavaScript engine. You can install Node.js from here &lt;a href="https://nodejs.org/en/download/"&gt;https://nodejs.org/en/download/&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;For Windows people, pay attention to where you install your applications like Node.js, Git, Python, etc. Windows installers do not add your installed programs to path directly, which might give you an error when you try to run it on the command line. They will most likely be installed at &lt;code&gt;C:/Users/[your name]/Program Files&lt;/code&gt;. If you ever get an error trying to run an application x, consider googling “How to add x to PATH Windows”.&lt;/p&gt;

&lt;p&gt;Let’s get started! Open up your VS Code and create a new file called &lt;code&gt;lecture3.js&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hello World
&lt;/h2&gt;

&lt;p&gt;Like all programs, we first start with Hello World. To print a line to the console, you would use &lt;code&gt;console.log("Hello World");&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// lecture3.js
// On your terminal or cmd, run "node lecture3.js"

console.log("hello world");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Declarations
&lt;/h2&gt;

&lt;p&gt;You don't have to explicitly declare the type of the variable like Java or C++ with JavaScript. However, it has its own keywords, which note the &lt;strong&gt;scope&lt;/strong&gt; and the &lt;strong&gt;mutability&lt;/strong&gt; of the variable.  These keywords are &lt;code&gt;const&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;var&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  const
&lt;/h3&gt;

&lt;p&gt;When you don't want to change the variable's assignment, you use &lt;code&gt;const&lt;/code&gt;. Once the variable is declared as &lt;code&gt;const&lt;/code&gt;, you cannot change or reassign it!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pixar = "Cars";
pixar = "Ratatouille";     // Illegal! Can't reassign!
const pixar = "Coco";      // Illegal! Cant' redeclare!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  var
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; used to be the default declaration for most variables. Any variables that are declared with the &lt;code&gt;var&lt;/code&gt; keyword can be updated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 10;        // After initializing x,
if (x &amp;gt; 0) {
    x = 20;        // You can update it b.c. x is declared with var
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But people don't use &lt;code&gt;var&lt;/code&gt; anymore! That is because var is function scoped. As long as it is declared in a function, it can be accessed/updated/reassigned within the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 10;
var x = 20;

if (x &amp;gt; 10) {
    var y = "I live in `if` block!";
}
console.log(y);    // This is totally legal with var!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  let
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; was introduced on ES6 to replace some of &lt;code&gt;var&lt;/code&gt;'s quirks. Unlike &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; is block scoped. This means the above example would be illegal with &lt;code&gt;let&lt;/code&gt; because &lt;code&gt;y&lt;/code&gt; is bounded within the if block. If you try to access it outside of its scope, you will get a reference error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = 10;
let x = 20;        // Illegal! Syntax Error! Already declared!

if (x &amp;gt; 10) {
    let y = "I live in `if` block!";
}
console.log(y);    // Illegal! Reference error!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;blockquote&gt;
&lt;p&gt;When in doubt, use &lt;code&gt;let&lt;/code&gt; over &lt;code&gt;var&lt;/code&gt; to avoid breaking your code&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before we move onto other JS topics, I'd like to quickly note a unique behavior of &lt;code&gt;const&lt;/code&gt; objects and arrays. While you can't reassign a const object and arrays with new instances, you can modify its attribute or add new elements to it. &lt;/p&gt;

&lt;p&gt;When you declare a variable &lt;code&gt;const x = ["hello", "world"]&lt;/code&gt;, you are assigning the reference to the array. This might confuse new programmers, but for now understand that modifying the values of const objects and array is possible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Arrays
const array = [1,2,3,4];
array[0] = -1;            // OK!
array.push(5);            // OK!
array = [4,5,6,7];        // Not OK! This is reassigning!

// Objects
const teacher = {
    name: "Mr. Hendrix",
    subject: ["Math", "Physics"]
    age: 29
}

teacher.age = 30;                            // OK!
teacher.subject = ["Calculus", "Chemistry"]  // OK!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Strings
&lt;/h2&gt;

&lt;p&gt;In JavaScript, Strings are immutable. This means you cannot modify any character of the string or append another string to it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Objects
&lt;/h2&gt;

&lt;p&gt;Objects in Java&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Arrays
&lt;/h2&gt;

</description>
    </item>
    <item>
      <title>Using documentToReactComponents with options</title>
      <dc:creator>luke9kim8</dc:creator>
      <pubDate>Tue, 11 May 2021 03:34:11 +0000</pubDate>
      <link>https://dev.to/luke9kim8/using-documenttoreactcomponents-with-options-36a4</link>
      <guid>https://dev.to/luke9kim8/using-documenttoreactcomponents-with-options-36a4</guid>
      <description>&lt;p&gt;There are many tutorials on how to use Next.js with Contentful, but not a lot talk about how to render rich text files(RTF) with images. In this post, I'll talk about how to render RTFs with images by passing in an option to documentToReactComponents.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Here is a sample blogpost that I want to publish on my personal website.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqm05q51x42veee4obt8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqm05q51x42veee4obt8.png" alt="Screen Shot 2021-05-10 at 10.46.07 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Contentful offers documentToReactComponents function to render its rich text file fields. Install rich-text-react-renderer via &lt;code&gt;npm install @contentful/rich-text-react-renderer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To use documentToReactComponents, first import it in your component. Then, pass in the rich text file field you fetched from Contentful using &lt;code&gt;client.getEntries()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {documentToReactComponents} from '@contentful/rich-text-react-renderer'

export async function getStaticProps({params}) {
  const client = createClient({
    space: process.env.CONTENTFUL_SPACE_ID,
    accessToken: process.env.CONTENTFUL_ACCESS_KEY
  })
  const res = await client.getEntries({content_type: 'blogpost'})
   return {
     props: {
       blogposts: res.items
     }
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my example, I call &lt;code&gt;client.getEntries()&lt;/code&gt; in &lt;code&gt;getStaticProps&lt;/code&gt; and call &lt;code&gt;documentToReactComponents&lt;/code&gt; in the Blog component JSX. &lt;code&gt;blogpost.fields.blogContent&lt;/code&gt; is my rich text file field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Blog = ({blogposts}) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Blog&amp;lt;/h1&amp;gt;
      {blogposts.map(blogpost =&amp;gt; (
        &amp;lt;div&amp;gt;
          &amp;lt;h1&amp;gt;{blogpost.fields.title}&amp;lt;/h1&amp;gt;
          &amp;lt;div&amp;gt;{documentToReactComponents(blogpost.fields.blogContent)}&amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
      ))}
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should render some data on your app, but not all of them. In my situation, I don't see any images despite I embedded them in my RTF.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6bybua10ypopyls7qj34.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6bybua10ypopyls7qj34.png" alt="Screen Shot 2021-05-10 at 10.58.02 PM"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;To solve this, we need to specify how to render these embedded data. We can do this by passing a second &lt;code&gt;renderOption&lt;/code&gt; parameter to &lt;code&gt;documentToReactComponents&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;renderOption&lt;/code&gt; is just an object with a renderNode key. The value associated with renderNode is an object specifying how to render the content on the RTF.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const renderOption = {
  renderNode: {
    [BLOCKS.EMBEDDED_ASSET]: (node, children) =&amp;gt; {
      return (&amp;lt;Image
        src={`https:${node.data.target.fields.file.url}`}
        height={node.data.target.fields.file.details.image.height}
        width={node.data.target.fields.file.details.image.width}
      /&amp;gt;)
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The keys of &lt;code&gt;renderNode&lt;/code&gt; specify different types of content (like heading 1, paragraphs, or embedded assets), and the values specify the functions that transform the content to the format we want. This transformation function takes in two parameters: node and children. The node refers to the node itself while the children refers to children node of the specified content.&lt;/p&gt;

&lt;p&gt; &lt;br&gt;
 &lt;br&gt;
Helpful resources where I got all of these from&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.contentful.com/blog/2021/04/14/rendering-linked-assets-entries-in-contentful/" rel="noopener noreferrer"&gt;&lt;strong&gt;Rendering linked assets and entries in the Contentful Rich Text field&lt;/strong&gt;&lt;/a&gt; by Salma Alam-Naylor&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=MSbpKwZMaXQ" rel="noopener noreferrer"&gt;&lt;strong&gt;Contentful Strategies: How To Render RichText&lt;/strong&gt;&lt;/a&gt; by Stefan Judis&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>nextjs</category>
      <category>contentful</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
