<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Roshan]]></title><description><![CDATA[Front-end developer moving towards full-stack development. Exploring mobile dev with flutter and react native. Free time blogger.]]></description><link>https://roshankc.info.np</link><generator>RSS for Node</generator><lastBuildDate>Mon, 14 Sep 2026 11:03:50 GMT</lastBuildDate><atom:link href="https://roshankc.info.np/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[CustomAI - Create AI Image classification API without code | Made with Modus, HyperMode, and Dgraph]]></title><description><![CDATA[Before we get started I know you might be having a hard time believing how I made this app in 24 hours. Don’t worry I haven’t slept or moved from my chair and desktop for more than 10 minutes in these past 24 hours. Now let’s get started with the top...]]></description><link>https://roshankc.info.np/how-i-made-customai-app-in-just-24-hours-with-modus-hypermode-and-dgraph</link><guid isPermaLink="true">https://roshankc.info.np/how-i-made-customai-app-in-just-24-hours-with-modus-hypermode-and-dgraph</guid><category><![CDATA[ModusHack]]></category><category><![CDATA[HyperNode]]></category><category><![CDATA[modus]]></category><category><![CDATA[Dgraph]]></category><category><![CDATA[Hashnode]]></category><category><![CDATA[AI]]></category><dc:creator><![CDATA[Roshan kc]]></dc:creator><pubDate>Sat, 30 Nov 2024 19:27:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1732990759476/7e913269-1295-43fa-ac9b-188993d9899e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Before we get started I know you might be having a hard time believing how I made this app in 24 hours. Don’t worry I haven’t slept or moved from my chair and desktop for more than 10 minutes in these past 24 hours. Now let’s get started with the topic of the day. What is customAI, what it does and why did I choose the above-mentioned tech stack to build this project? Let’s get started!</p>
<p>As a student and working professionals, we have to go through lots of projects, and assignments and don’t even have free time left to enjoy life. For those monthly projects in college half of them are about building some cool AI-powered image classification App, for which we have to spend lots of time. Whether disease prediction or classification, dog breed classification, most of the projects done by a student in their lifetime include one of these apps. Talking about our work, most of time we need to test with data and develop some quick test solutions with them. Now let’s discuss what is the problem here and why I made this app.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732989711895/9915d0fe-b343-4010-9192-37fc762b5518.gif" alt class="image--center mx-auto" /></p>
<h3 id="heading-what-made-me-think-about-making-custom-ai">What made me think about Making Custom AI</h3>
<p>Whenever I think about making a project, I think of an app that involves AI. Recently I thought about making an app that helps to diagnose tuberculosis. In my past experiences also I have built a similar type of project like paddy disease prediction, malaria prediction, etc that involves providing labeled data and running inferences, spending hours to train data, and then just getting disappointed at the last minute by creating some half working API. I am thinking here I am not alone and most of you have done that too.</p>
<p>For building those projects, I have to do intense research, search for datasets, and then train the model, and spend days creating API. Then I thought Ahh! So why not just create a platform that takes your label images and performs a vector similarity search to just return your label from the most similar image? This way I can just create those projects in a few minutes and focus my entire time watching series and playing around.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732989764379/82999950-26a4-43c7-8bb1-1db968949c5b.gif" alt class="image--center mx-auto" /></p>
<p>Jokes aside now let’s talk the real-world problems. Let’s say I run multiple businesses. I own an e-commerce store for clothes. I have to organize those clothes, identify which clothes are under what label, and suggest those clothes with similar labels accordingly. For these I have to spend days or even months to create a API that does this. CustomAI can help you with this problem.</p>
<p><strong><em>The problem customAI deals with is it helps you create ready-to-use API for your labeled image dataset in just a few minutes or hours, which you can then integrate into your applications. It can be your day-to-day projects, assignments, or solving your crucial business needs.</em></strong></p>
<h3 id="heading-initial-brainstorming-and-browsing">Initial BrainStorming and browsing</h3>
<p>I had a rough idea about what I was going to build. Then I started listing the features and started brainstorming. I had to find technical, time, and economic feasibility. Then I came across hypermode modus and dgraph. Features that helped me decide why I chose them:</p>
<ul>
<li><p>I had to provide users the ability to query their own data. I couldn’t just make endpoints for everyone. I have to use GraphQL. Then I came across Modus. Code Functions and get ready to use graphql endpoints. What more could I ask for?</p>
</li>
<li><p>I have to store data and my app will be dealing with vector embeddings. Any good database that works well with Modus? Yes, Dgraph. More than that It provided me ready to use graphql API, which I just had to integrate with my Modus application to play with selected data and endpoints. I began questioning discord members from the hypermode channel and started my journey.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732991763968/d79c18b9-c320-425f-aae6-66436e5fd291.gif" alt class="image--center mx-auto" /></p>
<h3 id="heading-exploring-modus-dgraph-and-going-through-discord-helps">Exploring Modus, Dgraph, and going through discord helps</h3>
<p>I started going through Modus and dgraph documentation. Browsed everything and here are some key points that I found.</p>
<p><a target="_blank" href="https://docs.hypermode.com/modus/overview"><strong>For modus</strong></a> 🛠️</p>
<ul>
<li><p>It is an open-source serverless framework for building powerful AI-powered applications.</p>
</li>
<li><p>You can write code either in webAssembly or Go.</p>
</li>
<li><p>You can invoke AI models just like you invoke functions.</p>
</li>
<li><p>Easily secure your endpoints and add authentications.</p>
</li>
</ul>
<p><a target="_blank" href="https://dgraph.io/docs/"><strong>For Dgraph</strong></a> 🧠⃤</p>
<ul>
<li><p>Powerful Graph Database</p>
</li>
<li><p>Dgraph Cloud provides you ready to use graphql API along with features to explore API and data in their platform.</p>
</li>
<li><p>Allows vector embedding storage and embedding relation operations.</p>
</li>
<li><p>Best for AI-powered applications not limited to being vector databases alone but multipurpose ones.</p>
</li>
</ul>
<h3 id="heading-how-customai-works">How customAI works</h3>
<p>With customAI you can create a project, upload your labeled images, and get ready to use API to run inferences to find similar images and labels. You can create multiple projects and create different API for your different needs. You can also run test for the dataset you uploaded.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732994122392/b572c7ac-7084-41f5-bb7f-e3f4a50fa363.jpeg" alt class="image--center mx-auto" /></p>
<h3 id="heading-building-backend-with-hypermode-modus-and-dgraph">Building backend with hypermode, modus and Dgraph</h3>
<p>Here is the general workflow for my project creation journey.</p>
<p><strong>Getting started with hypermode and dgraph</strong></p>
<p>Go to hypermode and sign up for your account. Then click Create Project.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732994430549/f0378154-9106-40df-88b9-d601b191889d.jpeg" alt class="image--center mx-auto" /></p>
<p>Then connect your github account and follow the steps mentioned. You just have to upload github workflow files and it will automatically build backend deployment for you with you commits.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732994630596/19b2be30-6175-4656-a1d3-2a24f4295954.jpeg" alt class="image--center mx-auto" /></p>
<p><strong>Graphql schema defining in dgraph database</strong></p>
<p>Here is how I defined my Graphql schema.</p>
<pre><code class="lang-plaintext"> type User {
  id: ID!
  clerkId: String! @search(by: [exact])
  email: String
  projects: [Project] @hasInverse(field: user)
}

type Project {
  id: ID!
  name: String
  description: String
  owner: String @search(by:[exact])
  user: User
  datasets: [Image] @hasInverse(field: project)
  isDeployed:Boolean
  endpoint: String
  token: String
}

type Image {
  id: ID! 
  imageUrl: String!
  label: String @search(by:[exact])
  project: Project
  projectId: String @search(by: [exact])
  token: String @search(by: [exact])
  owner:String @search(by:[exact])
  embedding: [Float!] @embedding @search(by: ["hnsw(metric: euclidean, exponent: 4)"])
}
</code></pre>
<h3 id="heading-problem-encountered-with-type-defining-in-dgraph"><strong>Problem encountered with type defining in dgraph</strong></h3>
<p>Then I started building with modus. You can create modus app by installing modus cli and with modus new command. Basic step is given below.</p>
<p><code>npm install -g @hypermode/modus-cli</code></p>
<p><code>modus new</code></p>
<p>Now follow the questions asked and you are good to go.</p>
<p>For querying graphql api you have to define the connection in the modus.json file.</p>
<pre><code class="lang-json">  <span class="hljs-string">"connections"</span>: {
    <span class="hljs-attr">"flaskapi"</span>: {
      <span class="hljs-attr">"type"</span>: <span class="hljs-string">"http"</span>,
      <span class="hljs-attr">"endpoint"</span>: <span class="hljs-string">"https://embeddingextractor.com/extract-embedding"</span>
    },
      <span class="hljs-attr">"dgraph"</span>: {
        <span class="hljs-attr">"type"</span>: <span class="hljs-string">"http"</span>,
        <span class="hljs-attr">"endpoint"</span>: <span class="hljs-string">"https://yourapi/graphql"</span>,
        <span class="hljs-attr">"headers"</span>: {
          <span class="hljs-attr">"Authorization"</span>: <span class="hljs-string">"Bearer {{API_KEY}}"</span>
        }
      }
  }
