<?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: Navindu Abhishek</title>
    <description>The latest articles on Forem by Navindu Abhishek (@navinduabhishek).</description>
    <link>https://forem.com/navinduabhishek</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%2F628408%2F36648b21-973b-4976-abf8-64280f2be01d.jpg</url>
      <title>Forem: Navindu Abhishek</title>
      <link>https://forem.com/navinduabhishek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/navinduabhishek"/>
    <language>en</language>
    <item>
      <title>How to fix 'process' is not defined (React+Vite)</title>
      <dc:creator>Navindu Abhishek</dc:creator>
      <pubDate>Sun, 19 May 2024 06:27:53 +0000</pubDate>
      <link>https://forem.com/navinduabhishek/how-to-fix-process-is-not-defined-reactvite-1nn4</link>
      <guid>https://forem.com/navinduabhishek/how-to-fix-process-is-not-defined-reactvite-1nn4</guid>
      <description>&lt;p&gt;Most of the time when we use "create react app" this error not came. but when we working with react+vite its kind of different. So Here's a step-by-step guide to setting up environment variables in a Vite project.&lt;/p&gt;

&lt;p&gt;I think you already created a .env file root in your project. if not create .env file in root in your project and ensure all variables should be prefixed with VITE_.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;code&gt;VITE_EXAMPLE_API_KEY=your api key&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So we can use import.meta.env object to access these environment variables.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export const exerciseOptions ={&lt;br&gt;
    method: 'GET',&lt;br&gt;
    headers: {&lt;br&gt;
      'X-API-Key': import.meta.env.VITE_EXAMPLE_API_KEY,&lt;br&gt;
      'X-API-Host': 'www.example.com'&lt;br&gt;
    }&lt;br&gt;
  };&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After changing environment variables you have to restart the development server to make this work.&lt;/p&gt;

&lt;p&gt;By following these steps you can fix this issue.&lt;/p&gt;

</description>
      <category>react</category>
      <category>vite</category>
      <category>webdev</category>
      <category>env</category>
    </item>
    <item>
      <title>What is State in React?</title>
      <dc:creator>Navindu Abhishek</dc:creator>
      <pubDate>Mon, 08 Apr 2024 14:53:46 +0000</pubDate>
      <link>https://forem.com/navinduabhishek/what-is-state-in-react-5ckh</link>
      <guid>https://forem.com/navinduabhishek/what-is-state-in-react-5ckh</guid>
      <description>&lt;p&gt;React state is like some kind of storage. It can save values that belong to the component. Like form data or some kind of data like that change-over button click or any user interaction. When the state changes, the component re-renders itself. It automatically updates the changes in content and shows them to the user. This concept is more efficient because React only updates part of the component, not the entire DOM.&lt;/p&gt;

&lt;p&gt;In the past, state could only be used in class components, but with React Hooks introduced in React 16.8, you can now use state in functional components too! This makes your code cleaner and easier to understand. One of the hooks provided by React for managing state in functional components is useState().&lt;/p&gt;

&lt;p&gt;Lets discuss using two simple examples&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Counter App
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';

function Counter() {
  // Initializing state
  const [count, setCount] = useState(0);

  // Function to increment count
  const incrementCount = () =&amp;gt; {
    setCount(count + 1);
  };

  // Function to decrement count
  const decrementCount = () =&amp;gt; {
    setCount(count - 1);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;The count is: {count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={incrementCount}&amp;gt;Click me to increase count&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={decrementCount}&amp;gt;Click me to decrease count&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Counter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This block of code defines a functional component named Counter. It's a simple component that shows a count on the screen and provides buttons to either increment or decrement that count.&lt;/p&gt;

&lt;p&gt;const [count, setCount] = useState(0) initializes the component's state. useState(0) means that count starts with a value of 0.&lt;/p&gt;

&lt;p&gt;count is the current state value, and setCount is a function that updates this value. This is what "state" in React refers to: data that can change over time.&lt;/p&gt;

&lt;p&gt;function increases and decreases the count by 1 whenever it's called. React automatically re-renders the component with the updated state, showing the new count on the screen.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Toggle Message
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';

function ToggleMessage() {
  // Initialize state with the initial message
  const [message, setMessage] = useState("Hello, World!");

  // Function to toggle the message
  const toggleMessage = () =&amp;gt; {
    setMessage(prevMessage =&amp;gt; prevMessage === "Hello, World!" ? "Goodbye, World!" : "Hello, World!");
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;{message}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={toggleMessage}&amp;gt;Toggle Message&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default ToggleMessage;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The toggleMessage function changes the state based on its current value. If the message is "Hello, World!", it switches to "Goodbye, World!" and vice versa. It uses setMessage to update the state, and employs a callback function (prevMessage =&amp;gt; …) to access the current state value and decide the new state based on it.&lt;/p&gt;

&lt;p&gt;After Clicking the button invokes toggleMessage, toggling the state between "Hello, World!" and "Goodbye, World!", which in turn updates the displayed message.&lt;/p&gt;

&lt;p&gt;I hope this explanation helps clarify the concept of state in React. I encourage you to follow best practices in React development and continue exploring its powerful features!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactstate</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
