<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jayanth</title>
    <description>The latest articles on Forem by Jayanth (@jayanthbabu123).</description>
    <link>https://forem.com/jayanthbabu123</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F600861%2F88d4dbdc-8593-4526-81dd-f52f9d86bd11.png</url>
      <title>Forem: Jayanth</title>
      <link>https://forem.com/jayanthbabu123</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jayanthbabu123"/>
    <language>en</language>
    <item>
      <title>How to Change the Default Port Number in React</title>
      <dc:creator>Jayanth</dc:creator>
      <pubDate>Thu, 16 Nov 2023 18:14:11 +0000</pubDate>
      <link>https://forem.com/jayanthbabu123/how-to-change-the-default-port-number-in-react-28cn</link>
      <guid>https://forem.com/jayanthbabu123/how-to-change-the-default-port-number-in-react-28cn</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt; 🌟&lt;br&gt;
When diving into React development, one of the first things you might want to customize is the default port number. React applications, created using &lt;code&gt;create-react-app&lt;/code&gt;, typically run on &lt;code&gt;port 3000&lt;/code&gt;. However, there might be scenarios where this port is already in use or you need to align with certain deployment environments. This article will guide you through the process of changing the default port number in React. We'll cover steps for different operating systems, ensuring beginners and seasoned developers alike can follow along easily.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding the Default Setting&lt;/strong&gt; 🔍&lt;br&gt;
By default, React applications created with &lt;code&gt;create-react-app&lt;/code&gt; are configured to run on &lt;code&gt;port 3000&lt;/code&gt;. This setting is embedded in the underlying scripts that start the development server. To customize this, we need to modify the start-up configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Changing the Port Number in Windows 🪟&lt;/strong&gt;&lt;br&gt;
On Windows, you can change the port number using environment variables. Here's how:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the project folder in your favorite code editor.&lt;/li&gt;
&lt;li&gt;Locate the &lt;code&gt;package.json&lt;/code&gt; file in the root directory.&lt;/li&gt;
&lt;li&gt;In the scripts section, you will find a line similar to 
&lt;code&gt;"start": "react-scripts start"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Modify this line to set the PORT environment variable. For example, to change the port to 5000, use:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "start": "set PORT=5000 &amp;amp;&amp;amp; react-scripts start"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Save the &lt;code&gt;package.json&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Run your project using &lt;code&gt;npm start&lt;/code&gt;. Your React application should now be running on &lt;a href="http://localhost:5000"&gt;http://localhost:5000&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Changing the Port Number in macOS/Linux&lt;/strong&gt; 🐧🍏&lt;br&gt;
The process is similar on &lt;code&gt;macOS&lt;/code&gt; and &lt;code&gt;Linux&lt;/code&gt;, with a slight change in the syntax for setting environment variables. Follow these steps:&lt;br&gt;
Open your project in a code editor.&lt;br&gt;
In the &lt;code&gt;package.json&lt;/code&gt; file, modify the start script like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "start": "PORT=5000 react-scripts start"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;npm start&lt;/code&gt; in your terminal. The application will launch on the new port, such as &lt;a href="http://localhost:5000"&gt;http://localhost:5000&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Environment Configuration Files&lt;/strong&gt; 📄&lt;br&gt;
Another approach is to use environment-specific configuration files. &lt;code&gt;create-react-app&lt;/code&gt; supports &lt;code&gt;.env&lt;/code&gt; files for setting environment variables. Here's how you can use them:&lt;br&gt;
In your project root, create a file named &lt;code&gt;.env&lt;/code&gt;.&lt;br&gt;
Add the following line to set the port:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PORT=5000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the file and run &lt;code&gt;npm start&lt;/code&gt;. Your app will start on the specified port.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion 🎉&lt;/strong&gt;&lt;br&gt;
Customizing the port number in a React application is straightforward. Whether you're on Windows, macOS, or Linux, you can easily adjust this setting in your &lt;code&gt;package.json&lt;/code&gt; file or by using an &lt;code&gt;.env&lt;/code&gt; file. This flexibility allows for seamless integration with various development and deployment environments. Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>5 ways to create an Object in JavaScript</title>
      <dc:creator>Jayanth</dc:creator>
      <pubDate>Wed, 15 Nov 2023 16:46:57 +0000</pubDate>
      <link>https://forem.com/jayanthbabu123/5-ways-to-create-an-object-in-javascript-2g0n</link>
      <guid>https://forem.com/jayanthbabu123/5-ways-to-create-an-object-in-javascript-2g0n</guid>
      <description>&lt;p&gt;In JavaScript, objects are versatile tools that can be created in various ways, each suitable for different scenarios. Understanding when to use each method is key to writing efficient and maintainable JavaScript code. Let’s explore five common methods for creating objects in JavaScript, detailing the best use case for each.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;1. Object Literals&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Object literals are the simplest and fastest way to create objects in JavaScript, using curly braces {}. This method is ideal for creating single, standalone objects that don’t require a blueprint or repeated instantiation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const car = {
  make: 'Toyota',
  model: 'Corolla',
  year: 2021
};

