<?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: Rufat Aliyev</title>
    <description>The latest articles on Forem by Rufat Aliyev (@rufataliy).</description>
    <link>https://forem.com/rufataliy</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%2F176518%2Fbfc909c3-a88d-474a-970d-7a5f51b1dbda.jpg</url>
      <title>Forem: Rufat Aliyev</title>
      <link>https://forem.com/rufataliy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rufataliy"/>
    <language>en</language>
    <item>
      <title>Testing React Applications That Use Context Global State </title>
      <dc:creator>Rufat Aliyev</dc:creator>
      <pubDate>Wed, 13 Jan 2021 04:22:21 +0000</pubDate>
      <link>https://forem.com/rufataliy/testing-react-applications-that-use-context-global-state-45n4</link>
      <guid>https://forem.com/rufataliy/testing-react-applications-that-use-context-global-state-45n4</guid>
      <description>&lt;p&gt;Isn't satisfying to see all your test are passing with all those green tags in your terminal. I want to share the way I test my React applications that use Context to manage global state. &lt;/p&gt;

&lt;p&gt;If you want to read how I use Context for global state management check out &lt;a href="https://dev.to/rufataliy/react-global-state-with-usecontext-3d4n"&gt;this post&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  General guidelines
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;“Program to an interface, not an implementation.”&lt;br&gt;
&lt;a href="https://www.amazon.com/gp/product/B000SEIBB8?ie=UTF8&amp;amp;camp=213733&amp;amp;creative=393177&amp;amp;creativeASIN=B000SEIBB8&amp;amp;linkCode=shr&amp;amp;tag=eejs-20&amp;amp;linkId=CSQYBHTUP625XI4T"&gt;Design Patterns: Elements of Reusable Object Oriented Software&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While my internship at &lt;a href="https://acerta.ai/"&gt;Acerta&lt;/a&gt; we needed to set up our testing environment and I was assigned to research current testing approaches. As a result, I found two main streams in testing React applications.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implementation oriented&lt;/li&gt;
&lt;li&gt;Result oriented&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implementation oriented
&lt;/h3&gt;

&lt;p&gt;If you are trying to test the internals of your component, like, is the state updating correctly, is the rendering happening, then you are doing implementation-oriented testing. The problem with this approach is that&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;your implementation could change while still rendering the same interface and functionalities. It means that every time you make changes to your component you will need to adjust your tests as well, which is not ideal. &lt;/li&gt;
&lt;li&gt;you will need to have more tests and mocks. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Maybe you think that there are times that some logic needs to be thoroughly tested. In that case, your logic is too complex to be hosted by a React component. Create a custom hook and implement your logic there and import them to your component. This will make your component lighter and your testing easier. &lt;/p&gt;

&lt;h3&gt;
  
  
  Result oriented
&lt;/h3&gt;

&lt;p&gt;Testing the result of your component is when you are testing your components closer to the way your users will interact with them. It means that you are not testing React rendered objects, but the real DOM. In this way, you will also test if your component is rendered at all, and if the elements that carry the main logic of the component are in the DOM, and if they behave correctly. The benefits of this approach is that&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you will have more robust tests that will be subject to less often changes&lt;/li&gt;
&lt;li&gt;you will test more with less code&lt;/li&gt;
&lt;li&gt;you will test in a way as your application will be interacted by users&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Mock API Requests
&lt;/h2&gt;

&lt;p&gt;Enough of philosophy, let's get started with coding.&lt;/p&gt;

&lt;p&gt;I usually use &lt;a href="https://mswjs.io/"&gt;msw&lt;/a&gt; to mock my API requests. I strongly recommend it for your development environment. MSW is using service workers to intercept your API requests, which means that you don't change the way you fetch data. Just your API responses will come not from a server but predefined handlers. &lt;/p&gt;

&lt;p&gt;It is very useful while doing testing. Because you can use it in the browser and node environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mock Global State
&lt;/h2&gt;

&lt;p&gt;Now that we are good with API requests, let's tackle the global state.&lt;/p&gt;

&lt;p&gt;As my components are using the global state directly, I need to mock it so that I can assert if the methods provided by the global state are being called properly.&lt;/p&gt;