</code></pre>
<p>I have used two api endpoints. One had AI model to extract embeddings and another was dgraph graphql API.</p>
<p>I had faced lots of type mismatched issues between modus queries and dgraph graphql queries. The major ones were between type String! and String, type ID, and [ID!]. I simply checked my schema definition while writing mutations or queries in modus. Here is how I did that.</p>
<p>Here is my project type from graphql schema</p>
<p><code>type Project { id: ID! name: String description: String owner: String @search(by:[exact]) user: User datasets: [Image] @hasInverse(field: project) isDeployed:Boolean endpoint: String token: String }</code></p>
<p>All I had to do was mention same type in the first line of mutation statement.</p>
<pre><code class="lang-typescript">  <span class="hljs-keyword">const</span> statement = <span class="hljs-string">`
    mutation updateProject($id: string!, $owner: String!, $name: String!, $endpoint: String!, $isDeployed: Boolean!, $description: String!, $token: String!) {
        updateProject(input: {filter: {id: {eq: $id}}, set: {name: $name, endpoint: $endpoint, isDeployed: $isDeployed, description: $description, token: $token, owner: $owner}}) {
            numUids
        }
    }
`</span>;
</code></pre>
<p>I wrote String! as a variable type for a variable that maps to the same type in my schema. Problem solved!</p>
<h3 id="heading-problem-encounter-with-matching-types-of-data-between-modus-and-dgraph-graphql-api-response"><strong>Problem encounter with matching types of data between modus and dgraph graphql API response</strong></h3>
<p>Query was working in dgraph graphql API playground but I wasn’t getting any results back for the same query. I was going nowhere due to those null results while querying graphql endpoints in modus. Then after hours of research, checking types and everything I found the one major issue was response wasn’t properly mapped. Here is how I learned how to do so easily.</p>
<p>Go to dgraph and query the data. Now observe properly the returned data. You will see something like this:</p>
<pre><code class="lang-plaintext">{
data: [{
id: “iowahe”,
name: “roshan”
}]
}
</code></pre>
<p>Now you have to define class in your modus function at first with @json. If i want those id and name my class will be like this.</p>
<pre><code class="lang-typescript"><span class="hljs-meta">@json</span>
<span class="hljs-keyword">class</span> ProjectData {
  id: <span class="hljs-built_in">string</span>;
  name: <span class="hljs-built_in">string</span>;
}
</code></pre>
<p>Then as you see the response is an array so create a new response class to get those arrays.</p>
<pre><code class="lang-typescript"><span class="hljs-meta">@json</span>
<span class="hljs-keyword">class</span> ProjectResponse {
  queryProject: ProjectData[];
}
</code></pre>
<p>Then create a function to query and get a result. Your <code>graphql.execute</code> function will now have <code>ProjectResponse</code> class so it properly maps and returns value.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getProjects</span>(<span class="hljs-params">userId: <span class="hljs-built_in">string</span></span>): <span class="hljs-title">ProjectData</span>[] </span>{
  <span class="hljs-keyword">const</span> statement = <span class="hljs-string">`
    query queryProject($userId: String) {
        queryProject(filter: {owner: {eq: $userId}}) {
           description
            endpoint
            id
            isDeployed
            name
            owner
            token
        }
    }
`</span>;

  <span class="hljs-keyword">const</span> vars = <span class="hljs-keyword">new</span> graphql.Variables();
  vars.set(<span class="hljs-string">"userId"</span>, userId);

  <span class="hljs-keyword">const</span> response = graphql.execute&lt;ProjectResponse&gt;(
    connection,
    statement,
    vars,
  );

  <span class="hljs-keyword">return</span> response.data!.queryProject;
}
</code></pre>
<p>This simple thing took me hours to learn and after that took minutes to build my entire enpoints. Hope you will find this useful.</p>
<h3 id="heading-how-i-added-filtered-in-dgraph-graphql-api"><strong>How I added filtered in dgraph graphql API</strong></h3>
<p>I was having hard time on how to filter the embedding search to limit it for only the project created by the specific user. Then I found one easy way to add a filter in dgraph graphql. Here is how I did that.</p>
<p>First, go to schema and add <code>@search(by: [exact])</code> in the attribute by which you want to filter your queries. This indicates you can search by the exact value.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732993030818/e069298f-5143-4e68-be1f-3b77ae52fee0.jpeg" alt class="image--center mx-auto" /></p>
<p>Now I went to graphql tab to play around with queries. That gave me query to use in my modus application for retrieving data.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732993119700/f1070362-7da6-4b1e-982b-e870e533f3e9.jpeg" alt class="image--center mx-auto" /></p>
<p>The last thing remaining was to create an endpoint to filter through this data in modus. Here is how I did that.</p>
<pre><code class="lang-plaintext">import { graphql } from "@hypermode/modus-sdk-as";
import { embeddingGenerate } from "../helpers/embeddings";
import { JSON } from "json-as";

const connection: string = "dgraph";


@json
class LabelData {
  label: string;
}


@json
class QueryImageByEmbeddingResponse {
  querySimilarImageByEmbedding: LabelData[];
}

export function runInference(
  imageUrl: string,
  projectId: string,
  token: string,
): LabelData[] {
  const embedding = embeddingGenerate(imageUrl);
  const data = embedding.embedding;

  const log = JSON.stringify(data);
  console.log(log);
  const statement = `
  query querySimilarImageByEmbedding($embeddings: [Float!]!, $projectId: String, $token: String) {
      querySimilarImageByEmbedding(by: embedding, topK:1, vector: $embeddings, filter: {projectId: {eq: $projectId}, token: {eq: $token}}) {
        label
      }
  }
`;

  const vars = new graphql.Variables();
  vars.set("embeddings", data);
  vars.set("token", token);
  vars.set("projectId", projectId);

  const response = graphql.execute&lt;QueryImageByEmbeddingResponse&gt;(
    connection,
    statement,
    vars,
  );

  const resdata = response.data!.querySimilarImageByEmbedding;
  const logdata = JSON.stringify(resdata);
  console.log(logdata);

  return resdata;
}
</code></pre>
<p>Here you can see I created a class for the label and mapped the response. Like this, I implemented for my every endpoints, minimizing errors and rapid developments.</p>
<h3 id="heading-building-frontend-for-customai">Building Frontend For customAI</h3>
<p>The problem that I faced while building the backend was integrating with API, matching types for data sent and received, and everything. It was such a helpless situation. I have to query through Apollo client, write mutations and queries one by one and aah that is such as nightmare.</p>
<p>But guess what building frontend with nextjs was rapid not because I could create components faster but because I came across modus API explorer for the backend API that I created with modus. Here is an example I will show you.</p>
<p>First, run your modus app with <code>modus dev</code> the command go to the API endpoint with <code>http://localhost:8686/explorer</code></p>
<p>Here you can see options to choose between graphql and Typescript.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732992126988/7229b49c-8329-4ede-84bc-59f18be026b2.jpeg" alt class="image--center mx-auto" /></p>
<p>I choose typescript. Then I was surprised it provided me necessary codes to make queries. Lack of time, sleepless nights, and what’s even a hackathon if you don’t use hacks like that? I pasted that in my component and made the request.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732992162332/337a8930-092d-4470-af83-7435d7d78790.jpeg" alt class="image--center mx-auto" /></p>
<p>And with such ease, I could make requests to API endpoints and get data. I began implementing features rapidly in my application after that.</p>
<h3 id="heading-usage">Usage</h3>
<p>Currently, you can create image inferences API with customAI. It utilizes vector embedding and similarity search in dgraph database to return labels of similar items. Here is how you can use CustomAI.</p>
<ul>
<li><p>Open <a target="_blank" href="https://customai-eight.vercel.app/">customAI website</a> and click Get Started.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732990316912/084b38ed-f459-4da8-b373-73b4082a46fa.jpeg" alt class="image--center mx-auto" /></p>
</li>
<li><p>Log in with your email account. Then click on Create Project. Fill in your project details.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732990915830/b3b1a651-2722-43ec-b753-3781339b5b9f.jpeg" alt class="image--center mx-auto" /></p>
</li>
<li><p>Click on your project. Now you can upload your images and labels one by one. I have done this to prevent abuse of the demo for now.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732991037667/4c9450fc-b879-4c7d-85b2-23c7d41be7ac.jpeg" alt class="image--center mx-auto" /></p>
</li>
<li><p>Then you can test and upload one of the images you provided. This is because you might not have a good labeled dataset.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732991074374/762a0f93-6f9d-4947-94d9-dd6d372fb887.jpeg" alt class="image--center mx-auto" /></p>
</li>
<li><p>Get your API and integrate it into your application.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732991076959/2243afe0-02b7-4dc7-a332-11696dbe3870.jpeg" alt class="image--center mx-auto" /></p>
</li>
</ul>
<h3 id="heading-future-of-customai-with-modus-and-dgraph">Future of CustomAI with modus and dgraph 🚀🔜</h3>
<p>Dgraph and modus have been core technologies for my application. My future thoughts for customAI are providing the ability for people to create their own AI features to use within the application or outside by creating a dedicated API as it does now. Here are some future improvements and additions that I have thought about adding to my project.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732989653107/96cabb4b-c7aa-4012-a539-524f092298e2.gif" alt class="image--center mx-auto" /></p>
<ul>
<li><p>💲 <strong>Adding a subscription tier to the platform</strong>. Implementing credit system for API calls.</p>
</li>
<li><p>⚡ <strong>Adding the ability to create multipurpose text-based API</strong> with custom data and prompts utilizing the llms provided by hypermode. For example, uploading college notes to generate important questions from own content provided.</p>
</li>
<li><p>💼 <strong>The tailored algorithm as per use case:</strong> Adding the Ability to choose the best possible way between algorithms or to train custom train models as per need in order to generate API or feature.</p>
</li>
<li><p>🛠️ <strong>Adding custom feature creation ability.</strong> Not only creating API, user can create and use the features with text and image data of their own, extended by the powerful AI models integrated through modus. For example: User can create their own best health insurance plans finding feature by providing updated policies from their own countries or regions in CustomAI by creating a new project. The project can be the custom features we want, which will work for specific tasks that we want.</p>
</li>
</ul>
<p>Many more things can be added but I have listed these four which can show you future potential.</p>
<ul>
<li><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1732989151589/1377486e-0a37-4fa9-bee7-5ea6b17c0d15.gif" alt class="image--center mx-auto" /></li>
</ul>
<h3 id="heading-technologies-used">Technologies used <strong>⚙️</strong></h3>
<p>Here are the major technologies that helped me build this project.</p>
<ul>
<li><p><strong>Modus and AssemblyScript:</strong> A framework that uses typescript-like language and Go programming language and enables you to create powerful AI-powered and or non-AI multi-functional applications.</p>
</li>
<li><p><strong>HyperMode:</strong> Platform that allows you to host your modus backend and provides ready-to-use state-of-the-art AI models that can be easily integrated into backends.</p>
</li>
<li><p><strong>Dgraph database and dgraph cloud:</strong> A graph database that has a feature to store vector embeddings and perform operations like similarity search and a platform that provides ready-to-use Graphql API.</p>
</li>
<li><p><strong>Nextjs:</strong> Full stack framework to develop scalable efficient and powerful web applications typically used to create the front end of web apps.</p>
</li>
<li><p><strong>Clerk:</strong> A simple-to-use authentication management service.</p>
</li>
<li><p><strong>Cloudinary:</strong> Easy and secure cloud storage service optimized for image data.</p>
</li>
</ul>
<p>Thank you for reading through my blog. I am happy to share my knowledge, learnings, problems encountered and experiences of solving them with you all. Feel free to share it with your friends and have a good holiday!😁🌸</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://youtu.be/L2b00SRj4uE">https://youtu.be/L2b00SRj4uE</a></div>
<p> </p>
<h3 id="heading-important-links">Important Links <strong>🔗</strong></h3>
<p><a target="_blank" href="https://github.com/roshankcpkr/customai/"><strong>Github Repository - Frontend and backend</strong></a> 📦</p>
<p><strong>Demo URL -</strong> <a target="_blank" href="https://customai-eight.vercel.app/"><strong>https://customai-eight.vercel.app/</strong></a> 🚚</p>
<p><a target="_blank" href="https://docs.hypermode.com/modus/"><strong>Modus Documentation</strong></a> 🔨</p>
<p><a target="_blank" href="https://dgraph.io/blog/post/v24-graphql/"><strong>Dgraph Similarity search</strong></a> 🧠</p>
<p><a target="_blank" href="https://hypermode.com/"><strong>Hypermode website</strong></a> 🏠</p>
]]></content:encoded></item><item><title><![CDATA[Physicalli - A physical job posting platform]]></title><description><![CDATA[Story behind
I am an introverted person and like to spend most of my time inside the house. Because of this reason I like to find solutions to my problems from home i.e online. Normally I can buy things, and find/hire freelancing remote work online. ...]]></description><link>https://roshankc.info.np/physicalli-a-physical-job-posting-platform</link><guid isPermaLink="true">https://roshankc.info.np/physicalli-a-physical-job-posting-platform</guid><category><![CDATA[AWS Amplify]]></category><category><![CDATA[AWS Amplify Hackathon]]></category><category><![CDATA[AWS]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[Roshan kc]]></dc:creator><pubDate>Fri, 30 Sep 2022 21:42:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1665129956908/QaEG80ASz.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-story-behind">Story behind</h2>
<p>I am an introverted person and like to spend most of my time inside the house. Because of this reason I like to find solutions to my problems from home i.e online. Normally I can buy things, and find/hire freelancing remote work online. What if I need to repair something in my house? If a light bulb breaks or if I need to paint the house then I just need to roam around and find suitable workers which are really boring. </p>
<p>This is where physicalli comes to save the day of people like me and others. It is a platform dedicated to posting and finding physical jobs. You can post the job and find a suitable professional from your local neighborhood. You can search for the jobs available in your neighborhood.</p>
<h2 id="heading-introduction-to-physicalli">Introduction to physicalli</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1664564562532/_kGijux6G.jpg" alt="physicalli_home.jpg" /></p>
<p>Physicalli is a platform to post physically presence demanding jobs like painting, plumbing, electric repairing, etc. You can post your job and get contacted by a technician from your city. You can also search for workers according to your location. Users can just create accounts and find suitable work available near them or post their works. It is just a beautiful platform to make your physical labor easier by connecting workers and works available near them.</p>
<h2 id="heading-technologies-used">Technologies used</h2>
<p>I have made this app with the help of AWS amplify and reactjs.
Here is the list of tech stacks that are used to build this platform.</p>
<ul>
<li>Reactjs - For frontend</li>
<li>tailwindcss - For designing the user interface</li>
<li>AWS Amplify - For backend and hosting</li>
<li>AWS S3 - For image storage</li>
<li>Google maps API - For location autocomplete while posting and searching job</li>
<li>Graphql with help of amplify - For querying database</li>
</ul>
<h2 id="heading-how-does-it-work">How does it work?</h2>
<p>  To use physicalli you need to signup for an account. You can create an account to email and verify it. After that, you can navigate to your respective places and do your work.</p>
<p><strong>Schema for graphql</strong>
I used amplify to create the schema. Visual editor of amplify made it easier to define schema for physicalli.</p>
<pre><code>type Jobpost @model
@auth(rules: [
    { <span class="hljs-attr">allow</span>: owner },
    { <span class="hljs-attr">allow</span>: public, <span class="hljs-attr">operations</span>: [read] },
    { <span class="hljs-attr">allow</span>: private, <span class="hljs-attr">operations</span>: [read] }
  ]){
  <span class="hljs-attr">id</span>: ID!
  title: <span class="hljs-built_in">String</span>!
  description: <span class="hljs-built_in">String</span>!
  image: <span class="hljs-built_in">String</span>
  <span class="hljs-attr">price</span>: <span class="hljs-built_in">String</span>
  <span class="hljs-attr">contact</span>: <span class="hljs-built_in">String</span>!
  location: <span class="hljs-built_in">String</span>!
  createdAt: AWSDateTime
  <span class="hljs-attr">updatedAt</span>: AWSDateTime
  <span class="hljs-attr">owner</span>: <span class="hljs-built_in">String</span>
  <span class="hljs-attr">pro</span>: <span class="hljs-built_in">String</span>!
}