console.log(car);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use object literals for quick, straightforward objects where you don’t need methods or prototypes. They are perfect for configuration options, storing data, and encapsulating related properties and methods in a straightforward structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The new Object() Syntax
&lt;/h2&gt;

&lt;p&gt;Creating an object with the new Object() syntax is a more explicit way of object creation in JavaScript programming. This is similar to object literals but can be more readable for those coming from other programming languages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = new Object();
person.name = 'John';
person.age = 30;
person.isEmployed = true;

console.log(person);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method is beneficial when you want to explicitly demonstrate object creation or when transitioning from a language that heavily uses class-based or constructor-based object creation. It’s also useful when dynamically adding properties based on conditions.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Constructor Functions
&lt;/h2&gt;

&lt;p&gt;Constructor functions are used to create multiple instances of a similar object. They act as a blueprint for creating objects of the same type in JavaScript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Smartphone(brand, model, year) {
  this.brand = brand;
  this.model = model;
  this.year = year;
}

const myPhone = new Smartphone('Apple', 'iPhone 13', 2021);
console.log(myPhone);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Constructor functions are ideal when you need several objects with similar properties and methods. They are great for creating objects like users, products, or other entities that share a common structure but have different values.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. The Object.create() Method
&lt;/h2&gt;

&lt;p&gt;Object.create() in JavaScript creates a new object with a specified prototype and properties. This method provides more control over object inheritance than other methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const animal = {
  type: 'Animal',
  displayType: function() {
    console.log(this.type);
  }
};

const dog = Object.create(animal);
dog.type = 'Dog';
dog.displayType(); // Output: Dog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method is useful when you need to create an object that directly inherits from another without calling the parent’s constructor. It’s a powerful tool for complex inheritance structures and can be used for behavior delegation, among other advanced patterns.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. ES6 Class Syntax
&lt;/h2&gt;

&lt;p&gt;ES6 classes offer a more traditional, class-based way to create objects in JavaScript. It’s syntactical sugar over JavaScript’s prototype-based inheritance but provides a clearer and more familiar syntax for those coming from class-based languages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Book {
  constructor(title, author, year) {
    this.title = title;
    this.author = author;
    this.year = year;
  }

  getSummary() {
    return `${this.title} was written by ${this.author} in ${this.year}`;
  }
}

const myBook = new Book('1984', 'George Orwell', 1949);
console.log(myBook.getSummary());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use ES6 classes for more complex applications where the organization, readability, and inheritance structure of your code are important. They are useful for large-scale applications and when working in a team where clear and standardized coding practices are needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which Method is Best and Fastest?
&lt;/h2&gt;

