<?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: omgpiu</title>
    <description>The latest articles on DEV Community by omgpiu (@omgpiu).</description>
    <link>https://dev.to/omgpiu</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%2F849291%2F2fa24d1b-33de-4924-aaa9-03cfa12d1a59.jpeg</url>
      <title>DEV Community: omgpiu</title>
      <link>https://dev.to/omgpiu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omgpiu"/>
    <language>en</language>
    <item>
      <title>Effector in four articles.</title>
      <dc:creator>omgpiu</dc:creator>
      <pubDate>Mon, 05 Sep 2022 18:22:58 +0000</pubDate>
      <link>https://dev.to/omgpiu/effector-in-four-articles-5cn1</link>
      <guid>https://dev.to/omgpiu/effector-in-four-articles-5cn1</guid>
      <description>&lt;p&gt;About 1 year ago I met effector - a new state manager for me. &lt;br&gt;
It was a medium sized project for a large company to evaluate employees with many forms and activities. &lt;br&gt;
I heard a little about this tool. Also watched a video on Youtube ( there was only one ). That was't enough.&lt;br&gt;
I remember my struggling with Effector. I couldn't understand what’s going on,  a lot of new words such as sample, clock , source , event, effect…. &lt;/p&gt;

&lt;p&gt;Everything changed when I saw the video with Sergey Sova. There were many interesting hints, but the main thing is that the effector is a flow.  Flow actions and data. Change your mind from redux paradigm, forget it for a while.  &lt;/p&gt;

&lt;p&gt;I &lt;strong&gt;imagined&lt;/strong&gt; that effector is a road, and said my self -  work with effector like a road builder makes a road. Each road is  one line of effector actions. &lt;/p&gt;

&lt;p&gt;Traffic light like an event, crossroad like a sample, parking like a store, etc.&lt;br&gt;
From that time I didn’t have any problems with this amazing tool. &lt;br&gt;
I hope, my  &lt;strong&gt;article series will help you to start or even try it out.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No more counters and TODO.&lt;/strong&gt;&lt;br&gt;
I will show four lessons. Start from scratch to medium level logic. &lt;/p&gt;

&lt;p&gt;All lessons you can find in my &lt;a href="https://github.com/omgpiu/effector-article"&gt;repo.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's begin. &lt;/p&gt;
&lt;h2&gt;
  
  
  Events and stores in simple way
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Before we get into it
&lt;/h3&gt;

&lt;p&gt;There are few things you better look throw in effector and patronum docs :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://effector.dev/docs/api/effector/event"&gt;Events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://effector.dev/docs/api/effector/store"&gt;Stores&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/effector/patronum"&gt;Patronum&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's start our journey with a small component. Usually for similar logic you better use hooks, but for learning stuff we are going to use effector instead.&lt;/p&gt;
&lt;h3&gt;
  
  
  Task
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create a component with stars.&lt;/li&gt;
&lt;li&gt;Stars must change their color when we hover them.&lt;/li&gt;
&lt;li&gt;Stars must change their color according chosen rating.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  First step
&lt;/h4&gt;

&lt;p&gt;Here we create a store by createStore for hoveredRating and give an initial value for it.&lt;br&gt;
Best practice is to start store's name with $ sign.&lt;br&gt;
We can add types for store value or store shape.&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 $hover = createStore&amp;lt;number&amp;gt;(0, {
  name: 'hover'
})

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Second step
&lt;/h4&gt;

&lt;p&gt;Now it's time to create an event by createEvent. Simply speaking, event is a function with or without payload. Events work as triggers to start chain of actions. TS will help us to understand do we need payload or not. I gave a payload as a number and now, when I'm going to use this event, it's waiting for payload as a number.&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 setHoveredRating = createEvent&amp;lt;number&amp;gt;()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Third step
&lt;/h4&gt;

&lt;p&gt;This is magic time - connect our event to store. Let's try to understand - what's going on here?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$hover.on(setHoveredRating, (_, rating) =&amp;gt; rating)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Connected our $hover (store) with setHoveredRating (event), and when we fire event our store will change.&lt;/li&gt;
&lt;li&gt;Pass callback as second argument in .on method.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This callback has two arguments - state and event payload. If we don't need store data, we name first argument as underscore ('_')&lt;/p&gt;

&lt;p&gt;When we call setHoveredRating with some payload, our store will change.&lt;br&gt;
You can do any logic, but remember our callback MUST BE A PURE FUNCTION.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Must to know&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you trigger any event with the same payload &lt;br&gt;
(oldState === payload from event), state will NOT change and will not UPDATE.&lt;br&gt;
If you pass undefined to store, store will NOT change and will not UPDATE.&lt;/p&gt;

&lt;p&gt;What we've done?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Created a store&lt;/li&gt;
&lt;li&gt;Created an event&lt;/li&gt;
&lt;li&gt;Connect the event with the store.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the next lesson we are going to work with API, use tool instead of useEffect etc. &lt;/p&gt;

&lt;p&gt;See you in next the article.  &lt;/p&gt;

</description>
      <category>effector</category>
      <category>tutorial</category>
      <category>react</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