type User @model @auth(rules: [
    { <span class="hljs-attr">allow</span>: owner },
    { <span class="hljs-attr">allow</span>: public, <span class="hljs-attr">operations</span>: [read] },
    { <span class="hljs-attr">allow</span>: private, <span class="hljs-attr">operations</span>: [read] }
  ]){
  <span class="hljs-attr">id</span>: ID!
  firstname: <span class="hljs-built_in">String</span>!
  lastname: <span class="hljs-built_in">String</span>!
  email: <span class="hljs-built_in">String</span>!
  description: <span class="hljs-built_in">String</span>
  <span class="hljs-attr">skill</span>: [<span class="hljs-built_in">String</span>]
  <span class="hljs-attr">profilepic</span>: <span class="hljs-built_in">String</span>
  <span class="hljs-attr">createdAt</span>: AWSDateTime
  <span class="hljs-attr">updatedAt</span>: AWSDateTime
  <span class="hljs-attr">owner</span>: <span class="hljs-built_in">String</span>
  <span class="hljs-attr">location</span>: <span class="hljs-built_in">String</span>!
}
</code></pre><ul>
<li>Create an account with your email address and login</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1664564944376/qtSZdCILo.jpg" alt="login_demo.jpg" />
I used amplify to add authentication to the application. Only authenticated users can perform activities like job posting, editing jobs posted by you, and searching for jobs.</p>
<ul>
<li>On the dashboard page, you can choose what you want to do.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1665164072395/6abhRHs-v.jpg" alt="dashboards.jpg" /></p>
<ul>
<li>Click on Post job to post your work on the app</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1665047040959/0zBfiCpQo.jpg" alt="postjob.jpg" />
You can post images related to your job. Location autocomplete is made with help of google maps API. You can include details about jobs that are saved to dynamodb with help of graphql. S3 is used to store images related to jobs.</p>
<ul>
<li>You can click on the find job option if you want to find work.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1665047077843/Q_PwvXF24.jpg" alt="findjob.jpg" /></p>
<ul>
<li>You can also click on Check your post option from the dashboard to edit/delete posts done by you.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1665164181251/u91E598EW.jpg" alt="viewedit.jpg" /></p>
<h2 id="heading-features-of-physicalli">Features of physicalli</h2>
<p>Here are some of the features of physicalli</p>
<ul>
<li><p><strong> Post job</strong>
You can post a job with details about the job and your location and contact details. You can include an image of the job also.</p>
</li>
<li><p><strong> Find Job</strong>
You can browse jobs and also search for jobs as per your preferred location and title. Google maps API is used to autocomplete location searches.</p>
</li>
<li><p><strong>View Job posted by You</strong>
You can edit and delete job posted by you. This can be helpful to delete job if it is completed and edit job if you made mistake while posting. </p>
</li>
<li><p><strong>Update your profile</strong>
You can update your profile.</p>
</li>
</ul>
<h2 id="heading-challenges-faced-during-development">Challenges faced during development</h2>
<p>I was completely new to aws amplify. I had to learn a great deal about what it is and how to use it. I found so many errors during development and couldn't find reliable resources.</p>
<h2 id="heading-hosting">Hosting</h2>
<p>Amplify is used to host applications. After completing I ran the command <strong>amplify publish</strong> which hosted my application and provided live URL. If we made changes to our project code on the local machine then we can enter the same command (amplify publish) on the terminal at the root directory of the project which will reflect changes to the live URL.</p>
<h2 id="heading-some-url">Some URL</h2>
<ul>
<li><strong>GitHub repo:</strong>  <a target="_blank" href="https://github.com/roshankcpkr/physicalli">https://github.com/roshankcpkr/physicalli</a></li>
<li><strong>Demo URL: </strong> <a target="_blank" href="https://dev.d35k3ru9hsakk9.amplifyapp.com">https://dev.d35k3ru9hsakk9.amplifyapp.com</a></li>
</ul>
<h2 id="heading-demo-credentials">Demo Credentials</h2>
<p>Here is a demo credential that you can use to sign in to the application.</p>
<p>Email: pkrrosh@gmail.com</p>
<p>Password: Yagami123@</p>
<h2 id="heading-future-improvements">Future improvements</h2>
<p>I have some future improvement plans which I was unable to complete due to lack of time.
Here are some of the features which I will add soon.</p>
<ul>
<li>Fetch the location with help of a map.</li>
<li>Feature of saving jobs for future reference</li>
<li>Search for freelancers according to location</li>
<li>Using machine learning to suggest jobs</li>
<li>Real-time chat between client and worker</li>
</ul>
<h2 id="heading-my-final-thoughts">My final thoughts</h2>
<p>This hackathon has been a great learning opportunity. I was able to create a platform that could solve a problem that many people might be facing regarding physical or local job postings and searching. I learned various AWS services such as S3, Amplify, Cognito, and Dynamodb, Google maps. Thank you <a target="_blank" href="https://hashnode.com">Hashnode</a> and <a target="_blank" href="https://aws.amazon.com/amplify/">amplify</a> for organizing such an amazon hackathon. </p>
]]></content:encoded></item><item><title><![CDATA[Different ways to passing props and mapping in Reactjs - Day 3]]></title><description><![CDATA[Props are a great way on react to deal with data to make our component reusable. In our previous article, we have learned how to pass and receive props. Today we will learn how to map over data to make our component dynamic and our code more self-sus...]]></description><link>https://roshankc.info.np/mapping-in-reactjs</link><guid isPermaLink="true">https://roshankc.info.np/mapping-in-reactjs</guid><category><![CDATA[Mapping]]></category><category><![CDATA[Spread operator]]></category><category><![CDATA[props]]></category><category><![CDATA[passing props in react]]></category><category><![CDATA[Props in reactjs]]></category><dc:creator><![CDATA[Roshan kc]]></dc:creator><pubDate>Fri, 09 Sep 2022 04:13:41 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1662696737731/eYkhv24Ib.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Props are a great way on react to deal with data to make our component reusable. In our <a target="_blank" href="https://roshankc.info.np/reusable-components-with-props-in-reactjs">previous article</a>, we have learned how to pass and receive props. Today we will learn how to map over data to make our component dynamic and our code more self-sustaining.</p>
<h3 id="heading-the-problem-of-creating-components-manually">The problem of creating components manually</h3>
<p>We had created a card component on our previous blog. Here is the code of App.js file where we were using Card component.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Card</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./card'</span>
<span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">data</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./data.js'</span>