&lt;p&gt;There’s no one-size-fits-all answer to the best and fastest way to create objects as it largely depends on the specific requirements and context of your application. However, in terms of simplicity and speed, object literals are the fastest and easiest way to create objects, especially for simple, single-use objects. For more complex and structured applications, ES6 classes provide a balance of readability, traditional syntax, and performance, although they may have a slight overhead compared to constructor functions.&lt;/p&gt;

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

&lt;p&gt;Choosing the right method to create objects in JavaScript depends on the context of your application and specific requirements. Whether it’s a simple one-off object or a complex structure requiring inheritance, understanding these five methods enables you to write more effective and maintainable JavaScript code.&lt;/p&gt;

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

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Integrating GraphQL in React.js: Simplifying Data Fetching</title>
      <dc:creator>Jayanth</dc:creator>
      <pubDate>Thu, 09 Nov 2023 18:45:50 +0000</pubDate>
      <link>https://forem.com/jayanthbabu123/integrating-graphql-in-reactjs-simplifying-data-fetching-5aan</link>
      <guid>https://forem.com/jayanthbabu123/integrating-graphql-in-reactjs-simplifying-data-fetching-5aan</guid>
      <description>&lt;p&gt;GraphQL has revolutionized the way developers interact with APIs. Unlike REST APIs, GraphQL allows clients to request exactly the data they need, no more, no less. In React.js, integrating GraphQL enhances the component's data-fetching capabilities by making the data retrieval process more efficient and precise. In this blog, we'll walk through the steps to integrate a GraphQL API with a React.js application using Apollo Client.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is GraphQL?&lt;/strong&gt;&lt;br&gt;
GraphQL is a query language for your API, and a server-side runtime for executing those queries by parsing your data. It isn't tied to any specific database or storage engine and is instead backed by your existing code and data.&lt;/p&gt;

&lt;p&gt;To integrate GraphQL into a React.js application, we will use &lt;code&gt;Apollo Client&lt;/code&gt;, a popular, feature-rich GraphQL client that provides a comprehensive approach to data management in modern web applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up Apollo Client&lt;/strong&gt;&lt;br&gt;
To integrate Apollo Client into your React app, you'll first need to install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @apollo/client graphql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, you’ll need to set up &lt;code&gt;ApolloClient&lt;/code&gt;and the &lt;code&gt;ApolloProvider&lt;/code&gt;. This provider component uses React's Context API to make a client instance available throughout the component tree.&lt;/p&gt;

&lt;p&gt;For demonstration purposes, we'll use the &lt;code&gt;SpaceX GraphQL API&lt;/code&gt;, which is a public API that provides data about SpaceX launches.&lt;/p&gt;

&lt;p&gt;Here's how you configure Apollo Client with this real GraphQL endpoint:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbh9nmh3tb2tkftkt3lkm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbh9nmh3tb2tkftkt3lkm.png" alt="Appolo Client with React"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this configuration, your React application is now ready to query the SpaceX API and render the data in your UI. You can use the provided &lt;code&gt;useQuery&lt;/code&gt;, &lt;code&gt;useMutation&lt;/code&gt;, and &lt;code&gt;useSubscription&lt;/code&gt;hooks to interact with the API according to your application's needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fetching Data in the Dashboard Component&lt;/strong&gt;&lt;br&gt;
Within the Dashboard component, you can now make use of Apollo Client's &lt;code&gt;useQuery&lt;/code&gt;hook to fetch data from the SpaceX API. The &lt;code&gt;useQuery&lt;/code&gt;hook leverages React's Hooks API to fetch and load data from GraphQL queries.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use &lt;code&gt;useQuery&lt;/code&gt; to retrieve a list of SpaceX launches: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4r4eijjehej2vwagfony.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4r4eijjehej2vwagfony.png" alt="Fetching data with graphql API in React"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this code, we are creating a simple UI that maps over the array of past launches returned by the &lt;code&gt;GET_LAUNCHES&lt;/code&gt;query. Each launch is displayed in its own div with details such as mission name, launch date. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advanced Data Fetching Techniques with Apollo Client&lt;/strong&gt;&lt;br&gt;
As you become more comfortable with the basics of fetching data using Apollo Client, you'll soon run into scenarios that require more advanced techniques. Let's delve into some of these techniques to enhance your React applications.&lt;/p&gt;

