<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: CHIMDI</title>
    <description>The latest articles on DEV Community by CHIMDI (@anyiamchimdia).</description>
    <link>https://dev.to/anyiamchimdia</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%2F443768%2F5fceee1a-8178-4a5f-8a03-023a023314f1.jpg</url>
      <title>DEV Community: CHIMDI</title>
      <link>https://dev.to/anyiamchimdia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anyiamchimdia"/>
    <language>en</language>
    <item>
      <title>Beginners guide to react query</title>
      <dc:creator>CHIMDI</dc:creator>
      <pubDate>Thu, 29 Jun 2023 13:34:16 +0000</pubDate>
      <link>https://dev.to/anyiamchimdia/beginners-guide-to-react-query-4id1</link>
      <guid>https://dev.to/anyiamchimdia/beginners-guide-to-react-query-4id1</guid>
      <description>&lt;p&gt;This is you building the frontend of your million-dollar app: Fetch data from APIs, store resulting data in state, create a loading state, access data in multiple components, access loading state, update state after editing data on the backend, optimise API calls, etc. These and many more are the issues that many developers face when building their frontend apps. As developers, we almost always fetch data from APIs, handle state management, and optimise performance. These tasks can be a pain in the a** even for the smallest applications but I bring good news: React Query. React Query is a tool that was built to make the tasks listed earlier seem like a walk in the park.&lt;/p&gt;

&lt;p&gt;On their website, they describe it as “the missing data-fetching library for React, but in more technical terms, it makes fetching, caching, synchronising and updating server state in your React applications a breeze.” In this article, I will be introducing you to react query and showing you how you can use it to make your life as a developer not as bad as it already is (that was a joke lol).&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To follow along with this article, it is expected that you have :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Basic react knowledge&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;In order to learn how react query works, we’re going to be building the classic todo app.&lt;/p&gt;

&lt;p&gt;Let’s begin by initialising a new react app using vite. Open a new terminal session and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn create vite todo-query --template react

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I named my project todo-query, you can use whatever name you want. &lt;/p&gt;

&lt;p&gt;Run the following set of commands to install the required dependencies and start the development server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd todo-query
yarn
yarn dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running these, you should have a project looking like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VsnJlX83--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zi9xf6x1w1w6qclrioif.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VsnJlX83--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zi9xf6x1w1w6qclrioif.png" alt="Initial project" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let’s install react-query using this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add react-query
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installing these dependencies, proceed to open the project in any IDE of your choice. I will be using VSCode.&lt;/p&gt;

&lt;p&gt;Navigate to the main.jsx file in your code editor and replace it with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
import "./index.css";
import {
  QueryClient,
  QueryClientProvider,
} from "@tanstack/react-query";

// Create a client
const queryClient = new QueryClient();

ReactDOM.createRoot(document.getElementById("root")).render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;QueryClientProvider client={queryClient}&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/QueryClientProvider&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the piece of code above, we are importing the query client and query client provider, instantiating a new query client, and using the query client provider to make the query client accessible all through our app. (Think of it like providing a react context to your app with a context provider).&lt;/p&gt;

&lt;p&gt;We now have a query client provided all through our app and can begin to use react query for our data fetching and caching needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Queries
&lt;/h2&gt;

&lt;p&gt;Next, we want to fetch a list of todos and display them to the user when our app loads.&lt;br&gt;
To do this, we make use of the useQuery hook. The useQuery hook accepts different parameters, but fundamentally, we want to pass it two important parameters:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;queryKey: This is a unique string that will be used to “identify” your query. It is used by react query  to manage the caching of your queries. It can be a string, an array of strings, or a nested object.&lt;/li&gt;
&lt;li&gt;queryFunction: This is the function that the query would use to fetch data; it should return a promise.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Delete everything in index.css.&lt;/p&gt;

&lt;p&gt;In the app.jsx file, write the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useQuery } from "react-query";