<span class="hljs-title"><span class="hljs-keyword">function</span></span> <span class="hljs-title">App</span>() {
  <span class="hljs-title">let</span> <span class="hljs-title">firstData</span> <span class="hljs-operator">=</span> <span class="hljs-title">data</span>[0]
  <span class="hljs-title">let</span> <span class="hljs-title">secondData</span> <span class="hljs-operator">=</span> <span class="hljs-title">data</span>[1]
  <span class="hljs-title">let</span> <span class="hljs-title">thirdData</span> <span class="hljs-operator">=</span> <span class="hljs-title">data</span>[2]
  <span class="hljs-title"><span class="hljs-keyword">return</span></span> (
    <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"App"</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span><span class="hljs-title">Card</span>
        <span class="hljs-title">name</span> <span class="hljs-operator">=</span> {<span class="hljs-title">firstData</span>.<span class="hljs-title">name</span>}
        <span class="hljs-title">pic</span> <span class="hljs-operator">=</span> {<span class="hljs-title">firstData</span>.<span class="hljs-title">profilePic</span>}
        <span class="hljs-title">title</span> <span class="hljs-operator">=</span> {<span class="hljs-title">firstData</span>.<span class="hljs-title">title</span>}
        <span class="hljs-title">email</span> <span class="hljs-operator">=</span> {<span class="hljs-title">firstData</span>.<span class="hljs-title">email</span>}
        <span class="hljs-title">about</span> <span class="hljs-operator">=</span> {<span class="hljs-title">firstData</span>.<span class="hljs-title">about</span>}
      <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
       <span class="hljs-operator">&lt;</span><span class="hljs-title">Card</span>
        <span class="hljs-title">name</span> <span class="hljs-operator">=</span> {<span class="hljs-title">secondData</span>.<span class="hljs-title">name</span>}
        <span class="hljs-title">pic</span> <span class="hljs-operator">=</span> {<span class="hljs-title">secondData</span>.<span class="hljs-title">profilePic</span>}
        <span class="hljs-title">title</span> <span class="hljs-operator">=</span> {<span class="hljs-title">secondData</span>.<span class="hljs-title">title</span>}
        <span class="hljs-title">email</span> <span class="hljs-operator">=</span> {<span class="hljs-title">secondData</span>.<span class="hljs-title">email</span>}
        <span class="hljs-title">about</span> <span class="hljs-operator">=</span> {<span class="hljs-title">secondData</span>.<span class="hljs-title">about</span>}
      <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span><span class="hljs-title">Card</span>
        <span class="hljs-title">name</span> <span class="hljs-operator">=</span> {<span class="hljs-title">thirdData</span>.<span class="hljs-title">name</span>}
        <span class="hljs-title">pic</span> <span class="hljs-operator">=</span> {<span class="hljs-title">thirdData</span>.<span class="hljs-title">profilePic</span>}
        <span class="hljs-title">title</span> <span class="hljs-operator">=</span> {<span class="hljs-title">thirdData</span>.<span class="hljs-title">title</span>}
        <span class="hljs-title">email</span> <span class="hljs-operator">=</span> {<span class="hljs-title">thirdData</span>.<span class="hljs-title">email</span>}
        <span class="hljs-title">about</span> <span class="hljs-operator">=</span> {<span class="hljs-title">thirdData</span>.<span class="hljs-title">about</span>}
      <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
  );
}

export default App;
</code></pre><p>Here we have a problem with our code. Our component is manually used and data are also passed manually. What if new data is added to data.js file? Will it automatically form a new Card component? No. We are manually accessing data from data.js file and passing them by calling Card manually. Our code isn't self-sustaining. To solve this issue map comes to the rescue. </p>
<h3 id="heading-mapping-over-data-to-create-the-component">Mapping over Data to create the component</h3>
<p>Now instead of manually passing data, we will now use the map to go through an array of data and pass the props into our component. Here is how we will do that.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Card</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./card'</span>
<span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">data</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./data.js'</span>

<span class="hljs-title"><span class="hljs-keyword">function</span></span> <span class="hljs-title">App</span>() {
  <span class="hljs-title">let</span> <span class="hljs-title">profileCard</span> <span class="hljs-operator">=</span> <span class="hljs-title">data</span>.<span class="hljs-title">map</span>((<span class="hljs-title">el</span>)<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>{
    <span class="hljs-title"><span class="hljs-keyword">return</span></span> <span class="hljs-operator">&lt;</span><span class="hljs-title">Card</span>
                <span class="hljs-title">name</span> <span class="hljs-operator">=</span> {<span class="hljs-title">el</span>.<span class="hljs-title">name</span>}
                <span class="hljs-title">pic</span> <span class="hljs-operator">=</span> {<span class="hljs-title">el</span>.<span class="hljs-title">profilePic</span>}
                <span class="hljs-title">title</span> <span class="hljs-operator">=</span> {<span class="hljs-title">el</span>.<span class="hljs-title">title</span>}
                <span class="hljs-title">email</span> <span class="hljs-operator">=</span> {<span class="hljs-title">el</span>.<span class="hljs-title">email</span>}
                <span class="hljs-title">about</span> <span class="hljs-operator">=</span> {<span class="hljs-title">el</span>.<span class="hljs-title">about</span>}
            <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
  })
  <span class="hljs-title"><span class="hljs-keyword">return</span></span> (
    <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"App"</span><span class="hljs-operator">&gt;</span>
      {<span class="hljs-title">profileCard</span>}
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
  );
}

export default App;
</code></pre><p>We have mapped over data and we know that data is an array of objects. So el is an object of a specific index starting from 0 to the length of the array and we access data in the format of objectName.keyname like we did on our code. Then we saved it in a profileCard variable which will now contain jsx element returned by the card. Then we used the profileCard variable in div with curly braces.</p>
<h3 id="heading-fix-warning-each-child-in-a-list-should-have-a-unique-key-prop">Fix warning: Each child in a list should have a unique "key" prop.</h3>
<p>If you open dev tools on your browser, you will see this error on your console. This means that you need to pass a key prop with a unique value each time you are passing props on a component. In our cases when we are passing props on Card component, we must pass additional props called keyswith unique values. So, how to pass unique values to the key? Well, there are certain ways but for now, we will just add an id field to data.js file with a unique value for data and pass it to the key.</p>
<p>Here is how our data.js file look now</p>
<pre><code><span class="hljs-keyword">const</span>  data = [
    {
        <span class="hljs-keyword">id</span>: <span class="hljs-number">1</span>,
        name: <span class="hljs-string">"Roshan K.C"</span>,
        title: <span class="hljs-string">"Software engineer | Cybersecurity enthusiast"</span>,
        profilePic: <span class="hljs-string">"https://avatars.githubusercontent.com/u/83297534?v=4"</span>,
        email: <span class="hljs-string">"admin@roshankc.info.np"</span>,
        about: <span class="hljs-string">"I am a computer science student from Pokhara Nepal who is interested in software engineering, Cybersecurity research, and blogging."</span>
    },
    {
        <span class="hljs-keyword">id</span>: <span class="hljs-number">2</span>,
        name: <span class="hljs-string">"Yagami light"</span>,
        title: <span class="hljs-string">"Software engineer"</span>,
        profilePic: <span class="hljs-string">"https://avatars.githubusercontent.com/u/83297534?v=4"</span>,
        email: <span class="hljs-string">"test1@gmail.com"</span>,
        about: <span class="hljs-string">"I am kira."</span>
    },
    {
        <span class="hljs-keyword">id</span>: <span class="hljs-number">3</span>,
        name: <span class="hljs-string">"James bond"</span>,
        title: <span class="hljs-string">"007"</span>,
        profilePic: <span class="hljs-string">"https://avatars.githubusercontent.com/u/83297534?v=4"</span>,
        email: <span class="hljs-string">"007@gmail.com"</span>,
        about: <span class="hljs-string">"Name is enough."</span>
    }
]
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> data
</code></pre><p>Here is how you pass a key prop.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Card</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./card'</span>
<span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">data</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./data.js'</span>

<span class="hljs-title"><span class="hljs-keyword">function</span></span> <span class="hljs-title">App</span>() {
  <span class="hljs-title">let</span> <span class="hljs-title">profileCard</span> <span class="hljs-operator">=</span> <span class="hljs-title">data</span>.<span class="hljs-title">map</span>((<span class="hljs-title">el</span>)<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>{
    <span class="hljs-title"><span class="hljs-keyword">return</span></span> <span class="hljs-operator">&lt;</span><span class="hljs-title">Card</span>
                <span class="hljs-title">key</span><span class="hljs-operator">=</span>{<span class="hljs-title">el</span>.<span class="hljs-title">id</span>}
                <span class="hljs-title">name</span> <span class="hljs-operator">=</span> {<span class="hljs-title">el</span>.<span class="hljs-title">name</span>}
                <span class="hljs-title">pic</span> <span class="hljs-operator">=</span> {<span class="hljs-title">el</span>.<span class="hljs-title">profilePic</span>}
                <span class="hljs-title">title</span> <span class="hljs-operator">=</span> {<span class="hljs-title">el</span>.<span class="hljs-title">title</span>}
                <span class="hljs-title">email</span> <span class="hljs-operator">=</span> {<span class="hljs-title">el</span>.<span class="hljs-title">email</span>}
                <span class="hljs-title">about</span> <span class="hljs-operator">=</span> {<span class="hljs-title">el</span>.<span class="hljs-title">about</span>}
            <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
  })
  <span class="hljs-title"><span class="hljs-keyword">return</span></span> (
    <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"App"</span><span class="hljs-operator">&gt;</span>
      {<span class="hljs-title">profileCard</span>}
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
  );
}

export default App;
</code></pre><p>Now the warning should be gone.</p>
<h3 id="heading-pass-object-as-props">Pass object as props</h3>
<p>Here we are passing our props individually. What if there are more props? It would be a hassle to pass all items individually. Why not pass the whole object as props? Yes, we can do that.</p>
<p>import './App.css';
import Card from './card'
import data from './data.js'</p>
<p>function App() {
  let profileCard = data.map((el)=&gt;{
    return 
  })
  return (
    <div>
      {profileCard}
    </div>
  );
}</p>
<p>export default App;</p>
<p>I have passed all el objects as data props. Now we need to go back to our card component and fix some issues. Now try to console.log(props) in Card.js file. We will see that props object has another object named data inside it whose value is the object we passed. So like we access and object inside the object in normal javascript, we do same here. Here is how our Card component looks now.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'./card.css'</span>