&lt;p&gt;Using variables in your GraphQL queries allows you to write dynamic and reusable queries that can adapt based on user interaction or component state changes. Let's walk through an example where users can filter a list of launches by a mission name that they input into a search field.&lt;/p&gt;

&lt;p&gt;First, we define our GraphQL query to accept a variable for the &lt;code&gt;mission name&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Define the GraphQL query with a variable
const GET_LAUNCHES_BY_MISSION_NAME = gql`
  query GetLaunchesByMissionName($missionName: String!) {
    launchesPast(find: { mission_name: $missionName }) {
      mission_name
      launch_date_local
    }
  }
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we'll create a React component with a search field where users can type in a &lt;code&gt;mission name&lt;/code&gt;. We'll use Apollo Client's &lt;code&gt;useQuery&lt;/code&gt;hook to fetch the launches based on the &lt;code&gt;mission name&lt;/code&gt;provided.&lt;/p&gt;

&lt;p&gt;Here's a concise example of how that might look in your React component:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjgt4p3r6b4yss3nlekrc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjgt4p3r6b4yss3nlekrc.png" alt="User Search example with GraphQL"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;LaunchesSearch&lt;/code&gt;component, we are maintaining the state for &lt;code&gt;missionName&lt;/code&gt;which is updated whenever the input field changes. The &lt;code&gt;useQuery&lt;/code&gt;hook then uses this state as a variable for the GraphQL query, which will refetch the data whenever the &lt;code&gt;missionName&lt;/code&gt;changes. The skip option is used to avoid executing the query before the user has entered a search term.&lt;/p&gt;

&lt;p&gt;This pattern is highly effective for creating interactive and responsive interfaces that provide feedback to the user as they search or filter data.&lt;/p&gt;

&lt;p&gt;The complete working code example you can found here.&lt;br&gt;
&lt;a href="https://codesandbox.io/s/react-js-with-graphql-forked-tcmz2v?file=/src/App.js" rel="noopener noreferrer"&gt;https://codesandbox.io/s/react-js-with-graphql-forked-tcmz2v?file=/src/App.js&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;We've seen how to integrate a &lt;code&gt;GraphQL&lt;/code&gt; API into a React.js application using &lt;code&gt;Apollo Client&lt;/code&gt;, from setting up Apollo Client and configuring it to use a real GraphQL endpoint, to fetching data and displaying it with a styled component. GraphQL’s power lies in its ability to allow clients to request exactly what they need, and Apollo Client makes it easy to fetch, cache, and manage that data in a React.js application.&lt;/p&gt;

&lt;p&gt;Remember, the &lt;code&gt;SpaceX API&lt;/code&gt; used in this example is a real public API, which makes it an excellent resource for practicing GraphQL queries and mutations.&lt;/p&gt;

&lt;p&gt;Integrating GraphQL with React.js can significantly improve the performance and user experience of your application by avoiding over-fetching or under-fetching of data. With tools like Apollo Client, it becomes relatively straightforward to manage your data and keep your UI in sync with the state of your data in the backend.&lt;/p&gt;

&lt;p&gt;I hope this guide has been helpful to get you started with GraphQL in React.js applications. With the basic setup done, you're now ready to explore more advanced features of Apollo Client such as local state management, error handling, and subscriptions for real-time updates.&lt;/p&gt;

&lt;p&gt;Keep coding, and may your queries always return exactly what you expect!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>graphql</category>
    </item>
  </channel>
</rss>
