<?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: Matti Salokangas</title>
    <description>The latest articles on DEV Community by Matti Salokangas (@sturdynut).</description>
    <link>https://dev.to/sturdynut</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%2F155774%2Fadd531aa-530f-4f6a-8caa-06d2a1c115e7.jpg</url>
      <title>DEV Community: Matti Salokangas</title>
      <link>https://dev.to/sturdynut</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sturdynut"/>
    <language>en</language>
    <item>
      <title>An Introduction to React</title>
      <dc:creator>Matti Salokangas</dc:creator>
      <pubDate>Fri, 12 Feb 2021 18:23:00 +0000</pubDate>
      <link>https://dev.to/sturdynut/an-introduction-to-react-1mng</link>
      <guid>https://dev.to/sturdynut/an-introduction-to-react-1mng</guid>
      <description>&lt;h2&gt;
  
  
  Starting Off
&lt;/h2&gt;

&lt;p&gt;If you aren't familiar with Javascript or want to make sure to fill in the gaps, read through this javascript primer from MDN: &lt;a href="https://developer.mozilla.org/en--US/docs/Web/JavaScript/A_re-introduction_to_JavaScript"&gt;Javascript Basics&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This content is inspired by the React &lt;a href="https://reactjs.org/docs/getting-started.html"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is React?
&lt;/h2&gt;

&lt;p&gt;At the highest level, React is a Javascript library that is the "View" in the MVC (model/view/controller) pattern.&lt;/p&gt;

&lt;p&gt;React components allow you to represent your application data in a tree of components.&lt;/p&gt;

&lt;p&gt;Your entire app could be a giant tree of components or you could have mini-component trees in various parts of your existing application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing Components
&lt;/h2&gt;

&lt;p&gt;Each component in your apps tree is written using a special extension to the javascript language called JSX.  JSX stands for Javascript and XML and once you get the hang of it, it is really nice!  The syntax can turn off some folks initially, but in time you get used to it.  Dive more into JSX in the docs &lt;a href="https://reactjs.org/docs/introducing-jsx.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What does it look like?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SomeJSXElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/SomeJSXElement&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to make this work, Facebook took advantage of a tool called &lt;a href="https://babeljs.io/"&gt;babel&lt;/a&gt; and created a plugin that transforms JSX into regular old javascript that a browser can understand.&lt;/p&gt;

&lt;p&gt;So, you write this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SomeJSXElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/SomeJSXElement&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And babel transforms it into this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;SomeJSXElement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is nothing stopping you from writing all your React using &lt;code&gt;React.createElement(...)&lt;/code&gt; however it will be painful and is generally not recommended.&lt;/p&gt;

&lt;h2&gt;
  
  
  Props vs State
&lt;/h2&gt;

&lt;p&gt;Components don't do much without having some sort of properties set or some local state.&lt;/p&gt;

&lt;p&gt;Here are some general rules when it comes to props vs state:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Props are passed down the component tree.&lt;/li&gt;
&lt;li&gt;State is localized to the component it is used in, but can also be passed down to child components as a prop.&lt;/li&gt;
&lt;li&gt;Global state is achieved through &lt;code&gt;Context&lt;/code&gt;, which we will discuss later.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Props&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the example below, "name" is considered a prop of the "MyComponent" component.&lt;/p&gt;

&lt;p&gt;It is passed to the component like so: &lt;code&gt;&amp;lt;MyComponent name="Sarah" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you want to update a prop, you'd typically also pass a function to your component that would provide the new value for the prop.  For example, &lt;code&gt;&amp;lt;MyComponent name="Jim" updateName={updateName} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;"favoriteColor" is considered state and is updated by calling the "setFavoriteColor" function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You might update it after clicking a button or typing into a textbox.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// This is using array destructing&lt;/span&gt;
  &lt;span class="c1"&gt;// The default value for "favoriteColor" will be "Green"&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;favoriteColor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFavoriteColor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Green&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Hi&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;  &lt;span class="nx"&gt;Your&lt;/span&gt; &lt;span class="nx"&gt;favorite&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;favoriteColor&lt;/span&gt;&lt;span class="p"&gt;}.&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defaultProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;propTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Designing Components
&lt;/h2&gt;

&lt;p&gt;When beginning to design a component or an entire page in React it is helpful to approach things using this technique:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Whiteboard or write up static HTML first&lt;/li&gt;
&lt;li&gt;Decide where it makes sense to extract your components given the UI&lt;/li&gt;
&lt;li&gt;Consider what props or state each component will need:

&lt;ul&gt;
&lt;li&gt;Pay attention to…&lt;/li&gt;
&lt;li&gt;Shared props

&lt;ul&gt;
&lt;li&gt;What props are needed across multiple components?

&lt;ul&gt;
&lt;li&gt;Does it make sense to keep track of these props in the parent component or is something that should be in context?  Usually parent component is the best place to start until you need it else where.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Local state

&lt;ul&gt;
&lt;li&gt;What can be localized to the component?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Create your components

&lt;ul&gt;
&lt;li&gt;Make sure to add your propTypes and defaultProps&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Read more about this strategy &lt;a href="https://reactjs.org/docs/thinking-in-react.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Function vs Class Components
&lt;/h2&gt;

&lt;p&gt;There are generally two approaches to writing components; function components or class components.  Both are valid ways to express React components.  &lt;/p&gt;

&lt;p&gt;Class components are a great way to learn, however there is a movement towards using Function components and hooks, so that should be the focus.  There are exceptions, such as &lt;code&gt;ErrorBoundary&lt;/code&gt; components that must be class based, but otherwise, it is recommended to use function components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Function Components
&lt;/h4&gt;

&lt;p&gt;There are a couple ways to express function components.  All of these are the same, just different ways of doing things.&lt;/p&gt;

&lt;p&gt;Using a fat-arrow function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyComponent = ({name}) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;Hello, {name}&amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can actually be even cleaner, which is sometimes why the fat-arrow function is preferred:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyComponent = ({name}) =&amp;gt; &amp;lt;div&amp;gt;Hello, {name}&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using a named function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function MyComponent({name}) {
  return (
    &amp;lt;div&amp;gt;Hello, {name}&amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Class Components
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyComponent extends React.Component {
  render() {
    return &amp;lt;h1&amp;gt;Hello, {this.props.name}&amp;lt;/h1&amp;gt;;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Lifting State Up
&lt;/h2&gt;

&lt;p&gt;No matter how much design you do up front, one will inevitably run into a scenario where more than one component needs access to some local state.&lt;/p&gt;

&lt;p&gt;In this case, you will want to lift your state up the component hierarchy so you can pass the state back down as "props".&lt;/p&gt;

&lt;p&gt;The React docs explain this quite well &lt;a href="https://reactjs.org/docs/lifting-state-up.html"&gt;here&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;There are times when you will need to access something deep down in the component tree and also at the top.  Rather than passing those props down through the component tree (called prop drilling), you can use "context".&lt;/p&gt;

&lt;p&gt;To use context...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You must first create it.&lt;/li&gt;
&lt;li&gt;Then you provide the context to your components by wrapping them in a "Provider"&lt;/li&gt;
&lt;li&gt;You then gain access to context by using the &lt;code&gt;useContext&lt;/code&gt; hook&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Read more &lt;a href="https://reactjs.org/docs/context.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create context
const MyContext = React.createContext();

// Provide it to your app using a Provider
const App = () =&amp;gt; {
  const [theme, updateTheme] = useState();

  return (
    &amp;lt;MyContext.Provider value={{ theme, updateTheme }}&amp;gt;
      &amp;lt;Container&amp;gt;
        &amp;lt;Header /&amp;gt;
        &amp;lt;Content /&amp;gt;
        &amp;lt;Footer /&amp;gt;
      &amp;lt;/Container&amp;gt;
    &amp;lt;/MyContext.Provider&amp;gt;
  )
}

// Then, let's say down in your &amp;lt;Content /&amp;gt; component you want
// to access the theme:

const Content = () =&amp;gt; {
  const { theme } = useContext(MyContext);

  return (
     &amp;lt;div&amp;gt;
       You are using the {theme} theme!
     &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Hooks
&lt;/h2&gt;

&lt;p&gt;A fairly recent update to React was the introduction of hooks.  These are simply special functions that start with "use", abide by some rules and allow you to do things in function components that were previously achieved via higher order components or class components.&lt;/p&gt;

&lt;p&gt;Let's take a look at a few common hooks.  If you are eager to dive in, check out the docs &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  useState
&lt;/h3&gt;

&lt;p&gt;Any time you want to keep track of something that you will be changing over time and want to tell React to re-render when it changes, you'll use the &lt;code&gt;useState&lt;/code&gt; hook.&lt;/p&gt;

&lt;p&gt;Read more &lt;a href="https://reactjs.org/docs/hooks-state.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [name, setName] = useState("Jim")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  useEffect
&lt;/h3&gt;

&lt;p&gt;Any time there is some sort of side effect, you'll want to use a &lt;code&gt;useEffect&lt;/code&gt; hook.&lt;/p&gt;

&lt;p&gt;Read more &lt;a href="https://reactjs.org/docs/hooks-effect.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This will run every render
useEffect(() =&amp;gt; {
  console.log("I ran!")
})

// This will run every time the "loading" prop changes
useEffect(() =&amp;gt; {
  console.log(loading)
}, [loading])

// This will run every time the "loading" prop changes
// and will also run a function returned from the useEffect
// hook when this component is removed from the DOM
useEffect(() =&amp;gt; {
  console.log(loading)

  return () =&amp;gt; {
    console.log("This is where you can run clean up code!")
  }
}, [loading])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  useLayoutEffect
&lt;/h3&gt;

&lt;p&gt;Works the same as &lt;code&gt;useEffect&lt;/code&gt;, but you'll want to use this if you are doing any sort of measuring of style-related things with the DOM.  Essentially, if you find yourself accessing HTML elements properties like height or width, you will want to use the &lt;code&gt;useLayoutEffect&lt;/code&gt; hook.&lt;/p&gt;

&lt;p&gt;Read more &lt;a href="https://reactjs.org/docs/hooks-reference.html#uselayouteffect"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  useRef
&lt;/h3&gt;

&lt;p&gt;Allows you to keep track of something for the lifetime of your component.   This is often used to access the native DOM element associated to some JSX element.&lt;/p&gt;

&lt;p&gt;Read more &lt;a href="https://reactjs.org/docs/hooks-reference.html#useref"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyComponent = () =&amp;gt; {
  const inputRef = useRef(null);

  const doIt = () =&amp;gt; {
    // Output the background color for the div
    console.log(inputRef.current.style.backgroundColor)
  }

  return (&amp;lt;&amp;gt;
    &amp;lt;div ref={inputRef} style={{backgroundColor: "#123"}}&amp;gt;Hello World&amp;lt;/div&amp;gt;
    &amp;lt;button onClick={doIt}&amp;gt;Click Me&amp;lt;/button&amp;gt;
  &amp;lt;/&amp;gt;)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Rules of Hooks
&lt;/h2&gt;

&lt;p&gt;There only a couple rules one must abide by in order for Hooks to work in the React world.&lt;/p&gt;

&lt;p&gt;1) Hooks must be first&lt;br&gt;
2) Hooks can only be using in function components or other hooks&lt;/p&gt;

&lt;p&gt;Read more &lt;a href="https://reactjs.org/docs/hooks-rules.html"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Render and Portals
&lt;/h2&gt;

&lt;p&gt;There are a couple ways to render React components.  The most common way in the web-world is to call &lt;code&gt;react-dom&lt;/code&gt;'s &lt;code&gt;render&lt;/code&gt; method.  The other way is by using a &lt;code&gt;portal&lt;/code&gt;, which allows you to basically inject a React component anywhere in your site.&lt;/p&gt;

&lt;p&gt;Sometimes you aren't working with a 100% React application.  For example, if you are working on a WordPress site where you want to use React for a dropdown menu at the top of the page and a specialized store locator tool.  This is where you'd want to lean on React's &lt;code&gt;portal&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Read more &lt;a href="https://reactjs.org/docs/portals.html#gatsby-focus-wrapper"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Mapping Data and Keys
&lt;/h2&gt;

&lt;p&gt;Often times you will be displaying a list of things.  If you find yourself using &lt;code&gt;map&lt;/code&gt;, make sure to provide each item with a &lt;code&gt;key&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Read more &lt;a href="https://reactjs.org/docs/lists-and-keys.html#keys"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data.map(item =&amp;gt; &amp;lt;li key={item.id}&amp;gt;{item.label}&amp;lt;/li&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Odd Html Attributes
&lt;/h2&gt;

&lt;p&gt;One oddity of React is that they had to slightly adjust some HTML attributes in order to have it jive with the ones already established for native HTML elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some examples:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rather than&lt;code&gt;&amp;lt;div class=...&lt;/code&gt;, you'd use &lt;code&gt;&amp;lt;div className=...&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Rather than&lt;code&gt;&amp;lt;button onclick=...&lt;/code&gt;, you'd use &lt;code&gt;&amp;lt;button onClick=...&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Rather than&lt;code&gt;&amp;lt;label for=...&lt;/code&gt;, you'd use &lt;code&gt;&amp;lt;label htmlFor=...&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Be on the look out for these!  There are only so many and if you do happen to make a mistake, React is pretty good about yelling at you in the console.  So, be sure to pay attention to those console warnings!&lt;/p&gt;

&lt;h2&gt;
  
  
  More Advanced Topics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://reactjs.org/tutorial/tutorial.html"&gt;Intro to React Tutorial&lt;/a&gt; - This React tutorial will help solidify what we covered above.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://reactjs.org/docs/hello-world.html"&gt;Explore more React&lt;/a&gt; - There are many other aspects of React not mentioned above, so if you would like to explore other areas, feel free to peruse.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://create-react-app.dev/docs/getting-started"&gt;Create React App&lt;/a&gt; - This is the defacto standard for creating new React apps.  This is where you want to start if you want to quickly get going with your very own React app.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://react-redux.js.org/introduction/basic-tutorial"&gt;Redux&lt;/a&gt; - This is a common library used to keep track of your application state in "stores".  It is an essential ingredient in many React apps and although you may not need to use redux all the time, it is important to understand the concepts of reducers, stores and actions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Frameworks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://nextjs.org/"&gt;NextJS&lt;/a&gt; - React is considered a library, where as Next.js is considered a framework.  There are many rules and conventions that Next.js has adopted and although it is overwhelming to try and just learn Next.js, it is helpful to at least be aware of it and know when you might want to use it.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.gatsbyjs.com/"&gt;GatsbyJS&lt;/a&gt; - Another React framework for building static web sites.  This is great for blogs and many other sites.  The learning curve here is steep, similar to Next.JS, so take it slow and this is another to be aware of in the case where it makes sense for you to use.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://remix.run/"&gt;Remix&lt;/a&gt; - Yet another React framework.  This framework does cost money to use, but if you are looking to get going fast with a robust framework, this may be it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Originally posted on ZEAL's blog &lt;a href="https://www.codingzeal.com/post/an-introduction-to-react"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Cleaner React: Conditional Rendering</title>
      <dc:creator>Matti Salokangas</dc:creator>
      <pubDate>Fri, 25 Sep 2020 17:10:47 +0000</pubDate>
      <link>https://dev.to/sturdynut/cleaner-react-conditional-rendering-h34</link>
      <guid>https://dev.to/sturdynut/cleaner-react-conditional-rendering-h34</guid>
      <description>&lt;p&gt;Often times React components become hard to understand due to conditional rendering.  At first a simple if/else or ternary operator appears benign to your overall readability, but over time as changes occur another if/else or ternary may be added.&lt;/p&gt;

&lt;p&gt;Compounding this issue is when conditional operators are nested many times, which unfortunately is too easy to do.&lt;/p&gt;

&lt;p&gt;Let's first look at how to conditionally render in React and then dive into several experiments that might present more readable ways to conditionally render in React.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conditional Rendering Styles
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Simple Scenario
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Scenario:&lt;/em&gt; Show a login component vs a register component given the property "isLoggedIn"&lt;/p&gt;

&lt;h4&gt;
  
  
  Using &amp;amp;&amp;amp;
&lt;/h4&gt;

&lt;p&gt;Used quite often the "&amp;amp;&amp;amp;" is easy to throw in for some quick conditional logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Session = ({ isLoggedIn }) =&amp;gt; {  
 return (  
   &amp;lt;&amp;gt;  
     {isLoggedIn &amp;amp;&amp;amp; &amp;lt;Login /&amp;gt;}  
     {!isLoggedIn &amp;amp;&amp;amp; &amp;lt;SignOut /&amp;gt;}  
   &amp;lt;/&amp;gt;  
 );  
};  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using If/Else Statements
&lt;/h4&gt;

&lt;p&gt;Given this simple scenario, a guard clause works here and is a bit more readable than the "&amp;amp;&amp;amp;".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Session = ({ isLoggedIn }) =&amp;gt; {  
 if (isLoggedIn) {  
   return &amp;lt;SignOut /&amp;gt;  
 }  

 return &amp;lt;Login /&amp;gt;  
};  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using Ternary Operator
&lt;/h4&gt;

&lt;p&gt;This also is easier to understand; being able to one line this is pretty nice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const Session = ({ isLoggedIn }) =&amp;gt; isLoggedIn ? &amp;lt;SignOut /&amp;gt; : &amp;lt;Login /&amp;gt;;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Complex Scenario
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Scenario:&lt;/em&gt; Show a login component vs a register component given the property "isLoggedIn", in addition show the "UnicornLogin" component if the "isUnicorn" flag is true.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using &amp;amp;&amp;amp;
&lt;/h4&gt;

&lt;p&gt;This is awful. It is clear that the "&amp;amp;&amp;amp;" is only good to use sparingly and only when there is one condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Session = ({ isLoggedIn, isUnicorn }) =&amp;gt; {  
 &amp;lt;&amp;gt;  
   {isLoggedIn &amp;amp;&amp;amp; !isUnicorn &amp;amp;&amp;amp; &amp;lt;Login /&amp;gt;}  
   {!isLoggedIn &amp;amp;&amp;amp; isUnicorn &amp;amp;&amp;amp; &amp;lt;isUnicorn /&amp;gt;}  
   {!isLoggedIn &amp;amp;&amp;amp; !isUnicorn &amp;amp;&amp;amp; &amp;lt;SignOut /&amp;gt;}  
 &amp;lt;/&amp;gt;;  
};  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using If/Else Statements
&lt;/h4&gt;

&lt;p&gt;Less awful, but this is going to make things tricky if you ever wanted to wrap each of the components being returned with another component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Session = ({ isLoggedIn, isUnicorn }) =&amp;gt; {  
 if (isLoggedIn) {  
   return &amp;lt;SignOut /&amp;gt;;  
 } else if (isUnicorn) {  
   return &amp;lt;UnicornLogin /&amp;gt;;  
 };

 return &amp;lt;Login /&amp;gt;;  
};  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using Ternary Operator
&lt;/h4&gt;

&lt;p&gt;Also, less awful, yet prone to the same struggles as using if/else statements when more changes inevitably occur.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Session = ({ isLoggedIn, isUnicorn }) =&amp;gt; {  
 if (isLoggedIn) {  
   return &amp;lt;SignOut /&amp;gt;;  
 }

 return isUnicorn ? &amp;lt;UnicornLogin /&amp;gt; : &amp;lt;Login /&amp;gt;;  
};  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conditional Gotchas
&lt;/h2&gt;

&lt;p&gt;Now that we've seen how to use conditional rendering in React, let's take a look at some specific examples where conditional rendering may not do what you expect.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logical "&amp;amp;&amp;amp;" Operator
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Rendering a 0 when mapping
&lt;/h4&gt;

&lt;p&gt;When using "&amp;amp;&amp;amp;" to check the length of a data set and then mapping over it will render "0" when that list is empty.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This was recently highlighted by Kent C. Dodds in his article&lt;/em&gt; &lt;a href="https://kentcdodds.com/blog/use-ternaries-rather-than-and-and-in-jsx"&gt;&lt;em&gt;https://kentcdodds.com/blog/use-ternaries-rather-than-and-and-in-jsx&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code below will render "0" when data is empty.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const RenderData = ({ data }) =&amp;gt;  data.length &amp;amp;&amp;amp; data.map(...);  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be resolved by either using a ternary operator instead of "&amp;amp;&amp;amp;".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const RenderData = ({ data }) =&amp;gt;  data.length &amp;gt; 0 ? data.map(...) : null;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can also be resolved by either using an if/else statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const RenderData = ({ data }) =&amp;gt;  data.length &amp;gt; 0 ? data.map(.const RenderData = ({ data }) =&amp;gt; {  
 if (data.length === 0) return null;

 return data.map(...)  
}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Rendering a 0 in a  component
&lt;/h4&gt;

&lt;p&gt;This React Native specific, but when conditionally rendering a  component and passing in your condition you may inadvertently render a 0. This results in your app crashing and the following error message: "Invariant Violation: Text strings must be rendered within a  component.".&lt;/p&gt;

&lt;p&gt;This will crash your app if "message" is ever 0:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;message &amp;amp;&amp;amp; &amp;lt;Text&amp;gt;{message}&amp;lt;/Text&amp;gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Nested Ternaries
&lt;/h3&gt;

&lt;p&gt;Ternaries are nice if you have one condition. However, it is too easy to not refactor and quickly add another check and then another.&lt;/p&gt;

&lt;p&gt;This is a simple example, you can imagine what this would look like if each component we rendered were 5-10 or more lines long.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const RenderData = ({ data }) =&amp;gt; {  
 return data.length === 0 ? null  
   : data.length === 1  
   ? &amp;lt;SingleItem data={data} /&amp;gt;  
   : data.length === 2  
   ? &amp;lt;DoubleItem data={data} /&amp;gt;  
   : &amp;lt;MultiItem data={data} /&amp;gt;  
}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Writing Better Conditions
&lt;/h2&gt;

&lt;p&gt;We've taken a look at how to write basic conditional statements in React, along with some pitfalls to avoid. Let's consider how we can write better conditional code in React.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditional Operator Component
&lt;/h3&gt;

&lt;p&gt;I think it is easier to read JSX if your brain only has to parse  and not conditional statements plus . So, how can we write conditional operators as XML?&lt;/p&gt;

&lt;p&gt;Let's consider creating a component called "RenderIf" and takes a boolean property of "isTrue" and renders its children.&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 RenderIf = ({ children, isTrue }) =&amp;gt; isTrue ? children : null;

RenderIf.propTypes = {  
 children: node.isRequired,  
 isTrue: bool.isRequired,  
};  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Re-writing our example with the "RenderIf" component, I'm mostly looking at XML. However, there is still some boolean logic that could be cleaned up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const RenderData = ({ data }) =&amp;gt; {  
 return (  
   &amp;lt;&amp;gt;  
     &amp;lt;RenderIf isTrue={data.length === 1}&amp;gt;  
       &amp;lt;SingleItem data={data} /&amp;gt;  
     &amp;lt;/RenderIf&amp;gt;  
     &amp;lt;RenderIf isTrue={data.length === 2}&amp;gt;  
       &amp;lt;DoubleItem data={data} /&amp;gt;  
     &amp;lt;/RenderIf&amp;gt;  
     &amp;lt;RenderIf isTrue={data.length &amp;gt; 2}&amp;gt;  
       &amp;lt;MultiItem data={data} /&amp;gt;  
     &amp;lt;/RenderIf&amp;gt;  
   &amp;lt;/&amp;gt;  
 );  
}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can clean up the boolean logic by wrapping our "RenderIf" component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const IfSingleItem = ({ children, data }) =&amp;gt; &amp;lt;RenderIf isTrue={data.length === 1}&amp;gt;{children}&amp;lt;/RenderIf&amp;gt;  
const IfDoubleItem = ({ children, data }) =&amp;gt; &amp;lt;RenderIf isTrue={data.length === 2}&amp;gt;{children}&amp;lt;/RenderIf&amp;gt;  
const IfMultiItem = ({ children, data }) =&amp;gt; &amp;lt;RenderIf isTrue={data.length &amp;gt; 3}&amp;gt;{children}&amp;lt;/RenderIf&amp;gt;

const RenderData = ({ data }) =&amp;gt; {  
 return (  
   &amp;lt;&amp;gt;  
     &amp;lt;IfSingleItem data={data}&amp;gt;  
       &amp;lt;SingleItem data={data} /&amp;gt;  
     &amp;lt;/IfSingleItem&amp;gt;  
     &amp;lt;IfDoubleItem data={data}&amp;gt;  
       &amp;lt;DoubleItem data={data} /&amp;gt;  
     &amp;lt;/IfDoubleItem&amp;gt;  
     &amp;lt;IfMultiItem data={data}&amp;gt;  
       &amp;lt;MultiItem data={data} /&amp;gt;  
     &amp;lt;/IfMultiItem&amp;gt;  
   &amp;lt;/&amp;gt;  
 );  
}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Having our Cake and Eating it too!
&lt;/h3&gt;

&lt;p&gt;I personally like reading more declarative React, however one of the pitfalls that I had not mentioned is that the children for the RenderIf component will still go through a render cycle even if our condition is false. This is because RenderIf is still JSX vs being straight javascript. &lt;/p&gt;

&lt;p&gt;So, how do we get around this?&lt;/p&gt;

&lt;p&gt;I happen to write a &lt;code&gt;RenderIf&lt;/code&gt; Babel plugin that does just this! You can find the code out on my GitHub &lt;a href="https://github.com/sturdynut/babel-plugin-render-if"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Essentially, this plugin will take code that 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;&amp;lt;RenderIf isTrue={someCondition}&amp;gt;
  &amp;lt;span&amp;gt;I am the children&amp;lt;/span&amp;gt;
&amp;lt;/RenderIf&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and turn it into this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{someCondition ? &amp;lt;span&amp;gt;I am the children&amp;lt;/span&amp;gt; : null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, we are getting our declarative syntax and when it is transpiled, we get the more performant version.  Also, if you use this plugin you won't have to write your own RenderIf component! 🎉&lt;/p&gt;

&lt;h3&gt;
  
  
  When to Refactor
&lt;/h3&gt;

&lt;p&gt;Often times if there is an accumulation of complexity in a component it is a sign that there are components that should be refactored out. Although knowing exactly when and how to refactor is hard to know, here are some general rules of thumb that you might consider.&lt;/p&gt;

&lt;h4&gt;
  
  
  100+ Lines of Code
&lt;/h4&gt;

&lt;p&gt;Keep components to less than 100 lines. As you start to get into the 100-250 line territory you should really start thinking about refactoring. If you are at 500+ lines of code, that should be refactored as soon as possible.&lt;/p&gt;

&lt;h4&gt;
  
  
  High Cyclomatic Complexity
&lt;/h4&gt;

&lt;p&gt;Cyclomatic complexity is the number of paths through your code. So, if you have a simple if/else block, then it has a cyclomatic complexity of 2, where as if you had a block of if/else if/else if/else if/else, the cyclomatic complexity would be 5.&lt;/p&gt;

&lt;p&gt;You can enforce this by using the &lt;a href="https://eslint.org/docs/rules/complexity"&gt;ESLint complexity rule&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is up to you what level of complexity is appropriate, but somewhere around 4-5 is usually a good place to start.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Cleaner React
&lt;/h3&gt;

&lt;p&gt;We can write cleaner React by extracting out distracting syntax and knowing when to refactor.&lt;/p&gt;

&lt;p&gt;Creating a helper component like "RenderIf" is an example in how you could extract out conditional logic into a declarative syntax. This makes it a little bit easier for your brain since it is mostly taking in XML. Building on that idea, we can wrap our helper component to create a richer set of conditional components that add even more context.&lt;/p&gt;

&lt;p&gt;At the end of the day, a component that is large and complex, no matter how clean the React is, will be prone to bugs and simply not fun to work on. It is a good practice to know when to refactor and to be disciplined to do that refactor when you know it needs to happen.&lt;/p&gt;

&lt;p&gt;Happy coding and keep your React clean!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally posted on ZEAL's blog &lt;a href="https://www.codingzeal.com/post/cleaner-react-conditional-rendering"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;br&gt;
‍&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Coding without Ego</title>
      <dc:creator>Matti Salokangas</dc:creator>
      <pubDate>Sun, 01 Mar 2020 19:03:31 +0000</pubDate>
      <link>https://dev.to/sturdynut/coding-without-ego-503k</link>
      <guid>https://dev.to/sturdynut/coding-without-ego-503k</guid>
      <description>&lt;p&gt;I think an often overlooked quality that makes a software developer "senior" is that they don't feel like they need to defend their code, that they are open to changing directions and to doing it another way.&lt;/p&gt;

&lt;p&gt;However, I also think there are "senior" developers out there that simply want to do it their way and don't care what others think.  That is ok too; just not great if you want to help others grow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ego
&lt;/h2&gt;

&lt;p&gt;What is the ego?  You ask google and it will tell you "a person's sense of self-esteem or self-importance.".  It makes me think that if I am not defending my self-importance that I might just let others run the show.  This "defense" can be distilled down to one's own pride.  We are often told to take "pride" in our work, but no one really said to take &lt;em&gt;some&lt;/em&gt; pride, but be open to feedback because it probably could be improved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pride
&lt;/h2&gt;

&lt;p&gt;I remember taking so much pride in my code that if someone said anything about it, I would take it personally.  I think anyone who is a programmer knows what I am talking about.  Well, there's no getting around it and it is just part of the journey!  Often times I think a new developer is so eager that she or he works relentlessly to learn all of the things...I mean the learning tracks nowadays are nuts!  It's no wonder so many of us get so burned out and then have to deal with  imposter syndrome. 😩😩😩&lt;/p&gt;

&lt;h2&gt;
  
  
  Pairing
&lt;/h2&gt;

&lt;p&gt;Given today's climate, it is very difficult to feel comfortable.  This was why I wrote about &lt;a href="https://sturdynut.com/2019/05/14/staying-uncomfortable/"&gt;staying uncomfortable&lt;/a&gt;; you simply cannot keep up and might as well just accept it.&lt;/p&gt;

&lt;p&gt;Working at &lt;a href="https://codingzeal.com"&gt;Zeal&lt;/a&gt; we pair program a lot.  Sure, there's a lot of debate about this; some do not like pairing others do.  If it isn't your thing, no big deal, but I think we can all take something away from pairing that has nothing to do with writing code and getting a product shipped.&lt;/p&gt;

&lt;p&gt;I remember my first pairing experience.  It was kinda like all of sudden I was on a stage and was live coding, which back  then was scary for me.  Thankfully the folks at Zeal were kind to me and showed me the way with kindness and thoughtfulness.  It was an experience that set the tone for me and how I approach pairing.&lt;/p&gt;

&lt;p&gt;Simply put, I code (and pair) without ego. I do not identify with my code.  The things that I do try to identify with are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Readability&lt;/li&gt;
&lt;li&gt;Maintainability&lt;/li&gt;
&lt;li&gt;Testability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These 3 things are more often than not the most important things for me as I am writing code.  All of these are selfless and require that I do things that might be uncomfortable.  A refactor that I just don't feel like doing or oftentimes listening to my pair and realizing that I am wrong.&lt;/p&gt;

&lt;p&gt;Nobody loves brown field code, but it can better if we write code that is easier to read, update and test.  That is indisputable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Empathy
&lt;/h2&gt;

&lt;p&gt;I do believe that empathy is gaining more and more momentum as a fundamental skill for a software developer.  But, how do&lt;br&gt;
you teach this?  Well, for starters let's ask google again what this is: "the ability to understand and share the feelings of another."  Pretty straight forward definition, but in practice, we have all these hurdles in our mind.  The biggest is our own ego.  Ego is nearly the antithesis of empathy.&lt;/p&gt;

&lt;p&gt;It certainly is good to feel good about the work you do and you should!  However, this doesn't help others feel good.    This is where empathy comes in and is equally important to be aware of as ego.  When pairing empathy is paramount, especially if you are in a position of seniority, it can be quite threatening to your pair.  In these scenarios your collective result will be colored by the subtle tension between you and your pair; the code will simply not be as good as it could have been.&lt;/p&gt;

&lt;p&gt;When pairing, ask yourself (&lt;em&gt;be honest&lt;/em&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Am I resisting input from my pair?  &lt;em&gt;If so, ask why?  Probably not a good reason...&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Is there a sense of flow?  &lt;em&gt;If not, where is the resistance?&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Where are my reactions coming from?  &lt;em&gt;Am I reacting out of compassion or fear?&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are many questions you can ask yourself, but the point is to become aware.  Pairing can be a humbling experience for many.  It can be a bit painful on bad days, but on good days it can be a greater experience than coding solo.  It can be even better when ego and empathy are balanced.  The key is awareness.&lt;/p&gt;

&lt;p&gt;Be aware of how your pair is feeling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Be Better than Yesterday
&lt;/h2&gt;

&lt;p&gt;It is our ego that makes us feel like an imposter or that we didn't do a "good enough" job.  It is a hollow feeling and&lt;br&gt;
sucks for sure.  However, this is a lie; we did do a "good enough" job and we are not an imposter.  At least that is what&lt;br&gt;
our best friend would tell us.  But, this isn't what we tell ourselves, we are our own worst enemy unless we become aware of it.  So, pay attention to what is going on in your mind, specifically that inner dialog.  Simply through awareness, you can improve your own world and make today a little better.&lt;/p&gt;

&lt;p&gt;Paying attention to your thoughts helps, but only goes so far.  Not feeling bad is better than feeling bad.  But, feeling good is certainly better than not feeling bad.  It is a funny thing that when we help others we get a sense of fulfillment.  It is like free happy points.  Honestly, give it a try.  Go out of your way, do something unexpected for a stranger that simply makes their day.  You will have a rush of feel-good neurotransmitters and your overall sense of well being will be much higher!&lt;/p&gt;

&lt;h2&gt;
  
  
  It is the Balance
&lt;/h2&gt;

&lt;p&gt;To bring it together, I believe we can all write better code by balancing our ego with empathy.  Be proud of whatever you do, because you should feel good about yourself.  But, at the same time, you should feel that same sense of pride in others as you acknowledge their triumphs.&lt;/p&gt;

&lt;p&gt;The balance is tricky, but essentially be selfish, but also be selfless.&lt;/p&gt;

&lt;p&gt;See original post &lt;a href="https://sturdynut.com/2020/03/01/coding-without-ego"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ego</category>
      <category>empathy</category>
      <category>coding</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