<span class="hljs-title">export</span> <span class="hljs-title">default</span> <span class="hljs-title"><span class="hljs-keyword">function</span></span> <span class="hljs-title">Card</span>(<span class="hljs-title">props</span>) {
    <span class="hljs-title">console</span>.<span class="hljs-title">log</span>(<span class="hljs-title">props</span>)
    <span class="hljs-title"><span class="hljs-keyword">return</span></span> (
        <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"card"</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">img</span> <span class="hljs-title">src</span><span class="hljs-operator">=</span>{<span class="hljs-title">props</span>.<span class="hljs-title">data</span>.<span class="hljs-title">profilePic</span>} <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"pp-img"</span> <span class="hljs-title">alt</span><span class="hljs-operator">=</span><span class="hljs-string">"profile"</span> <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"bio"</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">h2</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"name"</span><span class="hljs-operator">&gt;</span>{<span class="hljs-title">props</span>.<span class="hljs-title">data</span>.<span class="hljs-title">name</span>}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">p</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"title"</span><span class="hljs-operator">&gt;</span>{<span class="hljs-title">props</span>.<span class="hljs-title">data</span>.<span class="hljs-title">title</span>}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">a</span> <span class="hljs-title">href</span><span class="hljs-operator">=</span>{`<span class="hljs-title">mailto</span>:<span class="hljs-title">$</span>{<span class="hljs-title">props</span>.<span class="hljs-title">data</span>.<span class="hljs-title">email</span>}`} <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">'email'</span><span class="hljs-operator">&gt;</span><span class="hljs-title">Email</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">a</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">'about'</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span><span class="hljs-title">About</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>{<span class="hljs-title">props</span>.<span class="hljs-title">data</span>.<span class="hljs-title">about</span>}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
        <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
    )
}
</code></pre><h3 id="heading-spread-object-while-passing">Spread Object while passing</h3>
<p>Instead of a directly passing object, we can also use a spread operator to spread our data before passing.
This is how our App component looks now. We have spread the object el, which will pull out all values of the object and spread them out, which will be similar to before when we passed individually.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Card</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./card'</span>
<span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">data</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./data.js'</span>

<span class="hljs-title"><span class="hljs-keyword">function</span></span> <span class="hljs-title">App</span>() {
  <span class="hljs-title">let</span> <span class="hljs-title">profileCard</span> <span class="hljs-operator">=</span> <span class="hljs-title">data</span>.<span class="hljs-title">map</span>((<span class="hljs-title">el</span>)<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>{
    <span class="hljs-title"><span class="hljs-keyword">return</span></span> <span class="hljs-operator">&lt;</span><span class="hljs-title">Card</span>
                <span class="hljs-title">key</span><span class="hljs-operator">=</span>{<span class="hljs-title">el</span>.<span class="hljs-title">id</span>}
                {...<span class="hljs-title">el</span>}
            <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
  })
  <span class="hljs-title"><span class="hljs-keyword">return</span></span> (
    <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"App"</span><span class="hljs-operator">&gt;</span>
      {<span class="hljs-title">profileCard</span>}
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
  );
}

export default App;
</code></pre><p>Now the last thing is to fix our Card component because it is receiving the properties of objects directly. You can console.log(props) and see so that you know how to fix it. Here is how I have fixed it.</p>
<pre><code>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./card.css'</span>