function App() {
  let getTodos = async () =&amp;gt; {
    const response = await fetch("https://jsonplaceholder.typicode.com/todos”);
    return response.json();
  };
  const { data } = useQuery("todos", getTodos);

  console.log(data)

  return (
    &amp;lt;&amp;gt;
      &amp;lt;h3&amp;gt;Awesome Todo&amp;lt;/h3&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s go through the code above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useQuery } from "react-query";

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’re importing the necessary dependency which is react query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let getTodos = async () =&amp;gt; {
    const response = await fetch("https://jsonplaceholder.typicode.com/todos);
    return response.json();
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create a getTodos function that uses the fetch API to get fetch a list of todos from a third-party API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { data } = useQuery("todos", getTodos);
console.log(data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use react query here to make a query with the queryKey being "todos" and the query function being getTodos after which we log the results to the console.&lt;/p&gt;

&lt;p&gt;If you have been following up to this point and you open up your console, you should see a JSON object that contains a list of todo items.&lt;/p&gt;

&lt;p&gt;The next thing we want to do is display these todo items on the screen.&lt;br&gt;
Replace your code with the code below, changing only what’s necessary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useQuery } from "react-query";

function App() {
  let getTodos = async () =&amp;gt; {
    const response = await fetch("https://jsonplaceholder.typicode.com/todos");
    return response.json();
  };
  const { data, isLoading, isError } = useQuery("todos", getTodos);

  console.log(data);

  return (
    &amp;lt;&amp;gt;
      &amp;lt;h3&amp;gt;Awesome Todo&amp;lt;/h3&amp;gt;
      {isError &amp;amp;&amp;amp; &amp;lt;p&amp;gt;An Unexpected error has occured&amp;lt;/p&amp;gt;}
      {isLoading &amp;amp;&amp;amp; &amp;lt;p&amp;gt;Loading Todos&amp;lt;/p&amp;gt;}
      {!isLoading &amp;amp;&amp;amp;
        data.map((todo) =&amp;gt; (
          &amp;lt;div
            key={todo.id}
            style={{
              borderBottom: `1px solid gray`,
              color: `${todo.completed ? "green" : "red"}`,
            }}
          &amp;gt;
            &amp;lt;p&amp;gt;User:{todo.userId}&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;Title:{todo.title}&amp;lt;/p&amp;gt;
          &amp;lt;/div&amp;gt;
        ))}
    &amp;lt;/&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s go through the parts of the code that have changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { data, isLoading, isError } = useQuery("todos", getTodos);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, in addition to extracting the data from the query, we can also extract the isLoading and isError states, which tell us if the query is still loading and if an error occurred respectively. Without react query, we would have had to manually create and manage these states but react query does this for us effortlessly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return (
    &amp;lt;&amp;gt;
      &amp;lt;h3&amp;gt;Awesome Todo&amp;lt;/h3&amp;gt;
      {isError &amp;amp;&amp;amp; &amp;lt;p&amp;gt;An Unexpected error has occured&amp;lt;/p&amp;gt;}
      {isLoading &amp;amp;&amp;amp; &amp;lt;p&amp;gt;Loading Todos&amp;lt;/p&amp;gt;}
      {!isLoading &amp;amp;&amp;amp;
        data.map((todo) =&amp;gt; (
          &amp;lt;div
            key={todo.id}
            style={{
              borderBottom: `1px solid gray`,
              color: `${todo.completed ? "green" : "red"}`,
            }}
          &amp;gt;
            &amp;lt;p&amp;gt;User:{todo.userId}&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;Title:{todo.title}&amp;lt;/p&amp;gt;
          &amp;lt;/div&amp;gt;
        ))}
    &amp;lt;/&amp;gt;
  );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are returning the jsx, where we display an error message or a loading message depending on their states, or we loop over the todo data and display them on the screen.&lt;br&gt;
Now we should have something that looks like this; forgive the poor UI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bG1NarsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j911lmmb5dh3z3ige0r8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bG1NarsJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j911lmmb5dh3z3ige0r8.png" alt="Todo UI" width="800" height="751"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the above, we can see how we got the loading state, error state, and data in just one line of code. How awesome is that?&lt;/p&gt;

&lt;p&gt;React query allows us to do multiple other things, such as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Deduping requests&lt;/li&gt;
&lt;li&gt;Paginating queries&lt;/li&gt;
&lt;li&gt;Auto-refetching / polling&lt;/li&gt;
&lt;li&gt;Caching, and a lot more&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Mutations
&lt;/h2&gt;

&lt;p&gt;We now know how to get data from our server using react query; the next thing we’ll learn to do is to make changes/updates to our server state. In react query terminology, these are called mutations. We can read our list of todos, next thing we want to do is add a new todo item to our list of todos. To do this, we’ll make use of the useMutation hook, and our code will look like the one below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useQuery, useMutation } from "react-query";

function App() {
  let getTodos = async () =&amp;gt; {
    const response = await fetch("https://jsonplaceholder.typicode.com/todos");
    return response.json();
  };
  const { data, isLoading, isError } = useQuery("todos", getTodos);

  let addTodo = async () =&amp;gt; {
    const response = await fetch("https://jsonplaceholder.typicode.com/todos", {
      method: "POST",
      body: JSON.stringify({
        title: "fodddddo",
        completed: "false",
        id: 1,
        userId: 1,
      }),
      headers: {
        "Content-type": "application/json; charset=UTF-8",
      },
    });
    return response.json();
  };
  const { mutate } = useMutation(addTodo, {
    onSuccess: (res) =&amp;gt; {
      console.log(res);
    },
    onError: (error) =&amp;gt; {
      console.log(error);
    },
  });

  return (
    &amp;lt;&amp;gt;
      &amp;lt;h3&amp;gt;Awesome Todo&amp;lt;/h3&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; mutate()}&amp;gt;Add Todo&amp;lt;/button&amp;gt;
      {isError &amp;amp;&amp;amp; &amp;lt;p&amp;gt;An Unexpected error has occured&amp;lt;/p&amp;gt;}
      {isLoading &amp;amp;&amp;amp; &amp;lt;p&amp;gt;Loading Todos&amp;lt;/p&amp;gt;}
      {!isLoading &amp;amp;&amp;amp;
        data.map((todo) =&amp;gt; (
          &amp;lt;div
            key={todo.id}
            style={{
              borderBottom: `1px solid gray`,
              color: `${todo.completed ? "green" : "red"}`,
            }}
          &amp;gt;
            &amp;lt;p&amp;gt;User:{todo.userId}&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;Title:{todo.title}&amp;lt;/p&amp;gt;
          &amp;lt;/div&amp;gt;
        ))}
    &amp;lt;/&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Let’s go through the changes in the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; let addTodo = async () =&amp;gt; {
    const response = await fetch("https://jsonplaceholder.typicode.com/todos", {
      method: "POST",
      body: JSON.stringify({
        title: "fodddddo",
        completed: "false",
        id: 1,
        userId: 1,
      }),
      headers: {
        "Content-type": "application/json; charset=UTF-8",
      },
    });
    return response.json();
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this block of code, we are creating our mutation function that is passed to the useMutation hook. The function should perform an asynchronous task and return a promise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { mutate ,isLoading: addingTodo } = useMutation(addTodo, {
    onSuccess: (res) =&amp;gt; {
      console.log(res);
    },
    onError: (error) =&amp;gt; {
      console.log(error);
    },
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, this is where we use our useMutation hook and destructure the mutate function and the isLoading function (we rename it to addingTodo because we’ve used isLoading before) from it. We also pass in an onSuccess and onError function to it so we can perform actions depending on what happens after the mutation. Because we aren’t using our own API, we will just be logging the results to the console for now, but in the onSuccess function for example, you’ll want to do something like invalidate the todos query so that react query can fetch the new list of todos with our updated todo and in the onError function, we can alert the user that an error occurred.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button disabled={addingTodo} onClick={() =&amp;gt; mutate()}&amp;gt;Add Todo&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we simply pass a function that calls our mutation function into the onClick handler of our button, and we set the disabled value of our button to be equal to the value of addingTodo (i.e isLoading).&lt;/p&gt;

&lt;p&gt;See how simple react-query makes fetching and changing server state look. We would have had to use different data states and loading states and also add side effects for when we change data on the server, but react-query allows us to  handle all of this elegantly.&lt;/p&gt;

&lt;h2&gt;
  
  
  React Query Devtools
&lt;/h2&gt;

&lt;p&gt;React query also has a devtool!! To add it to your project, replace your main.jsx with the code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
import "./index.css";
import { QueryClient, QueryClientProvider } from "react-query";
import { ReactQueryDevtools } from "react-query/devtools";

// Create a client
const queryClient = new QueryClient();

ReactDOM.createRoot(document.getElementById("root")).render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;QueryClientProvider client={queryClient}&amp;gt;
      &amp;lt;App /&amp;gt;
      &amp;lt;ReactQueryDevtools initialIsOpen={false} /&amp;gt;
    &amp;lt;/QueryClientProvider&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We import the ReactQueryDevtools and use it in our project. If you go back to your project you should see an icon in the bottom left corner. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eaWwXTw2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzg19n84bvq4jklxmtxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eaWwXTw2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzg19n84bvq4jklxmtxy.png" alt="React query devtools icon" width="800" height="157"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you click on it, you will see the dev tools panel&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iK7BztiO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mb9udflf26ex27c8gk38.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iK7BztiO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mb9udflf26ex27c8gk38.png" alt="React query devtools" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The devtools will help you picture your apps cache and everything that’s going on as far as react-query is concerned. It can be a very useful tool when debugging.&lt;/p&gt;

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

&lt;p&gt;And that’s a wrap!! We just just went through using react query to make our interactions with our server state easier and smoother but there’s a whole lot more we can do with react query. This is just a tip of the iceberg with react query you can do a whole lot more ad I invite you to check out what you can do by taking a look at their &lt;a href="https://tanstack.com/query/v3/docs/react/overview"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>frontend</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Creating Multilingual React Apps with i18n: A Step-by-Step Guide to Internationalisation</title>
      <dc:creator>CHIMDI</dc:creator>
      <pubDate>Tue, 11 Apr 2023 10:42:59 +0000</pubDate>
      <link>https://dev.to/anyiamchimdia/creating-multilingual-react-apps-with-i18n-a-step-by-step-guide-to-internationalisation-107o</link>
      <guid>https://dev.to/anyiamchimdia/creating-multilingual-react-apps-with-i18n-a-step-by-step-guide-to-internationalisation-107o</guid>
      <description>&lt;p&gt;With over 20 languages having more than 80 million speakers, if you are building software or a website for the global market and want it to be widely accepted, giving the users an opportunity to use the product in their native language would definitely lead to a better user experience. I18next is an internationalisation framework that can help with internationalising/localising your app. &lt;/p&gt;

&lt;h4&gt;
  
  
  What is internationalisation?
&lt;/h4&gt;

&lt;p&gt;Internationalisation is a way of preparing and writing your apps so that they can be adapted to support certain local languages, cultures and regions. It enables localisation which is the adaptation of a product, application or document content to meet the language, cultural and other requirements of a specific target market (a locale). (&lt;a href="https://www.w3.org/International/questions/qa-i18n" rel="noopener noreferrer"&gt;https://www.w3.org/International/questions/qa-i18n&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;In this article, we will go over how to perform internationalisation in our react app using the i18next framework.&lt;br&gt;
I18next is an internationalisation framework written in JavaScript. It comes with everything you need to localise your web, desktop or mobile product such as user language detection, loading and caching translations, and  file conversion etc More features can be found in their documentation. (&lt;a href="https://www.i18next.com/" rel="noopener noreferrer"&gt;https://www.i18next.com/&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Now let us proceed to use i18n in our react application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step1 - Initialise the Project
&lt;/h4&gt;

&lt;p&gt;First let us create our React project using Vite. Open your terminal and run the following command to create a new react project using Vite.&lt;/p&gt;

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

yarn create vite my-i18n-app --template react


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Change your current directory to that of the new project using&lt;/p&gt;

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

cd my-i18n-app


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Proceed to open the newly created project in any code editor of your choice.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step2 - Install dependencies
&lt;/h4&gt;

&lt;p&gt;Add i18next to the project using the command below&lt;/p&gt;

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

yarn add i18next react-i18next



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Start the react project to make sure that everything is working fine&lt;/p&gt;

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

yarn run dev



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F5v9gnb81casb1jy2wfq1.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%2F5v9gnb81casb1jy2wfq1.png" alt="Initial app"&gt;&lt;/a&gt;&lt;br&gt;
You should have the above when you navigate to your projects url.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step3 - Change App content
&lt;/h4&gt;

&lt;p&gt;Lets go ahead and replace the entire app.jsx with the following code:&lt;/p&gt;

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

import './App.css'

function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;h1&amp;gt;Welcome to my i18n project&amp;lt;/h1&amp;gt;
      &amp;lt;h1&amp;gt;How is your day going?&amp;lt;/h1&amp;gt;

    &amp;lt;/div&amp;gt;
  )
}

export default App


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now our project should look like this:&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%2Ftnf9d5deo1qjenq9bl6p.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%2Ftnf9d5deo1qjenq9bl6p.png" alt="Initial project look"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step4 - Add Translations
&lt;/h4&gt;

&lt;p&gt;In order to maintain organisation in our code, let’s create translation files for each language in a translation folder. Our project will now have the following structure:&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%2Fed3xtsgca45585pdujg2.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%2Fed3xtsgca45585pdujg2.png" alt="Folder structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, add language jsons to the translation files.&lt;br&gt;
For English.json:&lt;/p&gt;

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

{
    "line1":"Welcome to my i18n project",
    "line2":"How is your day going?"
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For Italian.json:&lt;/p&gt;

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

{
    "line1":"Benvenuti nel mio progetto i18n",
    "line2":"Come sta andando la tua giornata?"
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Step4 - Add i18next
&lt;/h4&gt;

&lt;p&gt;Now let’s import i18next into our project and configure it.&lt;br&gt;
In the root of the project, create an i18next.jsx file and write the following code:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import i18next from "i18next";
import { initReactI18next } from "react-i18next";

//Import all translation files
import English from "./Translation/English.json";
import Italian from "./Translation/Italian.json";

const resources = {
    en: {
        translation: English,
    },
    it: {
        translation: Italian,
    },
}

i18next.use(initReactI18next)
.init({
  resources,
  lng:"en", //default language
});

export default i18next;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code above, we are setting up and initialising i18n in our project. We provide the sources of the translations and also set english as the default language. Note that the ISO 639–1 standard language codes should be used when identifying/choosing languages.&lt;/p&gt;

&lt;p&gt;Next, we import this i18n.jsx file inside our main.jsx file to connect it with our react app, It should look like this now: &lt;/p&gt;

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

import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import './index.css'

import './i18n'

ReactDOM.createRoot(document.getElementById('root')).render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;,
)



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Step5 - Create context for languages
&lt;/h4&gt;

&lt;p&gt;Next step is to create a language context that we will use to provide our i18n functionality all through our app.&lt;/p&gt;

&lt;p&gt;Create a new folder and a languageContext.jsx file in it, put the following code in the file:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import React, {
  createContext,
  useContext,
} from "react";
import { useTranslation } from "react-i18next";


export const LanguageContext = createContext(undefined);

export const LanguageContextProvider = ({ children }) =&amp;gt; {
  const languages = {
    en: { nativeName: "English" },
    It: { nativeName: “Italian” },
  };
  const { t, i18n } = useTranslation();

  const onClickLanguageChange = (e) =&amp;gt; {
    const language = e.target.value;
    i18n.changeLanguage(language); //change the language
  };

  return (
    &amp;lt;LanguageContext.Provider
      value={{ t, i18n, onClickLanguageChange, languages }}
    &amp;gt;
      {children}
    &amp;lt;/LanguageContext.Provider&amp;gt;
  );
};

export const useLanguageContext = () =&amp;gt; useContext(LanguageContext);



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s go through the code above&lt;/p&gt;

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

import React, {
  createContext,
  useContext,
} from "react";
import { useTranslation } from "react-i18next";


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here we import the necessary dependencies, createContext and useContext are used for context while the useTranslation is a hook from react-18next which provides us with the t function used to translate text in our app and the i8n instance that will be used to change language.&lt;/p&gt;

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

export const LanguageContext = createContext(undefined);



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This line creates the language context&lt;/p&gt;

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

const languages = {
    en: { nativeName: "English" },
    It: { nativeName: “Italian” },
  };


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here we are creating a languages object containing our supported languages, it will be used in our languageSelect component.&lt;/p&gt;

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

const { t, i18n } = useTranslation();



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Extracting t function and i18n instance with object destructuring&lt;/p&gt;

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

const onClickLanguageChange = (e) =&amp;gt; {
    const language = e.target.value;
    i18n.changeLanguage(language); //change the language
  };


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We create an Event handler to change the current language. &lt;/p&gt;

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

return (
    &amp;lt;LanguageContext.Provider
      value={{ t, i18n, onClickLanguageChange, languages }}
    &amp;gt;
      {children}
    &amp;lt;/LanguageContext.Provider&amp;gt;)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Returning the language context provider and passing the necessary values&lt;/p&gt;

&lt;p&gt;Now, go ahead and wrap the entire app with the language context provider in main.jsx&lt;/p&gt;

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

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./index.css";
import { LanguageContextProvider } from "./context/LanguageContext";

import "./i18n";

ReactDOM.createRoot(document.getElementById("root")).render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;LanguageContextProvider&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/LanguageContextProvider&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;
);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s check that this is working by using it in the app.jsx file&lt;/p&gt;

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

import "./App.css";

import { useLanguageContext } from "./context/LanguageContext";
function App() {
  const { t } = useLanguageContext();
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;h1&amp;gt;{t("line1")}&amp;lt;/h1&amp;gt;
      &amp;lt;h1&amp;gt;{t("line2")}&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you followed all the steps, there should be no change in the UI and this means that everything is working perfectly.&lt;/p&gt;

&lt;p&gt;Finally, let us create a LanguageSelect component that we can use to switch between languages.&lt;/p&gt;

&lt;p&gt;Create a LanguageSelect.jsx file and add the following code to it.&lt;/p&gt;

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

import { useLanguageContext } from "./context/LanguageContext";
const LanguageSelect = () =&amp;gt; {
  const { languages, onClickLanguageChange } = useLanguageContext();
  return (
    &amp;lt;select
      style={{
        width: 200,
        position: "absolute",
        top: 10,
        left: 10,
        height: "40px",
      }}
      onChange={onClickLanguageChange}
    &amp;gt;
      {Object.keys(languages).map((lng) =&amp;gt; (
        &amp;lt;option key={languages[lng].nativeName} value={lng}&amp;gt;
          {languages[lng].nativeName}
        &amp;lt;/option&amp;gt;
      ))}
    &amp;lt;/select&amp;gt;
  );
};

export default LanguageSelect;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code above we are getting the languages from the language context and mapping over them to create a select component that will trigger the onClickLanguageChange when its current value is changed.&lt;/p&gt;

&lt;p&gt;Go ahead and include the LanguageSelect component in our main.js file so it’s available throughout our app&lt;/p&gt;

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

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./index.css";
import { LanguageContextProvider } from "./context/LanguageContext";

import "./i18n";
import LanguageSelect from "./LanguageSelect";

ReactDOM.createRoot(document.getElementById("root")).render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;LanguageContextProvider&amp;gt;
      &amp;lt;LanguageSelect/&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/LanguageContextProvider&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;
);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now our project should look like this&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%2Fekw8k1m4tth8eki2i33j.gif" 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%2Fekw8k1m4tth8eki2i33j.gif" alt="Final Application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There you have it! You have successfully added internationalisation (i18n) functionality to your React app using i18next. Your app is now able to display text in different languages based on the user's preference.&lt;/p&gt;

&lt;p&gt;By following the steps outlined in this tutorial, you have learned how to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install and configure i18next in a React app&lt;/li&gt;
&lt;li&gt;Create translation files for multiple languages&lt;/li&gt;
&lt;li&gt;Use the useTranslation hook to translate text in your app&lt;/li&gt;
&lt;li&gt;Create a language context to provide i18n functionality throughout your app&lt;/li&gt;
&lt;li&gt;Use the LanguageContextProvider to wrap your app with the language context&lt;/li&gt;
&lt;li&gt;Create a LanguageSelect component to allow users to switch between languages&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Remember that i18n is an ongoing process, and there may be more text that needs to be translated as your app grows. With i18next, you can easily add new translations and update existing ones as needed.&lt;/p&gt;

&lt;p&gt;Thanks for following along, and happy internationalisation!&lt;/p&gt;

</description>
      <category>react</category>
      <category>i18n</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Setting Up a Vanilla TypeScript Project</title>
      <dc:creator>CHIMDI</dc:creator>
      <pubDate>Thu, 23 Mar 2023 11:52:26 +0000</pubDate>
      <link>https://dev.to/anyiamchimdia/setting-up-a-vanilla-typescript-project-4me1</link>
      <guid>https://dev.to/anyiamchimdia/setting-up-a-vanilla-typescript-project-4me1</guid>
      <description>&lt;p&gt;So you've just finished your JavaScript tutorial, and now you're hearing that there's a better way to write JavaScript - TypeScript. Depending on your previous programming experience, you may or may not be familiar with typing and type safety, which is what TypeScript adds to the already awesome JavaScript. TypeScript makes writing JavaScript easier and prevents a lot of common and unnecessary errors. In this article, you'll learn how to set up a vanilla TypeScript project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before you proceed with this article, the following are assumed:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You know JavaScript (obviously lol)&lt;/li&gt;
&lt;li&gt;The latest version of Node.js is installed on your system.&lt;/li&gt;
&lt;li&gt;You are familiar with npm.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 1 - Initialise the TypeScript Project
&lt;/h2&gt;

&lt;p&gt;First, you have to create a directory for your new project. Open the command line and type the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir my-awesome-typescript-project
cd my-awesome-typescript-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you're in the new directory, go ahead and install TypeScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i typescript --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;--save-dev&lt;/code&gt; flag saves TypeScript as a devDependency, so you know that TypeScript is needed for development.&lt;/p&gt;

&lt;p&gt;After TypeScript is installed, initialise the project by using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command above uses npx (a tool that allows you to execute any JavaScript package available on the NPM registry without even installing it) to run the command that initializes a TypeScript project.&lt;/p&gt;

&lt;p&gt;Running this command creates a new tsconfig.json file, which defines how TypeScript and the tsc compiler should interact (what JavaScript version to produce, what files to compile, what directory to compile files to, amongst others). You can learn more about the tsconfig.json file in the official TypeScript handbook.&lt;/p&gt;

&lt;p&gt;Go ahead and open the tsconfig.json file in any code editor of your choice. When you open it, you will notice a lot of defaults (mostly commented out), which may be overwhelming. But with time, you'll get to know which ones to modify to suit your use case.&lt;/p&gt;

&lt;p&gt;Replace the entire tsconfig.json file with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "lib": ["ES2015”],
    "module": "commonjs",
    "outDir": "dist",
    "strict": true,
    "target": "ES2015”,
  }
  "include": ["src"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above configuration specifies the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lib: Specify which APIs TSC should assume exists in the target runtime environment&lt;/li&gt;
&lt;li&gt;Module: Specify the module system tic should use to compile your code (commonJS,ES2015 etc)&lt;/li&gt;
&lt;li&gt;OutDir: Specify an output folder for all emitted files.&lt;/li&gt;
&lt;li&gt;Strict: Enable all strict type-checking options.&lt;/li&gt;
&lt;li&gt;Target: Set the JavaScript language version for emitted JavaScript and include compatible library declarations.&lt;/li&gt;
&lt;li&gt;Include: Specify which folders TSC should look in to find your TypeScript files&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once that is done, proceed to coding and compiling your typescript app&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2 - Code and compile your typescript project
&lt;/h2&gt;

&lt;p&gt;Now that you have successfully setup your typescript project, the next step is to write your code and compile it.&lt;/p&gt;

&lt;p&gt;From the tsconfig.json file above, we told the typescript compiler to compile code from the src folder so let us go ahead and create our first typescript file.&lt;br&gt;
In our projects directory, let’s create a new src directory where all our typescript files will be written and Lets go into the directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir src
cd src
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a typescript file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Touch index.ts

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now open the newly created file in your preferred code editor and add the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let isWorldSaved: boolean = false;

export function saveTheWorld(): string {
  if (isWorldSaved) {
    return `Too late, world has already been saved`;
  } else {
    isWorldSaved = true;
    return `Hurray, you just saved the world`;
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you’ve written this typescript code, go ahead and run the typescript compiler&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you run this, you’d notice that a new index.js file will be added to the dist folder just like we want and it should look 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;"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveTheWorld = void 0;
let isWorldSaved = false;
function saveTheWorld() {
    if (isWorldSaved) {
        return `Too late, world has already been saved`;
    }
    else {
        isWorldSaved = true;
        return `Hurray, you just saved the world`;
    }
}
exports.saveTheWorld = saveTheWorld;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also run the typescript compiler in watch mode so you don’t have to run the npx tsc command each time you make a change. Do this by running the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc -w
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it!! You’ve learnt how to setup and run your typescript project and now you can use typescript to avoid all the surprises and headache that comes with writing vanilla javascript. You can use this project to practice and learn more about typescript&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>html</category>
    </item>
  </channel>
</rss>