&lt;p&gt;I start by mocking my store object and assign &lt;a href="https://jestjs.io/en/"&gt;Jest&lt;/a&gt; mock functions to all the methods that will be imported by the components I will test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const store: DefaultContext = {
  getRandomRecipes: jest.fn(),
  getRecipeById: jest.fn(),
  searchByName: jest.fn(),
  searchByCountry: jest.fn(),
  searchByCategory: jest.fn(),
  searchByIngredients: jest.fn(),
  resetReviewState: jest.fn(),
  setRecipeList: jest.fn(),
  loading: false,
  recipeList: null,
  reviewBarOpen: false,
  reviewLoading: false,
  reviewedRecipe: null,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step will be creating a mock &lt;code&gt;&amp;lt;StateProvider/&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { Context } from "@/store/Context";
import { store } from "./mockStore";

export const StateProvider: React.FC = ({ children }) =&amp;gt; {
  return &amp;lt;Context.Provider value={store}&amp;gt;{children}&amp;lt;/Context.Provider&amp;gt;;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see here, I use the same &lt;code&gt;Context&lt;/code&gt; element, but I pass my mock store to it as a value.&lt;/p&gt;

&lt;p&gt;Alright, and now let's finally do some testing.&lt;/p&gt;

&lt;p&gt;So the main technologies I use for testing are &lt;a href="https://jestjs.io/en/"&gt;Jest&lt;/a&gt; and &lt;a href="https://testing-library.com/"&gt;Testing-library&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Testing-library especially is created to encourage result-oriented testing. It provides you with utilities to render your component and deal with asynchronous methods in your components. It also provides &lt;code&gt;screen&lt;/code&gt; API which represents the rendered element and selectors like &lt;code&gt;getByText&lt;/code&gt;, &lt;code&gt;getByTestId&lt;/code&gt; and etc. &lt;/p&gt;

&lt;p&gt;I want to specially talk about &lt;code&gt;getByTestId&lt;/code&gt;. You can get elements from DOM in numerous ways and most cases, it can be valid. But if you think about making your tests more resilient to changes you don't want them to be dependant on tag decisions someone made or alt text or text content, and so on. These are the things that can be changed more often and sometimes you can't do even anything about it. That's why I recommend using the &lt;code&gt;data-testid&lt;/code&gt; property on your HTML tags. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One benefit is that no matter what you render as long as you have &lt;code&gt;data-testid&lt;/code&gt; on it your test will pass. &lt;/li&gt;
&lt;li&gt;Another advantage is that it will communicate to other developers that this particular element is linked to some tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's write some tests
&lt;/h2&gt;

&lt;p&gt;I am going to test &lt;code&gt;&amp;lt;ByCategory/&amp;gt;&lt;/code&gt; component from my &lt;a href="https://recippy.rufat.tech/"&gt;Recippy&lt;/a&gt; project. This component is responsible for fetching categories from the server, displaying tabs with categories, and searching recipes by the selected category. It looks like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pEwELvXA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5ejdh8t5hpdfj7k9m9wz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pEwELvXA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5ejdh8t5hpdfj7k9m9wz.gif" alt="bycategory"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So I will mimic this in my test. &lt;/p&gt;

&lt;p&gt;First, I start up my mock server. (msw)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe("ByName", () =&amp;gt; {
  server.listen();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I pick the method I want to run my assertion on. I use &lt;a href="https://jestjs.io/en/"&gt;Jest&lt;/a&gt; &lt;code&gt;spyOn&lt;/code&gt; method to reference the &lt;code&gt;searchByCategory&lt;/code&gt;method in the global state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  it("Should search by category", () =&amp;gt; {
    const spy = jest.spyOn(mockStore, "searchByCategory");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wrapping my element with my mock global state ...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    render(
        &amp;lt;StateProvider&amp;gt;
          &amp;lt;ByCategory /&amp;gt;
        &amp;lt;/StateProvider&amp;gt;
      );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Waiting for the loader to be unmounted. . .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    await waitForElementToBeRemoved(() =&amp;gt; screen.getByTestId(LOADER));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Selecting a tab . . .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   const tab = screen.getByTestId(CATEGORY + index);

   expect(tab.textContent).toBe(categoryNames[index].strCategory);

   fireEvent.click(tab);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Submitting the search . . .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const search_btn = screen.getByTestId(SEARCH_BTN);

    fireEvent.click(search_btn);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Asserting if my &lt;code&gt;searchByCategory&lt;/code&gt; method is called properly . . .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   expect(spy).toBeCalledTimes(1);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, I close the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  server.close();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. Nothing fancy. &lt;/p&gt;

&lt;p&gt;As you can see, I do the same thing as the user would do, but I test lots of things there. I test if I got a result from API if my loader was there and disappeared after the request is finalized if I had tabs to click, and finally if I can call API again to get my search result. &lt;/p&gt;

&lt;p&gt;As you can see this test covers most parts of the component. Of course, you can test use cases as well, but this is the case I am interested in.&lt;/p&gt;

&lt;p&gt;Finally, remember that tests are dangerous if not implemented correctly.&lt;/p&gt;

&lt;p&gt;You wanna know more about testing and Javascript development I would highly recommend following [Kent C. Dodds].&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>react</category>
      <category>testing</category>
      <category>globalstate</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Recipe App</title>
      <dc:creator>Rufat Aliyev</dc:creator>
      <pubDate>Sun, 13 Dec 2020 01:53:06 +0000</pubDate>
      <link>https://forem.com/rufataliy/recipe-app-229h</link>
      <guid>https://forem.com/rufataliy/recipe-app-229h</guid>
      <description>&lt;h2&gt;
  
  
  What I built
&lt;/h2&gt;

&lt;p&gt;I built a recipe app using NextJS and MaterialUI. The deployment was super smooth. Previously, I have used GCP and Heroku, honestly, the best deployment experience I have ever had was with DigitalOcean. No hustle at all. Good Job, guys!&lt;/p&gt;

&lt;h3&gt;
  
  
  Category Submission:
&lt;/h3&gt;

&lt;p&gt;Random Roulette&lt;/p&gt;

&lt;h3&gt;
  
  
  App Link
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://recippy-3cm9c.ondigitalocean.app/"&gt;https://recippy-3cm9c.ondigitalocean.app/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Screenshots
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4dYo6wN2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pyfmxfadbec70xob06b7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4dYo6wN2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pyfmxfadbec70xob06b7.png" alt="recippy-3cm9c.ondigitalocean.app_"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Description
&lt;/h3&gt;

&lt;p&gt;You can search recipes by name, filter them by ingredients, countries, and categories.&lt;/p&gt;

&lt;h3&gt;
  
  
  Link to Source Code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/rufataliy/Recippy"&gt;https://github.com/rufataliy/Recippy&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Permissive License
&lt;/h3&gt;

&lt;p&gt;MIT&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;I love cooking, so thought why not make an app that could be useful for me and others.&lt;/p&gt;

</description>
      <category>dohackathon</category>
    </item>
    <item>
      <title>React Global State with useContext</title>
      <dc:creator>Rufat Aliyev</dc:creator>
      <pubDate>Fri, 11 Dec 2020 22:21:30 +0000</pubDate>
      <link>https://forem.com/rufataliy/react-global-state-with-usecontext-3d4n</link>
      <guid>https://forem.com/rufataliy/react-global-state-with-usecontext-3d4n</guid>
      <description>&lt;p&gt;Imagine your global state API looks 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;const Index: React.FC = () =&amp;gt; {
  const { loading, recipeList, getRandomRecipes} = useStore();
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just one hook that provides all you need from a global state handler. This could be achieved by using &lt;code&gt;Context&lt;/code&gt; API natively provided by React 16.x.&lt;/p&gt;

&lt;p&gt;According to the documentation, Context API is for avoiding prop drilling which means passing a prop down to deeply nested components through all its parents. You read more about his &lt;a href="https://reactjs.org/docs/context.html#before-you-use-context"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We will leverage the concept of the hook of React to make the consuming process of context more developer-friendly.&lt;/p&gt;

&lt;p&gt;First and simple, we need to create a context. I usually create all files that relate to the global state inside the store folder that is in the src folder of 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;|__src
    |__components
    |__store
        |__Context.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code for context will 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;export const Context = React.createContext(defaultContext)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can omit &lt;code&gt;defaultContex&lt;/code&gt;, but it is good practice to use it in order to be able to isolate and test it.&lt;/p&gt;

&lt;p&gt;So, now we have created our context. Let move to the main part which is creating an actual global state. There is nothing fancy here just a simple custom hook with your states. I usually call it &lt;code&gt;useGlobalState&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;|__src
    |__components
    |__store
        |__Context.ts
        |__useGlobalState.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating the file we create the states that should be accessible from any component of our application and the methods to manipulate the state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useMemo } from "react";
import { makeApiRequest } from "../utils";

export const useGlobalState = () =&amp;gt; {
  const [recipeList, setRecipeList] = useState(null);
  const [reviewBarOpen, setReviewBarOpen] = useState(false);
  const [loading, setLoading] = useState(true);

  const searchByName = useMemo(
    () =&amp;gt; (keyword: string) =&amp;gt; {
      makeApiRequest(
        `/api/search-by?keyword=${keyword}`,
        (data) =&amp;gt; setRecipeList(data.meals),
        setLoading
      );
    },
    [setLoading]
  );

  const searchByIngredients = useMemo(
    () =&amp;gt; (ingredients: string) =&amp;gt; {
      makeApiRequest(
        `/api/filter-by?filterType=i&amp;amp;filterValue=${ingredients}`,
        (data) =&amp;gt; setRecipeList(data.meals),
        setLoading
      );
    },
    [setLoading]
  );

  const openReviewBar = useMemo(() =&amp;gt; () =&amp;gt; 
                          setReviewBarOpen(true), [
                          setReviewBarOpen,
                        ]);

  const closeReviewBar = useMemo(() =&amp;gt; () =&amp;gt; 
                          setReviewBarOpen(false), [
                          setReviewBarOpen,
                        ]);
  const resetReviewState = useCallback(() =&amp;gt; {
                            setReviewedRecipe(null);
                            closeReviewBar();
                           }, [closeReviewBar]);
  return {
    recipeList,
    searchByName,
    searchByIngredients,
    reviewBarOpen,
    resetReviewState,
  };
};

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

&lt;/div&gt;



&lt;p&gt;So, basically, what we are doing is exposing only those parts of the state and methods that should be accessible publically from child components.&lt;/p&gt;

&lt;p&gt;The next step is optional but make this solution more elegant. I create an additional provider component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|__src
    |__components
    |__store
        |__Context.ts
        |__useGlobalState.ts
        |__StateProvider.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { Context } from "./Context";
import { useGlobalState } from "./useGlobalState";

export const StateProvider: React.FC = ({ children }) =&amp;gt; {
  const store = useGlobalState();

  return (
     &amp;lt;Context.Provider value={store}&amp;gt;
        {children}
     &amp;lt;/Context.Provider&amp;gt;
  )
};

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

&lt;/div&gt;



&lt;p&gt;Next, I wrap my application to the &lt;code&gt;StateProvider&lt;/code&gt;, If not I cannot access global in children components.&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";

export const App= ({children})=&amp;gt;{
 return (
    &amp;lt;StateProvider&amp;gt;
      &amp;lt;Layout&amp;gt;
        {children}
      &amp;lt;/Layout&amp;gt;
    &amp;lt;/StateProvider&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, I implement a custom hook to consume the global state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|__src
    |__components
    |__store
        |__Context.ts
        |__useGlobalState.ts
        |__useStateProvider.ts
        |__useStore.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useContext } from "react";
import { Context } from "./Context";

export const useStore = () =&amp;gt; {
  const store = useContext(Context);
  return store;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it, our global state is ready to use. Now, you just need to call the hook and consume the provided API.&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, { useEffect } from "react";
import { useStore } from "@/store";

export const ByName: React.FC = () =&amp;gt; {
  const { searchByName, getRandomRecipes } = useStore();
  const [value, setValue] = useState("");

  useEffect(() =&amp;gt; {
    if (!Boolean(value.trim())) {
      getRandomRecipes();
    }
  }, [value, getRandomRecipes]);

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

&lt;/div&gt;



&lt;p&gt;As a result, This keeps your components clean, only one place to look for bugs regarding your global state, and, also,  isolates the data layer from the view layer which makes it easy to test this kind of application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;If you wonder how you would test your components that consume global state directly check out my other &lt;a href="https://dev.to/rufataliy/testing-react-applications-that-use-context-global-state-45n4"&gt;post&lt;/a&gt; where I walk you through process.&lt;br&gt;
Let me know what's your implementation of the global state.&lt;/p&gt;

&lt;p&gt;By the way, if you wanted to check the app where I implemented this style you can view it &lt;a href="https://recippy.rufat.tech/"&gt;here&lt;/a&gt; and source code &lt;a href="https://github.com/rufataliy/Recippy"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>react</category>
      <category>contextapi</category>
      <category>globalstate</category>
      <category>usecontext</category>
    </item>
  </channel>
</rss>