<span class="hljs-title">export</span> <span class="hljs-title">default</span> <span class="hljs-title"><span class="hljs-keyword">function</span></span> <span class="hljs-title">Card</span>(<span class="hljs-title">props</span>) {
    <span class="hljs-title">console</span>.<span class="hljs-title">log</span>(<span class="hljs-title">props</span>)
    <span class="hljs-title"><span class="hljs-keyword">return</span></span> (
        <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"card"</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">img</span> <span class="hljs-title">src</span><span class="hljs-operator">=</span>{<span class="hljs-title">props</span>.<span class="hljs-title">profilePic</span>} <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"pp-img"</span> <span class="hljs-title">alt</span><span class="hljs-operator">=</span><span class="hljs-string">"profile"</span> <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"bio"</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">h2</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"name"</span><span class="hljs-operator">&gt;</span>{<span class="hljs-title">props</span>.<span class="hljs-title">name</span>}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">p</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"title"</span><span class="hljs-operator">&gt;</span>{<span class="hljs-title">props</span>.<span class="hljs-title">title</span>}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">a</span> <span class="hljs-title">href</span><span class="hljs-operator">=</span>{`<span class="hljs-title">mailto</span>:<span class="hljs-title">$</span>{<span class="hljs-title">props</span>.<span class="hljs-title">email</span>}`} <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">'email'</span><span class="hljs-operator">&gt;</span><span class="hljs-title">Email</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">a</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">'about'</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span><span class="hljs-title">About</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>{<span class="hljs-title">props</span>.<span class="hljs-title">about</span>}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
        <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
    )
}
</code></pre><h3 id="heading-what-we-learned">What we learned</h3>
<p>We have learned how to map over data to create self-sustaining code. We learned how to pass objects as props and use of spread operator to make our life easier.</p>
]]></content:encoded></item><item><title><![CDATA[Reusable components with Props in Reactjs | Props passing - Day2]]></title><description><![CDATA[In our previous article, we learned how to create custom components. It made our codes more composable. In this article, we will learn how to make a component reusable. If you don't know about jsx, and custom components then check my previous article...]]></description><link>https://roshankc.info.np/reusable-components-with-props-in-reactjs</link><guid isPermaLink="true">https://roshankc.info.np/reusable-components-with-props-in-reactjs</guid><category><![CDATA[Props in reactjs]]></category><category><![CDATA[passing props in react]]></category><category><![CDATA[receive props]]></category><dc:creator><![CDATA[Roshan kc]]></dc:creator><pubDate>Thu, 08 Sep 2022 11:47:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1662637129840/tOWgzLMqc.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In our previous article, we learned how to create custom components. It made our codes more composable. In this article, we will learn how to make a component reusable. If you don't know about jsx, and custom components then check my <a target="_blank" href="https://roshankc.info.np/installation-jsx-and-custom-components">previous article</a>.</p>
<h3 id="heading-custom-component-for-card">Custom Component for Card</h3>
<p>Let us start with an example. Suppose you have a card component. To create that you can create a new file called card.js and export the following code from it just like you did to create the navbar component on our previous blog. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1662637488103/smmA9CP7i.jpg" alt="cardcompo.jpg" /></p>
<p><strong>Card.js
</strong></p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'./card.css'</span>

<span class="hljs-title">export</span> <span class="hljs-title">default</span> <span class="hljs-title"><span class="hljs-keyword">function</span></span> <span class="hljs-title">Card</span>() {
    <span class="hljs-title"><span class="hljs-keyword">return</span></span> (
        <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"card"</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">img</span> <span class="hljs-title">src</span><span class="hljs-operator">=</span><span class="hljs-string">"https://avatars.githubusercontent.com/u/83297534?v=4"</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"pp-img"</span> <span class="hljs-title">alt</span><span class="hljs-operator">=</span><span class="hljs-string">"profile"</span> <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"bio"</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">h2</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"name"</span><span class="hljs-operator">&gt;</span><span class="hljs-title">Roshan</span> <span class="hljs-title">K</span>.<span class="hljs-title">C</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">p</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"title"</span><span class="hljs-operator">&gt;</span><span class="hljs-title">Software</span> <span class="hljs-title">engineer</span> <span class="hljs-operator">|</span> <span class="hljs-title">Cybersecurity</span> <span class="hljs-title">enthusiast</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">a</span> <span class="hljs-title">href</span><span class="hljs-operator">=</span><span class="hljs-string">"mailto:admin@roshankc.info.np"</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">'email'</span><span class="hljs-operator">&gt;</span><span class="hljs-title">Email</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">a</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">'about'</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span><span class="hljs-title">About</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span><span class="hljs-title">I</span> <span class="hljs-title">am</span> <span class="hljs-title">a</span> <span class="hljs-title">computer</span> <span class="hljs-title">science</span> <span class="hljs-title">student</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-title">Pokhara</span> <span class="hljs-title">Nepal</span> <span class="hljs-title">who</span> <span class="hljs-title"><span class="hljs-keyword">is</span></span> <span class="hljs-title">interested</span> <span class="hljs-title">in</span> <span class="hljs-title">software</span> <span class="hljs-title">engineering</span>, <span class="hljs-title">Cybersecurity</span> <span class="hljs-title">research</span>, <span class="hljs-title">and</span> <span class="hljs-title">blogging</span>.<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
        <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
    )
}
</code></pre><p>Here in this code, we have imported CSS from a new file called card.css. You can create a file called card.css which will contain normal CSS code to style jsx returned from card.js.</p>
<pre><code><span class="hljs-selector-class">.card</span>{
    <span class="hljs-attribute">width</span>: <span class="hljs-number">250px</span>;
    <span class="hljs-attribute">height</span>: auto;
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">flex-direction</span>: column;
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">10px</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#1A1B21</span>;
    <span class="hljs-attribute">overflow</span>: hidden;
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#F5F5F5</span>;
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">1em</span>;
}
<span class="hljs-selector-class">.pp-img</span>{
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;

}
<span class="hljs-selector-class">.bio</span>{
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">flex-direction</span>: column;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">1em</span>;
    <span class="hljs-attribute">text-align</span>: center;
}
<span class="hljs-selector-class">.name</span>{
    <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">700</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.5rem</span>;
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFFFFF</span>;
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
}
<span class="hljs-selector-class">.title</span>{
    <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">400</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">0.9rem</span>;
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#e1e0d9</span>;
    <span class="hljs-attribute">letter-spacing</span>: <span class="hljs-number">0.4px</span>;
    <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">0</span>;
}
<span class="hljs-selector-class">.about</span>{
    <span class="hljs-attribute">padding</span>:<span class="hljs-number">0</span> <span class="hljs-number">1em</span> <span class="hljs-number">1em</span> <span class="hljs-number">1em</span>; 
    <span class="hljs-attribute">text-align</span>: left;
}
<span class="hljs-selector-class">.about</span> <span class="hljs-selector-tag">h2</span>{
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;

}
<span class="hljs-selector-class">.about</span> <span class="hljs-selector-tag">p</span>{
    <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">400</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">0.9rem</span>;
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#DCDCDC</span>;
}
<span class="hljs-selector-class">.email</span>{
    <span class="hljs-attribute">text-decoration</span>: none;
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#374151</span>;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">0.5em</span> <span class="hljs-number">0.7em</span> <span class="hljs-number">0.5em</span> <span class="hljs-number">0.6em</span>;
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0.4em</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#FFFFFF</span>;
    <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#D1D5DB</span>;
    <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0px</span> <span class="hljs-number">1px</span> <span class="hljs-number">2px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.05</span>);
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">6px</span>;
}
<span class="hljs-selector-class">.email</span><span class="hljs-selector-pseudo">:hover</span>{
    <span class="hljs-attribute">filter</span>: <span class="hljs-built_in">brightness</span>(<span class="hljs-number">0.8</span>);
}
</code></pre><p>Now you can import the Card component on the App.js file. Remember how you call a custom component function to get returned value where we need it. We call custom components like we call a normal function. In our case, if we need to call the card function which is returning jsx( Html like code for card component) then we say card(). But inside jsx we call it by saying . Below is an example of using it inside App.js component.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Card</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./card'</span>
<span class="hljs-title"><span class="hljs-keyword">function</span></span> <span class="hljs-title">App</span>() {
  <span class="hljs-title"><span class="hljs-keyword">return</span></span> (
    <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"App"</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span><span class="hljs-title">Card</span><span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
  );
}

export default App;
</code></pre><p>Here the jsx returned from the Card component will be placed inside a div with the class name called App which will eventually get utilized to render through index.js file where the App component is called to render. This is how we utilize react to create a custom component that we can use anywhere just by importing and calling it in the place where we need it.</p>
<h3 id="heading-problem-with-our-card-component">Problem with our Card component</h3>
<p>But we have an issue with this method. We aren't making our component dynamic. Whenever we call it, it will return a component with static values. We might need a card for another person with different values. Our card component is not data-driven which means for every new type of card we need to again create a new card component. This means the component isn't generated as per data but by manually putting data inside it.</p>
<p>Let's take an example of the amazon products page. Here you can see every card contains details of the different products but the layout of the cards are same. Which means they are the same component with different data. But when a new product is added, a new card with details of the new product is automatically formed. Does that mean there is a person who updates the product card component with all the new data and makes a new card? Of course not. The card is updated with new data and generated accordingly when we add a new product.  </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1662634273314/zwed9TxJw.jpg" alt="reusable.jpg" />
But in our profile card, there is a need for a person who will update the card values as per new data. How can we make our component dynamic so that we can make it return with data as per our need? This is where props come into saving.</p>
<h3 id="heading-props-in-reactjs">Props in Reactjs</h3>
<p>Let's understand the concept of props and u can define yourself what are props. Let's start with this example. Here you have a function named squareNum to return the square of a number.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">squareNum</span>(<span class="hljs-params"></span>)
</span>{
    <span class="hljs-keyword">return</span> a<span class="hljs-operator">*</span>a
}
</code></pre><p>Now you can see every time you call this function, you will only get the square value of a. This function isn't returning dynamic values. So, how do you make this function return different values as per your data? Yes by setting parameters and passing your data through arguments. Now see the function given below.</p>
<pre><code>  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">squareNum</span>(<span class="hljs-params">num</span>)
</span>{
    <span class="hljs-keyword">return</span> num<span class="hljs-operator">*</span>num
}

squareNum(<span class="hljs-number">2</span>)
squareNum(<span class="hljs-number">5</span>)
</code></pre><p>Now you can see your function will return values as per the data you pass. Setting a parameter called num made your function reusable and dynamic. This is what the concept of props is. It is just a value you pass in your function/component and which value you will pass while calling/using your component. And the passing of value you do while calling method is passing props.</p>
<h3 id="heading-how-to-pass-props">How to pass Props</h3>
<p>As you know in real-world apps, data are fetched from api/db. In our case, we will create a file called data.js inside src directory where we will upload all our data in form of objects. Here is an example of data.js file from where we have exported an array of objects containing card component data.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1662637555472/2hiMpAjE0.jpg" alt="datajs.jpg" /></p>
<pre><code>const  data = [
    {
        <span class="hljs-type">name</span>: "Roshan K.C",
        title: "Software engineer | Cybersecurity enthusiast",
        profilePic: "https://avatars.githubusercontent.com/u/83297534?v=4",
        email: "admin@roshankc.info.np",
        about: "I am a computer science student from Pokhara Nepal who is interested in software engineering, Cybersecurity research, and blogging."
    },
    {
        <span class="hljs-type">name</span>: "Yagami light",
        title: "Software engineer",
        profilePic: "https://avatars.githubusercontent.com/u/83297534?v=4",
        email: "test1@gmail.com",
        about: "I am kira."
    },
    {
        <span class="hljs-type">name</span>: "James bond",
        title: "007",
        profilePic: "https://avatars.githubusercontent.com/u/83297534?v=4",
        email: "007@gmail.com",
        about: "Name is enough."
    }
]
export <span class="hljs-keyword">default</span> data
</code></pre><p>Let's see how we add a variable inside jsx.</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Title</span>(<span class="hljs-params"></span>)
</span>{
    let name <span class="hljs-operator">=</span> <span class="hljs-string">"Roshan K.C"</span>
    <span class="hljs-keyword">return</span> <span class="hljs-operator">&lt;</span>h1<span class="hljs-operator">&gt;</span>{name}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>h1<span class="hljs-operator">&gt;</span>
}
</code></pre><p>So you basically put them inside curly braces.</p>
<p>Now import the data on Card.js file. Then when you call the Card component you can pass props in form of the following format in App.js file.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Card</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./card'</span>
<span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">data</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./data.js'</span>

<span class="hljs-title"><span class="hljs-keyword">function</span></span> <span class="hljs-title">App</span>() {
  <span class="hljs-title">let</span> <span class="hljs-title">firstData</span> <span class="hljs-operator">=</span> <span class="hljs-title">data</span>[0]
  <span class="hljs-title">let</span> <span class="hljs-title">secondData</span> <span class="hljs-operator">=</span> <span class="hljs-title">data</span>[1]
  <span class="hljs-title">let</span> <span class="hljs-title">thirdData</span> <span class="hljs-operator">=</span> <span class="hljs-title">data</span>[2]
  <span class="hljs-title"><span class="hljs-keyword">return</span></span> (
    <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"App"</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span><span class="hljs-title">Card</span>
        <span class="hljs-title">name</span> <span class="hljs-operator">=</span> {<span class="hljs-title">firstData</span>.<span class="hljs-title">name</span>}
        <span class="hljs-title">pic</span> <span class="hljs-operator">=</span> {<span class="hljs-title">firstData</span>.<span class="hljs-title">profilePic</span>}
        <span class="hljs-title">title</span> <span class="hljs-operator">=</span> {<span class="hljs-title">firstData</span>.<span class="hljs-title">title</span>}
        <span class="hljs-title">email</span> <span class="hljs-operator">=</span> {<span class="hljs-title">firstData</span>.<span class="hljs-title">email</span>}
        <span class="hljs-title">about</span> <span class="hljs-operator">=</span> {<span class="hljs-title">firstData</span>.<span class="hljs-title">about</span>}
      <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
       <span class="hljs-operator">&lt;</span><span class="hljs-title">Card</span>
        <span class="hljs-title">name</span> <span class="hljs-operator">=</span> {<span class="hljs-title">secondData</span>.<span class="hljs-title">name</span>}
        <span class="hljs-title">pic</span> <span class="hljs-operator">=</span> {<span class="hljs-title">secondData</span>.<span class="hljs-title">profilePic</span>}
        <span class="hljs-title">title</span> <span class="hljs-operator">=</span> {<span class="hljs-title">secondData</span>.<span class="hljs-title">title</span>}
        <span class="hljs-title">email</span> <span class="hljs-operator">=</span> {<span class="hljs-title">secondData</span>.<span class="hljs-title">email</span>}
        <span class="hljs-title">about</span> <span class="hljs-operator">=</span> {<span class="hljs-title">secondData</span>.<span class="hljs-title">about</span>}
      <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-operator">&lt;</span><span class="hljs-title">Card</span>
        <span class="hljs-title">name</span> <span class="hljs-operator">=</span> {<span class="hljs-title">thirdData</span>.<span class="hljs-title">name</span>}
        <span class="hljs-title">pic</span> <span class="hljs-operator">=</span> {<span class="hljs-title">thirdData</span>.<span class="hljs-title">profilePic</span>}
        <span class="hljs-title">title</span> <span class="hljs-operator">=</span> {<span class="hljs-title">thirdData</span>.<span class="hljs-title">title</span>}
        <span class="hljs-title">email</span> <span class="hljs-operator">=</span> {<span class="hljs-title">thirdData</span>.<span class="hljs-title">email</span>}
        <span class="hljs-title">about</span> <span class="hljs-operator">=</span> {<span class="hljs-title">thirdData</span>.<span class="hljs-title">about</span>}
      <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
  );
}

export default App;
</code></pre><p>As you can see I have stored the objects at the different indexes of the array(data) in 3 different variables (firstData, secondData, thirdData). Then while calling , I passed the props in this format name = {firstData.profilePic}. Here, name will be now passed just like an argument to the function Card() in card.js file.</p>
<p>{firstData.profilePic} is just a way to get value from the object and as you can see it is an expression so we put it inside curly braces to use its value in jsx. So like that you pass each data with a different name as I did. 
I have called  three different times and passed different values, so this means we will get card component called three times and we will have three html for card component.</p>
<h3 id="heading-receiving-props-in-reactjs">Receiving props in Reactjs</h3>
<p>Now just like passing argument, props are also passed into their main function. Here our function is Card(). So to receive props, we need to set a parameter in function. Which we will set as props. So this parameter named props will receive all the values we pass as an object which you can verify by doing console.log(props) inside the function.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'./card.css'</span>

<span class="hljs-title">export</span> <span class="hljs-title">default</span> <span class="hljs-title"><span class="hljs-keyword">function</span></span> <span class="hljs-title">Card</span>(<span class="hljs-title">props</span>) {
    <span class="hljs-title">console</span>.<span class="hljs-title">log</span>(<span class="hljs-title">props</span>)
    <span class="hljs-title"><span class="hljs-keyword">return</span></span> (
        <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"card"</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">img</span> <span class="hljs-title">src</span><span class="hljs-operator">=</span><span class="hljs-string">"https://avatars.githubusercontent.com/u/83297534?v=4"</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"pp-img"</span> <span class="hljs-title">alt</span><span class="hljs-operator">=</span><span class="hljs-string">"profile"</span> <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"bio"</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">h2</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"name"</span><span class="hljs-operator">&gt;</span><span class="hljs-title">Roshan</span> <span class="hljs-title">K</span>.<span class="hljs-title">C</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">p</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"title"</span><span class="hljs-operator">&gt;</span><span class="hljs-title">Software</span> <span class="hljs-title">engineer</span> <span class="hljs-operator">|</span> <span class="hljs-title">Cybersecurity</span> <span class="hljs-title">enthusiast</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">a</span> <span class="hljs-title">href</span><span class="hljs-operator">=</span><span class="hljs-string">"mailto:admin@roshankc.info.np"</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">'email'</span><span class="hljs-operator">&gt;</span><span class="hljs-title">Email</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">a</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">'about'</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span><span class="hljs-title">About</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span><span class="hljs-title">I</span> <span class="hljs-title">am</span> <span class="hljs-title">a</span> <span class="hljs-title">computer</span> <span class="hljs-title">science</span> <span class="hljs-title">student</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-title">Pokhara</span> <span class="hljs-title">Nepal</span> <span class="hljs-title">who</span> <span class="hljs-title"><span class="hljs-keyword">is</span></span> <span class="hljs-title">interested</span> <span class="hljs-title">in</span> <span class="hljs-title">software</span> <span class="hljs-title">engineering</span>, <span class="hljs-title">Cybersecurity</span> <span class="hljs-title">research</span>, <span class="hljs-title">and</span> <span class="hljs-title">blogging</span>.<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
        <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
    )
}
</code></pre><p>You will get 3 objects logged on console with keyname as the props key you set and value as the value you had set. You get 3 object because card component is called three times on App component. Now how to use the value we received as props in our html? Remember, the props parameter is just receiving values as an object. So, how do we extract values from an object? Obviously with the help of key. Here is an example of using props.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'./card.css'</span>

<span class="hljs-title">export</span> <span class="hljs-title">default</span> <span class="hljs-title"><span class="hljs-keyword">function</span></span> <span class="hljs-title">Card</span>(<span class="hljs-title">props</span>) {
    <span class="hljs-title">console</span>.<span class="hljs-title">log</span>(<span class="hljs-title">props</span>)
    <span class="hljs-title"><span class="hljs-keyword">return</span></span> (
        <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"card"</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">img</span> <span class="hljs-title">src</span><span class="hljs-operator">=</span>{<span class="hljs-title">props</span>.<span class="hljs-title">pic</span>} <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"pp-img"</span> <span class="hljs-title">alt</span><span class="hljs-operator">=</span><span class="hljs-string">"profile"</span> <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"bio"</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">h2</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"name"</span><span class="hljs-operator">&gt;</span>{<span class="hljs-title">props</span>.<span class="hljs-title">name</span>}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">p</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">"title"</span><span class="hljs-operator">&gt;</span>{<span class="hljs-title">props</span>.<span class="hljs-title">title</span>}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span><span class="hljs-title">a</span> <span class="hljs-title">href</span><span class="hljs-operator">=</span>{`<span class="hljs-title">mailto</span>:<span class="hljs-title">$</span>{<span class="hljs-title">props</span>.<span class="hljs-title">email</span>}`} <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">'email'</span><span class="hljs-operator">&gt;</span><span class="hljs-title">Email</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">a</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">div</span> <span class="hljs-title">className</span><span class="hljs-operator">=</span><span class="hljs-string">'about'</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span><span class="hljs-title">About</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">h2</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>{<span class="hljs-title">props</span>.<span class="hljs-title">about</span>}<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">p</span><span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
        <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-title">div</span><span class="hljs-operator">&gt;</span>
    )
}
</code></pre><p>Here you just need to wrap your javascript expression inside curly braces to use in jsx(Jsx is just a javascript that looks like html). Now your card component will return value as per the data your pass in your props. This makes our code more reusable.</p>
<h3 id="heading-important-note">Important Note</h3>
<p>You must always remember that you can only pass props on to the custom components. You cannot pass props as "&lt;h1 name="Roshan" " on a html component. It is because h1 tag doesn't have any attribute known as a name. You should always remember that your custom component will at last get converted into html component that dom understands. Passing custom attributes on already existing tags like h1 will break your app and cause errors.</p>
<h3 id="heading-to-summarize">To summarize</h3>
<p>Props are just values that are passed through custom components. They are given specific names and values. Props are received through a parameter set in the component it is passed at. The value received is an object which is in form of {keyName: value}. We can use this as a normal object with curly braces inside jsx.</p>
<h3 id="heading-what-we-learned">What we learned</h3>
<ul>
<li>What are props</li>
<li>Passing props to custom components</li>
<li>Receiving and using props</li>
<li>Things to consider while passing props.</li>
</ul>
<p>Tomorrow we will learn how to use maps to dynamically render cards as data gets updated, various ways to receive and pass props and have a sneak peek at conditional rendering.</p>
]]></content:encoded></item><item><title><![CDATA[Introduction to React.js | Installation, JSX, and Custom Components - Day 1]]></title><description><![CDATA[Reactjs is a javascript library to build front-end user interfaces. It follows the declarative approach to writing code. You can manage the state in your application and react will render a specific component when the data changes. 
Reactjs uses JSX ...]]></description><link>https://roshankc.info.np/installation-jsx-and-custom-components</link><guid isPermaLink="true">https://roshankc.info.np/installation-jsx-and-custom-components</guid><category><![CDATA[React]]></category><category><![CDATA[JSX]]></category><category><![CDATA[how to install react js]]></category><category><![CDATA[create starter file for reactjs]]></category><category><![CDATA[custom components reactjs]]></category><dc:creator><![CDATA[Roshan kc]]></dc:creator><pubDate>Mon, 05 Sep 2022 15:24:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1662391700642/Q0F2y8tBy.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Reactjs is a javascript library to build front-end user interfaces. It follows the declarative approach to writing code. You can manage the state in your application and react will render a specific component when the data changes. </p>
<p>Reactjs uses JSX that helps to write HTML in react. JSX stands for JavaScript XML. JSX makes it easier to write and add HTML in React.</p>
<h3 id="heading-prerequisites-for-learning-reactjs">Prerequisites for Learning React.js</h3>
<ol>
<li>HTML</li>
<li>CSS</li>
<li>Javascript-ES6</li>
<li>Node js in your system. <a target="_blank" href="https://nodejs.org/en/">Click here</a> to download.</li>
<li>Code editor(VS code preferred)</li>
</ol>
<p>Intermediate-level experience with HTML, CSS, and javascript is required if you want to learn Reactjs. You must have web dev experience with vanilla CSS and js if you want to start learning Reactjs.</p>
<h3 id="heading-installation-of-starter-code-for-react">Installation of starter code for React</h3>
<p>If you have node/npm in your system then you can open your preferred code editor and follow the steps given below to install react.</p>
<ul>
<li>Create a folder with the name of your choice and open it in the code editor. I am using VS code.</li>
<li><p>Now open a terminal in vs code. You can use the (control + ~) shortcut on windows and (command+~) on mac. The tilde sign is situated on side of the number 1 on your keyboard. Then type the following command.</p>
</li>
<li><p>If you want to create react starter code on a new folder inside the folder you then type the following command</p>
</li>
</ul>
<pre><code>npx create<span class="hljs-operator">-</span>react<span class="hljs-operator">-</span>app your<span class="hljs-operator">-</span>folder<span class="hljs-operator">-</span>name
</code></pre><ul>
<li>If you want to install react on the folder you are in then type the following command.</li>
</ul>
<pre><code>npx create<span class="hljs-operator">-</span>react<span class="hljs-operator">-</span>app .
</code></pre><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1662385552364/Ze3zJGaPp.jpg" alt="install1.jpg" /></p>
<h3 id="heading-directories-and-files">Directories and files.</h3>
<p>Now after installation we will have some directories created on our code editor. Inside the src directory, we will see some files. This is where we will write our react code for our website. Meanwhile, the Public directory contains our images and HTML files. We edit the HTML as per our choice and there we will see div with id="root" on the body of the index.html file.</p>
<pre><code>  <span class="hljs-operator">&lt;</span>body<span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span>noscript<span class="hljs-operator">&gt;</span>You need to enable JavaScript to run <span class="hljs-built_in">this</span> app.&lt;<span class="hljs-operator">/</span>noscript<span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span>div id<span class="hljs-operator">=</span><span class="hljs-string">"root"</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
  <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>body<span class="hljs-operator">&gt;</span>
</code></pre><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1662390233971/dR2gXoFJB.jpg" alt="this.jpg" /></p>
<h3 id="heading-custom-component-and-reactjs-code-example">Custom Component and Reactjs code example</h3>
<p>Inside the src directory, we will see a bunch of files. Among them, App.js and index.js are important for us as of now. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1662390499956/r0sGpSE4f.jpg" alt="app file.jpg" /></p>
<p>App.js will contain a function called App which is exported from there. The function is returning HTML code. HTML is written inside the App function. But it isn't completely HTML but what we called jsx. You can see a change in how you write class attributes on normal HTML but here you write className in camelCase format just like in javascript. </p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="hljs-operator">&lt;</span>div className<span class="hljs-operator">=</span><span class="hljs-string">"App"</span><span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span>h1<span class="hljs-operator">&gt;</span>I am App file that get rendered through index.js&lt;<span class="hljs-operator">/</span>h1<span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
  );
}

export default App;
</code></pre><p>We can write HTML inside functions and return them. In fact, what is returned from the function App are just javascript objects which are imported on the index.js file. You can console.log value returned from the App function to see what we get too. </p>
<pre><code>console.log(App())
</code></pre><p>We need to import React from 'react' in our file because React (which you just imported from 'react') is what defines JSX.</p>
<p>Here in index.js file, we see a root variable that is just using createRoot method from ReactDOM to create the root. We are selecting the div with an id called root from index.html which we had seen inside public/index.html directory. Then we are passing that div with id root to createRoot element where we will render all our reactjs codes and components. So the div with id called root on index.html file will have all our components of react inside it because we just declared it as root by passing it on ReactDOM.createRoot () method. </p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-title">React</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">ReactDOM</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'react-dom/client'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">App</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./App'</span>;

const root <span class="hljs-operator">=</span> ReactDOM.createRoot(document.getElementById(<span class="hljs-string">'root'</span>));
root.render(
  <span class="hljs-operator">&lt;</span>React.StrictMode&gt;
    <span class="hljs-operator">&lt;</span>App <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
  <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>React.StrictMode&gt;
);
</code></pre><p>You can see the root.render() method which is rendering a component called  which is non other than the App function which we imported from App.js file. This is the syntax for the custom component. You write them just like a HTML component with closing syntax. In fact, what we call custom component are nonother than javascript functions like App() that returns jsx. We just call the function in the format of  to get object returned by them. render() function available on root variable is what converts the javascript to HTML code understood by the browser.</p>
<h3 id="heading-composable-due-to-custom-components">Composable due to custom components</h3>
<p>Custom components are part of our application which are represented as components. They are just function that returns a component of the website. For example if you want to create a navbar component for your website, then you can create a file called navbar.js in src directory. Then to create a custom component called navbar you need to import React from 'react'. After that you can create a function called Navbar and write your HTML inside it. Then you can export it.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1662390559640/3RtB6VdlI.jpg" alt="navbar.jpg" /></p>
<pre><code>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params"></span>)
</span>{
    <span class="hljs-keyword">return</span> (
        <span class="hljs-operator">&lt;</span>nav<span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span>ul<span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span>li<span class="hljs-operator">&gt;</span>Home<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>li<span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span>li<span class="hljs-operator">&gt;</span>About<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>li<span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span>li<span class="hljs-operator">&gt;</span>Contact<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>li<span class="hljs-operator">&gt;</span>
                <span class="hljs-operator">&lt;</span>li<span class="hljs-operator">&gt;</span>Skils<span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>li<span class="hljs-operator">&gt;</span>
            <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>ul<span class="hljs-operator">&gt;</span>
        <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>nav<span class="hljs-operator">&gt;</span>
    )
}

export default Navbar
</code></pre><p>After that, you can import the function called Navbar on index.js file. We always capitalize first letter for our component. Now you can call the function Navbar inside root.render method below  just like you called. This navbar is now a custom component that we can give any name and call on any place where we need it.  </p>
<p>After that try to run your react app by typing the following command on the terminal.</p>
<pre><code>npm <span class="hljs-keyword">start</span>
</code></pre><p>Now you will face some errors. The reason for that error is you cannot render two components directly under the root component. You need to house them inside a parent div. Here is kept  and  component inside a parent div to render them. We render jsx in this format.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-title">React</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">ReactDOM</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'react-dom/client'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">App</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./App'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Navbar</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./navbar'</span>
<span class="hljs-title">const</span> <span class="hljs-title">root</span> <span class="hljs-operator">=</span> <span class="hljs-title">ReactDOM</span>.<span class="hljs-title">createRoot</span>(<span class="hljs-title">document</span>.<span class="hljs-title">getElementById</span>(<span class="hljs-string">'root'</span>));
root.render(
  <span class="hljs-operator">&lt;</span>React.StrictMode&gt;
    <span class="hljs-operator">&lt;</span>div<span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span>App <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span>Navbar <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>div<span class="hljs-operator">&gt;</span>
  <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>React.StrictMode&gt;
);
</code></pre><p>Or you can use what we call jsx fragment i.e &lt;&gt;&lt;/&gt;.</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-title">React</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">ReactDOM</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'react-dom/client'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">App</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./App'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title">Navbar</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">'./navbar'</span>
<span class="hljs-title">const</span> <span class="hljs-title">root</span> <span class="hljs-operator">=</span> <span class="hljs-title">ReactDOM</span>.<span class="hljs-title">createRoot</span>(<span class="hljs-title">document</span>.<span class="hljs-title">getElementById</span>(<span class="hljs-string">'root'</span>));
root.render(
  <span class="hljs-operator">&lt;</span>React.StrictMode&gt;
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span>App <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span>Navbar <span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
    <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span>
  <span class="hljs-operator">&lt;</span><span class="hljs-operator">/</span>React.StrictMode&gt;
);
</code></pre><h3 id="heading-to-summarize">To Summarize</h3>
<p>So we just create a component which is just a javascript function that returns jsx codes. JSX is just an extension to js syntax that helps to structure a component on react like our App and Navbar. They look familiar to HTML but are just javascript objects. So when you log output while calling a function that returns jsx, you will get object as output. You then import components from files you exported, in our case imported at index.js.</p>
<p>Then you select where you want to render the components. We select a div with id root and passed it to createRoot method to make it as a parent div which will hold our all components then save it as root. Inside root.render method you put which component you want to render in that div called root. In our case, we rendered App and Navbar inside that div. We can only render one component directly so, to render more than one component we need to put them inside a parent div which we did to solve the issue.</p>
<h3 id="heading-what-did-we-learn">What did we learn?</h3>
<ol>
<li>What is react and how do we create react projects?</li>
<li>Directories and files we get by creating react project</li>
<li>Basic syntax of react and custom components.</li>
<li>Creating and using custom components.</li>
</ol>
<p>I hope this article was helpful for you. Now you can practice creating custom components for simple components of the website like the navbar, body, footer, cards, etc, export and import them and use them on index.js file as I did. You can try this by converting a page made with HTML CSS to reactjs. Share with your friends and bye-bye till the second part where we will learn about props.</p>
]]></content:encoded></item><item><title><![CDATA[Introduction to Nodejs | 100 seconds paper]]></title><description><![CDATA[What is Nodejs?
Node js, an asynchronous event-driven javascript runtime designed for building scalable network applications. Designed by Ryan Dahl and released in 2009, Nodejs is a javascript runtime built on google's open-source v8 js engine second...]]></description><link>https://roshankc.info.np/introduction-to-nodejs-100-seconds-paper</link><guid isPermaLink="true">https://roshankc.info.np/introduction-to-nodejs-100-seconds-paper</guid><category><![CDATA[Node.js]]></category><category><![CDATA[APIs]]></category><category><![CDATA[server]]></category><dc:creator><![CDATA[Roshan kc]]></dc:creator><pubDate>Fri, 22 Jul 2022 12:16:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1658492096167/U2FXvU1KL.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-nodejs">What is Nodejs?</h2>
<p>Node js, an asynchronous event-driven javascript runtime designed for building scalable network applications. Designed by Ryan Dahl and released in 2009, Nodejs is a javascript runtime built on google's open-source v8 js engine second line.</p>
<p>Js is run in a browser so the browser is js runtime, Nodejs enables the environment running js outside the browser on top of a v8 engine, like a container so it is js runtime.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1658491958912/HaDBBOImP.png" alt="nodejs.png" /></p>
<h3 id="heading-uses-of-nodejs">Uses of Nodejs</h3>
<p>Nodejs is used for building fast and scalable data-intensive applications. Server-side apps, API with DB, data streaming, server-side web apps, and real-time chat apps all can be made with node js.</p>
<h3 id="heading-architecture-of-nodejs">Architecture of Nodejs</h3>
<p>Following single thread architecture, node js follows a non-blocking event-driven coding pattern to ensure the process run for every use without blocking.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1658491946782/FwZfPL13Z.png" alt="thread.png" /></p>
<h3 id="heading-simple-server-in-nodejs">Simple server in Nodejs</h3>
<p>Example:
Import HTTP module. Now use the createServer method on it and send a response from the callback. The callback function has a request and response parameter. Then save on a variable and listen using that variable on desired port and IP.</p>
<pre><code>const http <span class="hljs-operator">=</span> <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>)
const server <span class="hljs-operator">=</span> http.createServer((req,res)<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>{
    res.end(<span class="hljs-string">"Hell from server of roshan"</span>)
})
server.listen(<span class="hljs-number">8000</span>, <span class="hljs-string">'127.0.0.1'</span>,()<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>{
    console.log(<span class="hljs-string">"listening on port 8000"</span>)
})
</code></pre><h3 id="heading-routing-in-nodejs">Routing in Nodejs</h3>
<p>Example of routing on nodejs:
Import HTTP module and save it on a variable, then use createServer method on  HTTP with callback function with req and res as a variable, save URL from request in a variable
use if else to send the response as per pathname you can write a custom header as per the status code then listen to your desired port and IP.</p>
<pre><code>const http <span class="hljs-operator">=</span> <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>)
const server <span class="hljs-operator">=</span> http.createServer((req,res)<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>{
    const pathName <span class="hljs-operator">=</span> req.url
    <span class="hljs-keyword">if</span>(pathName <span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-string">"/"</span> <span class="hljs-operator">|</span><span class="hljs-operator">|</span> pathName <span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-string">"/overview"</span>)
    {
        res.end(<span class="hljs-string">"Hell from server overview"</span>)
    }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(pathName <span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-string">"/products"</span>)
    {
        res.writeHead(<span class="hljs-number">200</span>, {
            <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html'</span>
        })
        res.end(<span class="hljs-string">"&lt;h1&gt;hello from product page&lt;/h1&gt;"</span>)
    }
    <span class="hljs-keyword">else</span>{
        res.writeHead(<span class="hljs-number">404</span>, {
            <span class="hljs-string">'content-type'</span>: <span class="hljs-string">"text/html"</span>,
            <span class="hljs-string">'my-own'</span>:<span class="hljs-string">'hehe'</span> 
        })
        res.end(<span class="hljs-string">"&lt;h1&gt;404 error: Page not found&lt;/h1&gt;"</span>)
    }
})
server.listen(<span class="hljs-number">8000</span>, <span class="hljs-string">'127.0.0.1'</span>,()<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>{
    console.log(<span class="hljs-string">"listening on port 8000"</span>)
})
</code></pre><h3 id="heading-simple-api-in-nodejs">Simple API in Nodejs</h3>
<p>To import content from a JSON file we need to import the fs module, then http
now read a file with a pathname and utf-8 encoding then parse data into JSON and save on variable create server and route as per request URL for the response also, you can specify header content. Then listen for the request.</p>
<pre><code>const fs <span class="hljs-operator">=</span> <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>)
const http <span class="hljs-operator">=</span> <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>)

const data <span class="hljs-operator">=</span> fs.readFileSync(${__dirname}<span class="hljs-operator">/</span>data<span class="hljs-operator">/</span>data.json, <span class="hljs-string">'utf-8'</span>)
const productData <span class="hljs-operator">=</span> JSON.parse(data)

const server <span class="hljs-operator">=</span> http.createServer((req, res)<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>{
    const path <span class="hljs-operator">=</span> req.url
    <span class="hljs-keyword">if</span>(path <span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-string">'/api'</span>)
    {
        res.writeHead(<span class="hljs-number">200</span>,{
            <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'application/json'</span>
        })
        res.end(data)
    }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(path <span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-string">"/"</span>)
    {
        res.end(<span class="hljs-string">"server is running"</span>)
    }
    <span class="hljs-keyword">else</span>{
        res.writeHead(<span class="hljs-number">404</span>,
            {
                <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html'</span>
            })
            res.end(<span class="hljs-string">"&lt;h1&gt;404 error: page not found&lt;/h1&gt;"</span>)
    }
})
server.listen(<span class="hljs-number">8000</span>, <span class="hljs-string">'127.0.0.1'</span>, ()<span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span>
{
    console.log(<span class="hljs-string">"listening on port 8000"</span>)
})
</code></pre><h3 id="heading-does-anyone-use-nodejs">Does anyone use Nodejs?</h3>
<p>Nodejs has many frameworks and libraries as per your need. Expressjs, socket.io, next.js, meteorjs are some of them. Big community, millions of packages and modules, and used big corporations like Netflix, eBay, PayPal, and uber, Nodejs popularity is increasing daily and so does job opportunity.</p>
]]></content:encoded></item></channel></rss>