<?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: Andreas Reiterer</title>
    <description>The latest articles on DEV Community by Andreas Reiterer (@a_reiterer).</description>
    <link>https://dev.to/a_reiterer</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%2F1959%2Fe10936ac-0aaf-4441-919f-27b60cd80cc8.JPG</url>
      <title>DEV Community: Andreas Reiterer</title>
      <link>https://dev.to/a_reiterer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/a_reiterer"/>
    <language>en</language>
    <item>
      <title>How to fix the Whitescreen After a Static Deployment with create-react-app</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Tue, 13 Nov 2018 19:25:50 +0000</pubDate>
      <link>https://dev.to/a_reiterer/how-to-fix-the-whitescreen-after-a-static-deployment-with-create-react-app-2f53</link>
      <guid>https://dev.to/a_reiterer/how-to-fix-the-whitescreen-after-a-static-deployment-with-create-react-app-2f53</guid>
      <description>

&lt;p&gt;It’s one of the most annoying situations after deploying a static React app: After all that work, you finally deployed your app for production. But as you visit the site – instead of your freshly deployed app – you see … nothing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A blank, white, screen.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The reason for this could be practically everything. For example, a critical error somewhere in your JavaScript, who knows?&lt;/p&gt;

&lt;p&gt;To find out what’s wrong, it’s always a good idea to open your browser’s console and check it for error messages.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Loading failed for the &amp;lt;script&amp;gt; with source ...&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A very common thing you might see there in a “white-screen-situation”, is that your app failed to load the JavaScript bundle – and that’s what we’re going to fix today.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D_oDr4LR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/www.andreasreiterer.at/wp-content/uploads/2018/11/mainjsnotfound.jpg%3Fresize%3D660%252C84%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D_oDr4LR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i0.wp.com/www.andreasreiterer.at/wp-content/uploads/2018/11/mainjsnotfound.jpg%3Fresize%3D660%252C84%26ssl%3D1" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let’s have a look where the files actually live. In my case, I put them into a sub-directory &lt;em&gt;react-app&lt;/em&gt;, so the correct URL is &lt;em&gt;&lt;a href="https://www.andreasreiterer.at/react-app/static/js%E2%80%A6"&gt;https://www.andreasreiterer.at/react-app/static/js…&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you check the DOM of your &lt;em&gt;index.html&lt;/em&gt;, you’ll see, that the JavaScript bundle’s links are absolute URLs – from the root of your application:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="/static/js/1.3aec9dfa.chunk.js"&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;script src="/static/js/main.72f93e60.chunk.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And that’s the root of our problem. Your app does not know it’s own location to build absolute URLs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Webpack’s &lt;em&gt;publicPath&lt;/em&gt; Setting
&lt;/h2&gt;

&lt;p&gt;There is the &lt;code&gt;publicPath&lt;/code&gt; setting in your Webpack configuration to tell an app what its root path is. In our example, that would be &lt;em&gt;&lt;a href="https://www.andreasreiterer.at/react-app/"&gt;https://www.andreasreiterer.at/react-app/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If set correctly, it will base links like above from that URL – and your application will load like expected.&lt;/p&gt;

&lt;p&gt;If you created your React app with &lt;em&gt;create-react-app&lt;/em&gt;, you probably can’t access your webpack configuration. (Except you’ve ejected, but that’s not relevant for now)&lt;/p&gt;

&lt;p&gt;_ &lt;strong&gt;So where do we put that information?&lt;/strong&gt; _&lt;/p&gt;

&lt;p&gt;I’ll tell you the secret sauce: It’s called &lt;em&gt;“homepage”&lt;/em&gt; and it lives in your &lt;em&gt;package.json&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Specifiying “homepage”
&lt;/h2&gt;

&lt;p&gt;If you’re using create-react-app, you won’t have to deal with Webpack configs. (Which is nice 😜 ) Instead – &lt;strong&gt;ejected or not&lt;/strong&gt; –  we just have to specify &lt;em&gt;“homepage”&lt;/em&gt; in our &lt;em&gt;package.json:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---meJEF1b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/www.andreasreiterer.at/wp-content/uploads/2018/11/homepage_package_json.jpg%3Fresize%3D660%252C90%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---meJEF1b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/www.andreasreiterer.at/wp-content/uploads/2018/11/homepage_package_json.jpg%3Fresize%3D660%252C90%26ssl%3D1" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The way how create-react-app has it’s webpack configuration set up, this will replace the &lt;code&gt;publicPath&lt;/code&gt; with the correct base URL for your app. (If you want to know more about the &lt;code&gt;publicPath&lt;/code&gt; setting,  have a look at the &lt;a href="https://webpack.js.org/configuration/output/#output-publicpath"&gt;Webpack documentation&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Now that we told your app it’s base URL, run &lt;code&gt;npm run build&lt;/code&gt; again and copy the app to your web space to find your app up and running.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; : If you experience routing-issues with &lt;code&gt;react-router&lt;/code&gt; on your static deployment, &lt;a href="https://dev.to/a_reiterer/how-to-fix-browserrouter-for-react-apps-on-apache-43jd-temp-slug-7791315"&gt;this article&lt;/a&gt; might be helpful for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;The next time you get a white screen after deploying a React app, remember the steps you’ve learned today:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check the browser’s console for errors&lt;/li&gt;
&lt;li&gt;Does the app link to the wrong bundle?&lt;/li&gt;
&lt;li&gt;Update the “homepage” setting in your &lt;em&gt;package.json&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Build your app again and put it on your web space&lt;/li&gt;
&lt;li&gt;Success!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Don't miss my next post!
&lt;/h3&gt;

&lt;p&gt;Did you like this article? Sign up to my &lt;a href="https://www.andreasreiterer.at/dev-newsletter/"&gt;newsletter&lt;/a&gt; and get my future blog posts delivered right into your inbox.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/photos/lTUyP3RaLpw?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Andrew Guan&lt;/a&gt; on&lt;/em&gt; &lt;a href="https://unsplash.com/search/photos/white-screen?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;&lt;em&gt;Unsplash&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://www.andreasreiterer.at/fix-whitescreen-static-react-app/"&gt;How to fix the Whitescreen After a Static Deployment with create-react-app&lt;/a&gt; appeared first on &lt;a href="https://www.andreasreiterer.at"&gt;Andreas Reiterer&lt;/a&gt;.&lt;/p&gt;


</description>
      <category>webdev</category>
    </item>
    <item>
      <title>How to fix BrowserRouter for React Apps on Apache</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Mon, 12 Nov 2018 05:27:58 +0000</pubDate>
      <link>https://dev.to/a_reiterer/how-to-fix-browserrouter-for-react-apps-on-apache-42e6</link>
      <guid>https://dev.to/a_reiterer/how-to-fix-browserrouter-for-react-apps-on-apache-42e6</guid>
      <description>

&lt;p&gt;While learning React, most people develop and test their apps locally. I mean … they’re just demo apps for learning purposes right? Nobody wants to see stuff like that, so why even think of deploying the apps somewhere?&lt;/p&gt;

&lt;p&gt;But then comes the day where you try to deploy a React App to a web server, so you could show it to the people out there: “Look! I made this!”. And that’s how it was for me too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deploying a static React bundle to an Apache web space
&lt;/h3&gt;

&lt;p&gt;I already had a web space, so it was obvious for me to put it up there. Because I used &lt;code&gt;create-react-app&lt;/code&gt;,  all I had to do was running &lt;code&gt;npm run build&lt;/code&gt; and copy the contents of my app’s &lt;code&gt;build&lt;/code&gt; folder to said web space.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;YEAH! After all that hard work I just deployed my first React app!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I started clicking through several routes and everything seemed fine, until I refreshed the page or tried to access a route directly. I got a 404 error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4i7olYr9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/www.andreasreiterer.at/wp-content/uploads/2018/11/oopsnotfound.jpg%3Fresize%3D660%252C92%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4i7olYr9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/www.andreasreiterer.at/wp-content/uploads/2018/11/oopsnotfound.jpg%3Fresize%3D660%252C92%26ssl%3D1" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“But it works like a charm locally – why doesn’t it on my web space?”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Does that sound familiar to you?&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is BrowserRouter not working?
&lt;/h3&gt;

&lt;p&gt;Some research brought up several solutions. One of them:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“Use HashRouter instead of BrowserRouter”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Okay that would be an easy one. But since I wanted the app to support the browser’s history, that was no option. So let’s dig deeper and have a look at the reason for the error.&lt;/p&gt;

&lt;p&gt;When you’re visiting a route of your app directly, e.g. via &lt;em&gt;&lt;a href="https://myapp.com/route/123"&gt;https://myapp.com/route/123&lt;/a&gt;&lt;/em&gt; Apache tries to map that URL to a file in the public folder. In this case it looks for &lt;em&gt;/route/123.html&lt;/em&gt; which obviously doesn’t exist – therefore the 404 error.&lt;/p&gt;

&lt;p&gt;To avoid that, we have to tell Apache to redirect all requests to our &lt;em&gt;index.html&lt;/em&gt; so our app can perform some routing magic for that URL.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fixing the app’s routing
&lt;/h3&gt;

&lt;p&gt;Now here’s how to finally fix the routing. To tell Apache to redirect requests to &lt;em&gt;index.html&lt;/em&gt; where our app lives, we have to modify the &lt;em&gt;.htaccess&lt;/em&gt; file. If there is no such file in your app’s folder yet, just create it.&lt;/p&gt;

&lt;p&gt;Then be sure that you put in those 4 lines that will magically make your routing work.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After we put that &lt;em&gt;.htaccess&lt;/em&gt; file into the same directory as the &lt;em&gt;index.html&lt;/em&gt;, Apache will redirect each new request directly to your app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus: Deploying the React app to a sub directory
&lt;/h3&gt;

&lt;p&gt;If you’re deploying your app into a sub directory, so it’s accessible e.g. via &lt;em&gt;&lt;a href="https://myapp.com/the-app"&gt;https://myapp.com/the-app&lt;/a&gt;&lt;/em&gt;, you’ll soon notice that there is another issue. Each click to a new route will transform the URL to something like &lt;em&gt;&lt;a href="https://myapp.com/route-abc"&gt;https://myapp.com/route-abc&lt;/a&gt;&lt;/em&gt; – which will break again after a reload. But there is a simple fix for that:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;BrowserRouter&lt;/em&gt; has a prop called &lt;code&gt;basename&lt;/code&gt; where you can specify your sub-directory path:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;BrowserRouter basename="/the-app"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;From now on, each Route like &lt;code&gt;/contacts&lt;/code&gt; will result in an URL like &lt;em&gt;&lt;a href="http://myapp.com/the-app/contacts"&gt;http://myapp.com/the-app/contacts&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping up
&lt;/h3&gt;

&lt;p&gt;Today you learned how to fix routing with &lt;em&gt;BrowserRouter&lt;/em&gt; for React Apps deployed to Apache. You learned, why you even get the 404 error in the beginning, and that you can overcome that error by redirecting all requests back to &lt;em&gt;index.html.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The next time you’re facing routing issues with a React App on Apache, head back to this article and try putting the redirect script to the &lt;em&gt;.htaccess&lt;/em&gt; file.&lt;/p&gt;

&lt;p&gt;Do you know more issues with hosting a static React app on Apache? Leave a comment at the bottom.&lt;/p&gt;

&lt;h3&gt;
  
  
  Don't miss my next posts!
&lt;/h3&gt;

&lt;p&gt;Sign up for my &lt;a href="https://www.andreasreiterer.at/dev-newsletter/"&gt;newsletter&lt;/a&gt; to get all my future blog posts delivered directly to your inbox! &lt;/p&gt;




&lt;p&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/photos/ZGH6xd3usAs?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Stef Westheim&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/server?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://www.andreasreiterer.at/fix-browserrouter-on-apache/"&gt;How to fix BrowserRouter for React Apps on Apache&lt;/a&gt; appeared first on &lt;a href="https://www.andreasreiterer.at"&gt;Andreas Reiterer&lt;/a&gt;.&lt;/p&gt;


</description>
      <category>webdev</category>
      <category>apache</category>
      <category>createreactapp</category>
      <category>deployment</category>
    </item>
    <item>
      <title>How to Test Your Presentational Components with Jest</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Mon, 26 Mar 2018 20:34:15 +0000</pubDate>
      <link>https://dev.to/a_reiterer/how-to-test-your-presentational-components-with-jest-2dnd</link>
      <guid>https://dev.to/a_reiterer/how-to-test-your-presentational-components-with-jest-2dnd</guid>
      <description>

&lt;p&gt;&lt;em&gt;Did you ever ask yourself, if it makes sense to test presentational components, or if it’s just too time consuming? Good news, you’re not alone! That’s why I put together three ways to create useful Jest tests for your presentational components without spending too much time.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Does it make sense to test static components that only render a UI?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer is: It depends. It might not be too useful if you only test if your JSX is rendered correctly, or if &lt;code&gt;componentDidMount&lt;/code&gt; was called correctly. However, there are cases, where you really want to test your component, as well as the one or other quick-win, that can help you to avoid some nasty bugs later on.&lt;/p&gt;

&lt;p&gt;In this post we will discuss some ways to introduce basic tests to your app. Please note: This is not meant to be a full blown in-depth article about the secrets of unit testing and how to achieve 100% code coverage. However, some testing is better than no testing – and the kind of tests that we’re discussing in the next sections might still save you some hours of debugging and bug fixing – without being too complicated and time consuming.&lt;/p&gt;

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

&lt;p&gt;If your component renders different content or child components, depending on the &lt;code&gt;props&lt;/code&gt; that were passed, it is a good idea to test, if your component actually renders as intended.&lt;/p&gt;

&lt;p&gt;To assert your rendered components with Jest, you can use &lt;code&gt;enzyme&lt;/code&gt; or React’s &lt;a href="http://facebook.github.io/react/docs/test-utils.html"&gt;TestUtils&lt;/a&gt;. For this example we use &lt;code&gt;enzyme&lt;/code&gt; but feel free to use whatever library suits you best.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react"; import { shallow } from "enzyme"; import ContactListWithLoadingIndicator from "./ContactListWithLoadingIndicator"; import LoadingIndicator from "./LoadingIndicator"; import ContactList from "./ContactList"; const dummyFunc = () =\&amp;gt; {}; const dummyArray = [{ id: 1, firstName: "Max", lastName: "Mustermann", street: "Duck Creek Road", house: 2561, zip: 94107, city: "San Francisco", phone: "650-795-0470", email: "max.mustermann@internet.com" }, { id: 2, firstName: "Maxine", lastName: "Musterfrau", street: "Duck Creek Road", house: 2562, zip: 94107, city: "San Francisco", phone: "650-795-0471", email: "maxine.musterfrau@internet.com" }]; test("ContactListWithLoadInd shows LoadingIndicator when loading", () =\&amp;gt; { const contactList = shallow( \&amp;lt;ContactListWithLoadingIndicator isLoading={true} contacts={dummyArray} /\&amp;gt; ); const loadingIndicator = contactList.find(LoadingIndicator); expect(loadingIndicator).toHaveLength(1); }); test("ContactListWithLoadInd shows ContactList when not loading", () =\&amp;gt; { const contactList = shallow( \&amp;lt;ContactListWithLoadingIndicator isLoading={false} contacts={dummyArray} /\&amp;gt; ); const list = contactList.find(ContactList); expect(list).toHaveLength(1); });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example we created two unit tests. The first one, renders our &lt;code&gt;&amp;lt;ContactListWithLoadingIndicator&amp;gt;&lt;/code&gt; with &lt;code&gt;isLoading={true}&lt;/code&gt; to check, if it renders a &lt;code&gt;&amp;lt;LoadingIndicator&amp;gt;&lt;/code&gt;, while in the second test case, we check if it renders the &lt;code&gt;&amp;lt;ContactList&amp;gt;&lt;/code&gt; component when it’s not currently loading.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Basic Component Tests
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;“It only renders some UI, it can’t break, so I don’t need to test it”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine the following scenario:  Months after you created your app, you get a change request, that requires you to change a certain object at one point. Chances are, that other components might be dependent on this object and are now breaking, because of your change. You won’t know if it actually broke something, until you clicked through your whole app. And with “whole app”, I mean each possible combination of components was rendered. Or …  you could just hope that nothing else depends on the object you changed.&lt;/p&gt;

&lt;p&gt;Sounds like a lot of fun, heh?&lt;/p&gt;

&lt;p&gt;You can avoid the necessity of clicking through all possible paths of your app. To do so, we can add basic component tests for each of your components.&lt;/p&gt;

&lt;p&gt;To do so, you have to create mock objects for everything you pass down the &lt;code&gt;props&lt;/code&gt; of the component. Then you simply render it with ReactDOM inside a Jest test, and if it can’t be rendered for some reason, the test will fail.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react"; import ReactDOM from "react-dom"; import ContactDetailView from "./ContactDetailView"; const dummyFunc = () =\&amp;gt; {}; const dummy = { id: 1, firstName: "Max", lastName: "Mustermann", street: "Duck Creek Road", house: 2561, zip: 94107, city: "San Francisco", phone: "650-795-0470", email: "max.mustermann@internet.com" }; test("ContactDetailView rendered correctly", () =\&amp;gt; { const div = document.createElement("div"); ReactDOM.render( \&amp;lt;ContactDetailView contact={dummy} onDelete={dummyFunc} onEdit={dummyFunc} /\&amp;gt;, div ); });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;“But how would I notice that my app breaks from a changed object, if I always pass a correct object in the test?&lt;/em&gt;” – You might think right now.&lt;/p&gt;

&lt;p&gt;You’re right. The above example renders a component and it’s child components. It only covers errors caused by changes that were made to the component or it’s children. However, if you change the props of a component, you also have to update the tests of this component. So if some child components depend on your changed object that was passed down through &lt;code&gt;props&lt;/code&gt;, this test won’t pass, unless you fixed all the child components. As you can see, this small test might save you some hours bug fixing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Basic Jest Snapshot Tests
&lt;/h2&gt;

&lt;p&gt;Snapshot tests are a powerful tool to exactly compare the rendered markup with a previously saved snapshot.&lt;/p&gt;

&lt;p&gt;Let’s have a look how we can create a snapshot for our &lt;code&gt;ContactDetailView&lt;/code&gt;&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react"; import ContactDetailView from "./ContactDetailView"; import renderer from "react-test-renderer"; const dummyFunc = () =\&amp;gt; {}; const dummy = { [... cropped for brevity ...] }; test("ContactDetailView matches the Snapshot", () =\&amp;gt; { const component = renderer.create( \&amp;lt;ContactDetailView contact={dummy} onDelete={dummyFunc} onEdit={dummyFunc} /\&amp;gt; ); let tree = component.toJSON(); expect(tree).toMatchSnapshot(); });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, we first render our component with &lt;code&gt;renderer.create&lt;/code&gt;. The first run will create a new snapshot, file that contains the exact markup of the rendered component. Now each time the test is executed, it compares the rendered markup with the snapshot.&lt;/p&gt;

&lt;p&gt;Snapshots are a good way to perform a very detailed check, if something in your components changed. This is especially useful to test presentational components.&lt;/p&gt;

&lt;p&gt;However, there are caveats: Each time you change the component, you will have to generate a new snapshot by running &lt;code&gt;jest -u&lt;/code&gt; to overwrite the existing snapshot. At this point it’s necessary to manually check how the markup has changed and if it’s really correct. You really don’t want your test to run against an incorrect snapshot. Usually you should check the snapshot before committing the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Snapshot tests are worth nothing if you don’t take the time to manually check the changes in your snapshot files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Congratulations to your new skill! You just learned three ways to add basic unit tests to test presentational components for your React components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Testing Conditional Rendering&lt;/li&gt;
&lt;li&gt;Basic Component Tests&lt;/li&gt;
&lt;li&gt;Basic Snapshot Testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As mentioned in the beginning of this post, there are a lot more ways to test your components in detail. However, if you’re concerned about the necessary time effort to add testing to your app, those three approaches are a quick way to reduce the risk of adding new bugs.&lt;/p&gt;

&lt;p&gt;Did you like this post? Subscribe to my &lt;a href="https://www.andreasreiterer.at/dev-newsletter/"&gt;newsletter&lt;/a&gt; and get more like that delivered directly to your inbox. &lt;/p&gt;


</description>
      <category>webdev</category>
      <category>enzyme</category>
      <category>javascript</category>
      <category>jest</category>
    </item>
    <item>
      <title>3 Quick Wins to Test Your Presentational React Components with Jest</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Mon, 26 Mar 2018 20:34:15 +0000</pubDate>
      <link>https://dev.to/a_reiterer/3-quick-wins-to-test-your-presentational-react-components-with-jest-3bgh</link>
      <guid>https://dev.to/a_reiterer/3-quick-wins-to-test-your-presentational-react-components-with-jest-3bgh</guid>
      <description>

&lt;p&gt;&lt;em&gt;Did you ever ask yourself, if it makes sense to test presentational components, or if it’s just too time consuming? Good news, you’re not alone! That’s why I put together three ways to create useful Jest tests for your presentational components without spending too much time.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Does it make sense to test static components that only render a UI?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer is: It depends. It might not be too useful if you only test your JSX, or if  you called &lt;code&gt;componentDidMount&lt;/code&gt; correctly. However, there are cases, where you really want to test your component, as well as the one or other quick-win, that can help you to avoid some nasty bugs later on.&lt;/p&gt;

&lt;p&gt;In this post we will discuss some ways to introduce basic tests to your app. Please note: This is not meant to be a full-blown in-depth article about the secrets of unit testing and how to meet 100% code coverage. However, some testing is better than no testing – and the kind of tests that we’re discussing in the next sections can save you some hours of debugging and bug fixing – without being too complicated and time-consuming.&lt;/p&gt;

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

&lt;p&gt;If your component renders different content or child components, depending on which &lt;code&gt;props&lt;/code&gt; you passed, it is a good idea to test, if your component actually renders as intended.&lt;/p&gt;

&lt;p&gt;To assert your rendered components with Jest, you can use &lt;code&gt;enzyme&lt;/code&gt; or React’s &lt;a href="http://facebook.github.io/react/docs/test-utils.html"&gt;TestUtils&lt;/a&gt;. For this example we use &lt;code&gt;enzyme&lt;/code&gt; but feel free to use whatever library suits you best.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { shallow } from "enzyme";
import ContactListWithLoadingIndicator from "./ContactListWithLoadingIndicator";
import LoadingIndicator from "./LoadingIndicator";
import ContactList from "./ContactList";

const dummyFunc = () =&amp;gt; {};
const dummyArray = [
  { 
    id: 1, 
    firstName: "Max", 
    lastName: "Mustermann", 
    street: "Duck Creek Road", 
    house: 2561, 
    zip: 94107, 
    city: "San Francisco", 
    phone: "650-795-0470", 
    email: "max.mustermann@internet.com" 
  }, 
  { 
    id: 2,
    firstName: "Maxine", 
    lastName: "Musterfrau", 
    street: "Duck Creek Road", 
    house: 2562, zip: 94107, 
    city: "San Francisco", 
    phone: "650-795-0471", 
    email: "maxine.musterfrau@internet.com" 
  }
];

test("ContactListWithLoadInd shows LoadingIndicator when loading", () =&amp;gt; { 
  const contactList = shallow(&amp;lt;ContactListWithLoadingIndicator isLoading={true} contacts={dummyArray} /&amp;gt; ); 
  const loadingIndicator = contactList.find(LoadingIndicator);

  expect(loadingIndicator).toHaveLength(1);
});

test("ContactListWithLoadInd shows ContactList when not loading", () =&amp;gt; { 
  const contactList = shallow(&amp;lt;ContactListWithLoadingIndicator isLoading={false} contacts={dummyArray} /&amp;gt;);
  const list = contactList.find(ContactList);

  expect(list).toHaveLength(1);});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example we created two unit tests. The first one, renders our &lt;code&gt;&amp;lt;ContactListWithLoadingIndicator&amp;gt;&lt;/code&gt; with &lt;code&gt;isLoading={true}&lt;/code&gt; to check, if it renders a &lt;code&gt;&amp;lt;LoadingIndicator&amp;gt;&lt;/code&gt;, while in the second test case, we check if it renders the &lt;code&gt;&amp;lt;ContactList&amp;gt;&lt;/code&gt; component when it’s not currently loading.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Basic Component Tests
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;“It only renders some UI, it can’t break, so I don’t need to test it”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine the following scenario:  Months after you created your app, you get a change request, that requires you to change a certain object at one point. Chances are, that other components might depend on this object and are now breaking, because of your change. You won’t know if it actually broke something, until you clicked through your whole app. And with “whole app”, I mean each possible combination of components that our app might be able to render. Or …  you could just hope that nothing else depends on the object you changed.&lt;/p&gt;

&lt;p&gt;Sounds like a lot of fun, heh?&lt;/p&gt;

&lt;p&gt;You can avoid the need of clicking through all possible paths of your app. To do so, we can add basic component tests for each of your components.&lt;/p&gt;

&lt;p&gt;To do so, you have to create mock objects for everything you pass down the &lt;code&gt;props&lt;/code&gt; of the component. Then you simply render it with ReactDOM inside a Jest test, and if it’s not possible to render for some reason, the test will fail.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import ReactDOM from "react-dom";
import ContactDetailView from "./ContactDetailView";

const dummyFunc = () =&amp;gt; {};
const dummy = {
  id: 1,
  firstName: "Max",
  lastName: "Mustermann",
  street: "Duck Creek Road",
  house: 2561,
  zip: 94107,
  city: "San Francisco",
  phone: "650-795-0470",
  email: "max.mustermann@internet.com"
};

test("ContactDetailView rendered correctly", () =&amp;gt; {
  const div = document.createElement("div");
  ReactDOM.render(
    &amp;lt;ContactDetailView
      contact={dummy}
      onDelete={dummyFunc}
      onEdit={dummyFunc}
    /&amp;gt;,
    div
  );
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;“But how would I notice that my app breaks from a changed object, if I always pass a correct object in the test?&lt;/em&gt;” – You might think&lt;/p&gt;

&lt;p&gt;You’re right. The above example renders a component and it’s child components. It only covers errors caused by changes to the component or it’s children. However, if you change the props of a component, you also have to update the tests of this component. So if some child components depend on your changed object through &lt;code&gt;props&lt;/code&gt;, this test won’t pass, unless you fixed all the child components. As you can see, this small test might save you some hours bug fixing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Basic Jest Snapshot Tests
&lt;/h2&gt;

&lt;p&gt;Snapshot tests are a powerful tool to exactly compare the rendered markup with a previously saved snapshot.&lt;/p&gt;

&lt;p&gt;Let’s have a look how we can create a snapshot for our &lt;code&gt;ContactDetailView&lt;/code&gt;&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import ContactDetailView from "./ContactDetailView";
import renderer from "react-test-renderer";
const dummyFunc = () =&amp;gt; {};
const dummy = {
   [... cropped for brevity ...]
};
test("ContactDetailView matches the Snapshot", () =&amp;gt; {
  const component = renderer.create(
    &amp;lt;ContactDetailView
      contact={dummy}
      onDelete={dummyFunc}
      onEdit={dummyFunc}
    /&amp;gt;
  );
  let tree = component.toJSON();
  expect(tree).toMatchSnapshot();
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, we first render our component with &lt;code&gt;renderer.create&lt;/code&gt;. The first run will create a new snapshot, file that has the exact markup of the rendered component. Now each time we execute the test, it compares the rendered markup with the snapshot.&lt;/p&gt;

&lt;p&gt;Snapshots are a good way to do a very detailed check, if something in your components changed. This is especially useful to test presentational components.&lt;/p&gt;

&lt;p&gt;However, there are caveats: Each time you change the component, you will have to generate a new snapshot by running &lt;code&gt;jest -u&lt;/code&gt; to overwrite the existing snapshot. At this point it’s necessary to manually check how the markup has changed and if it’s really correct. You really don’t want your test to run against an incorrect snapshot. Usually you should check the snapshot before committing the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Snapshot tests are worth nothing if you don’t take the time to manually check the changes in your snapshot files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Congratulations to your new skill! You just learned three ways to add basic unit tests to test presentational components for your React components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Testing Conditional Rendering&lt;/li&gt;
&lt;li&gt;Basic Component Tests&lt;/li&gt;
&lt;li&gt;Basic Snapshot Testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As mentioned in the beginning of this post, there are a lot more ways to test your components in detail. However, if you’re concerned about the necessary time effort to add testing to your app, those three approaches are a quick way to cut the risk of adding new bugs. So next time you consider skipping testing to save time, try your favourite approach and cut the risk of adding new bugs!&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Do you write unit tests? Why? Why Not? I’d love to hear about it! Leave a comment and tell me about your test experience and how you approach this subject in your projects. *&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Did you like this post? &lt;a href="https://www.andreasreiterer.at/dev-newsletter/"&gt;Sign up for my newsletter&lt;/a&gt; and get more articles like that delivered directly to your inbox. &lt;/p&gt;


</description>
      <category>webdev</category>
      <category>enzyme</category>
      <category>javascript</category>
      <category>jest</category>
    </item>
    <item>
      <title>How and Why to Bind a Callback Function in React Components</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Wed, 07 Feb 2018 19:40:56 +0000</pubDate>
      <link>https://dev.to/a_reiterer/how-and-why-to-bind-a-callback-function-in-react-components-45m8</link>
      <guid>https://dev.to/a_reiterer/how-and-why-to-bind-a-callback-function-in-react-components-45m8</guid>
      <description>

&lt;p&gt;This might sound familiar to you: You run your React app after you made some changes to test your new functionality and get an error message like this: “ &lt;strong&gt;this.setState’ is not a function&lt;/strong&gt; “. It’s probably because you forgot to bind a callback function before passing it down a prop. Today you’ll learn why you need to bind, and how to bind a callback function in React.&lt;/p&gt;

&lt;p&gt;Handling the &lt;code&gt;this&lt;/code&gt; keyword is causing a lot of headaches for many developers. That’s because in JavaScript it is not always clear what &lt;code&gt;this&lt;/code&gt; is actually referring to. Especially, when you’re working with callback functions.&lt;/p&gt;

&lt;p&gt;If you ask yourself, why you even have to bind callback functions and when you actually have to do this, this article is for you: Today we’re going to learn about binding the &lt;code&gt;this&lt;/code&gt; keyword in React components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; &lt;em&gt;Binding callbacks is a JavaScript thing. It’s necessary because you have to tell your callback what it’s context is. Bind your callbacks in React either in the constructor or by using the public class fields syntax.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why we Need to Bind: The Basics of &lt;code&gt;this&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;First of all, it’s not a React thing that you’ve got to bind &lt;code&gt;this&lt;/code&gt;. In fact, it’s related to the way JavaScript works. &lt;code&gt;this&lt;/code&gt; is a special keyword inside each function that refers to the current context. You might already have seen &lt;code&gt;this&lt;/code&gt; in other programming languages.&lt;/p&gt;

&lt;p&gt;In JavaScript, however, the value of &lt;code&gt;this&lt;/code&gt; depends on &lt;em&gt;how&lt;/em&gt; the function was called, not where or when it was defined. Also, it is not affected by scope, like a variable would be. This means, that whenever you pass a function down another function, &lt;code&gt;this&lt;/code&gt; will not refer to the same value.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction() {
 console.log(this);
}

// Calling the function normally
myFunction(); // 'this' will refer to 'window'

// pass the function to an object and call it as an object method
var myObj = { doSomething: myFunction};
myObj.doSomething; // 'this' will refer to 'myObj'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you want to learn more about &lt;code&gt;this&lt;/code&gt;, I recommend having a look at the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this"&gt;MDN documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But what does all of that have to do with binding this, you ask? Well – it’s possible to tell a function what it’s &lt;code&gt;this&lt;/code&gt; should refer to – and that’s what you’re doing with binding &lt;code&gt;this&lt;/code&gt;. Let’s have a look at that.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Bind &lt;code&gt;this&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If you bind a function, you can set the value of &lt;code&gt;this&lt;/code&gt; for later, so it doesn’t matter anymore where exactly your callback function is called. You can achieve this by calling &lt;code&gt;bind(this)&lt;/code&gt; for your function:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction() { 
  console.log(this); 
}

// bind(this) creates a new function where the value of 'this' is fixed
var boundFunction = myFunction.bind(this);

// It's also possible to replace the function by it's bound version
myFunction = myFunction.bind(this);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Calling &lt;code&gt;bind(this)&lt;/code&gt; on a function returns a new (bound) function that has the value of &lt;code&gt;this&lt;/code&gt; already defined. This way, if you would pass it to another object, or down a prop inside a React component, the value of &lt;code&gt;this&lt;/code&gt; inside this component will not change anymore.&lt;/p&gt;

&lt;p&gt;Now let’s have a look at how we can bind &lt;code&gt;this&lt;/code&gt; inside a React component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Binding &lt;code&gt;this&lt;/code&gt; in a React Component
&lt;/h2&gt;

&lt;p&gt;The best way to show the different approaches is an example. Have a look at the components in the following code sample:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const AddComponent = ({onAdd}) =&amp;gt; {
 return (
   &amp;lt;div&amp;gt;
     &amp;lt;button onClick={() =&amp;gt; {onAdd("item")}}&amp;gt;Add&amp;lt;/button&amp;gt;
   &amp;lt;/div&amp;gt;
 );
}

class MyListComponent extends Component {
 constructor(props) {
   super(props);
   this.state = {
     myObjects: []
   }
 }

 handleAdd(newObject) {
   this.setState((prevState) =&amp;gt; (
     Object.assign(
       {}, 
       this.state, 
       { myObjects: [...prevState.myObjects, newObject] }
     )
   ));
 }

 render() {
   return (
     &amp;lt;div&amp;gt;
       &amp;lt;AddComponent onAdd={this.handleAdd} /&amp;gt;
       {this.state.myObjects}
     &amp;lt;/div&amp;gt;
   )
 }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What we have here is a simple Component that holds a list of items. A child component adds items to this list with the help of a callback passed through props. So far so good. If you execute the above example, you’ll get an error because &lt;code&gt;this&lt;/code&gt; is undefined.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vyZwjTFF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/www.andreasreiterer.at/wp-content/uploads/2018/02/cannot-read-property-setState-of-undefined.jpg%3Fresize%3D660%252C487%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vyZwjTFF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i1.wp.com/www.andreasreiterer.at/wp-content/uploads/2018/02/cannot-read-property-setState-of-undefined.jpg%3Fresize%3D660%252C487%26ssl%3D1" alt=""&gt;&lt;/a&gt;The error you get, when you call a callback function without binding ‘this’&lt;/p&gt;

&lt;p&gt;In the following sections, I’m going to explain the &lt;a href="https://reactjs.org/docs/handling-events.html"&gt;recommended ways&lt;/a&gt; to bind &lt;code&gt;this&lt;/code&gt; to avoid such errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using The &lt;em&gt;Public Class Fields Syntax&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;It’s worth to note that the class fields syntax are not standardized yet. But they are already so widely used that &lt;em&gt;if&lt;/em&gt; there would be syntax changes, it probably won’t take long for a proper frictionless migration strategy to appear.&lt;/p&gt;

&lt;p&gt;When using the class field syntax, you’re going to transform your callback to a public field of your class. Doing so will bind the &lt;code&gt;this&lt;/code&gt; of the callback automatically to the class it was defined in. This allows you to pass the callback to your child component without having to bind it separately in your constructor.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;handleAdd = (newObject) =&amp;gt; {
   this.setState((prevState) =&amp;gt; (
     Object.assign(
       {}, 
       this.state, 
       { myObjects: [...prevState.myObjects, newObject] }
     )
   ));
 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Binding &lt;code&gt;this&lt;/code&gt; Inside the Constructor
&lt;/h3&gt;

&lt;p&gt;One of the easiest ways to bind &lt;code&gt;this&lt;/code&gt; is to do so in your parent component’s constructor by calling &lt;code&gt;.bind(this)&lt;/code&gt; for your callback function:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;constructor(props) {
  super(props);
  this.state = {
    myObjects: []
  }

  this.handleAdd = this.handleAdd.bind(this);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That’s it! Basically, you just have to bind every callback function inside the constructor and you’re safe to go.&lt;/p&gt;

&lt;p&gt;If you have a lot of callback functions you can probably imagine how big your constructor could get. If you compare this to using the public class fields syntax, you’ll notice that this approach means a bit more typing for you and a potentially bigger constructor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alternatives and Their Caveats
&lt;/h2&gt;

&lt;p&gt;In the previous sections, I explained the recommended ways of binding &lt;code&gt;this&lt;/code&gt;. However, if you don’t like the above-mentioned approaches, here are some more – if you are okay with their caveats.&lt;/p&gt;

&lt;h3&gt;
  
  
  Binding Directly Inside a Prop
&lt;/h3&gt;

&lt;p&gt;Instead of binding the callback function in your constructor, you can do so while passing it through a prop:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
   &amp;lt;AddComponent onAdd={this.handleAdd.bind(this)} /&amp;gt;
   {this.state.myObjects}
 &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Whenever your component is re-rendered, &lt;code&gt;bind(this)&lt;/code&gt; returns a new function and passes it down your component tree. This may lead to unnecessary re-rendering of your child components. Down the road, this can have a massive impact on your app performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Passing an arrow function to props
&lt;/h3&gt;

&lt;p&gt;Instead of using the class field syntax, you could directly pass an arrow function down your props:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
  &amp;lt;AddComponent onAdd={(obj) =&amp;gt; this.handleAdd(obj)} /&amp;gt;
  {this.state.myObjects}
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Like in the previous example, this creates a new callback each time your component is re-rendered and may cause unnecessary re-rendering of your component tree. Again a performance impact that you might not want to put up with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Things Up
&lt;/h2&gt;

&lt;p&gt;We won’t get around binding callback functions, since it’s how JavaScript works. But now that you know why we have to bind &lt;code&gt;this&lt;/code&gt; – and how to do it in React components, I hopefully made your life a bit easier.&lt;/p&gt;

&lt;p&gt;As always there are different ways to solve this issue. But from the four mentioned approaches, I’d suggest sticking to the recommended options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using the class field syntax&lt;/li&gt;
&lt;li&gt;Binding &lt;code&gt;this&lt;/code&gt; in the constructor&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Doing so might save you from unexpected performance issues. After you get used to using one approach, you won’t even think about it again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Action Steps
&lt;/h2&gt;

&lt;p&gt;Still not sure what’s the best approach for you? &lt;strong&gt;Take some time and try both of them!&lt;/strong&gt; After you spent some time trying and practicing, you might notice that you like one approach more than the other – you’ll never know just by reading without trying it.&lt;/p&gt;

&lt;p&gt;Now get your hands dirty and find your favorite approach!&lt;/p&gt;

&lt;h2&gt;
  
  
  Stay Updated
&lt;/h2&gt;

&lt;p&gt;Do you want to upgrade your React skills? Stay informed about new articles by &lt;a href="https://www.andreasreiterer.at/dev-newsletter/"&gt;subscribing to my Newsletter&lt;/a&gt;&lt;/p&gt;


</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>5 UI Libraries to Use With React</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Mon, 22 Jan 2018 19:47:04 +0000</pubDate>
      <link>https://dev.to/a_reiterer/5-ui-libraries-to-use-with-react-3mfh</link>
      <guid>https://dev.to/a_reiterer/5-ui-libraries-to-use-with-react-3mfh</guid>
      <description>

&lt;p&gt;If you’re a developer who – like me – is not a good designer or a CSS pro, you might know how troublesome it can be to put together the design for your React app. You spend a lot of time tinkering with the styles, but in the end, it just doesn’t look quite nice.&lt;/p&gt;

&lt;p&gt;This is not only annoying, it’s demotivating … after all, you just want to have a good-looking app that is also responsive to support mobile devices. How hard can it possibly be?&lt;/p&gt;

&lt;p&gt;If this sounds familiar to you, I can assure you’re definitely not alone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use UI Libraries to Style a React App
&lt;/h2&gt;

&lt;p&gt;Styling a web app is a huge pain for many developers. Maybe that’s why there are so many UI libraries out there in the wild. Here are some examples of libraries for styling React apps for you to try out:&lt;/p&gt;

&lt;h3&gt;
  
  
  React-Bootstrap
&lt;/h3&gt;

&lt;p&gt;You might already know the Bootstrap Framework. It helps you to create responsive grid layouts on your website and offers a whole lot of controls like buttons and form elements. React-Bootstrap offers the functionality of Bootstrap through handy-to-use React components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Website:&lt;/strong&gt; &lt;a href="https://react-bootstrap.github.io"&gt;https://react-bootstrap.github.io&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Semantic UI
&lt;/h3&gt;

&lt;p&gt;Semantic focuses on making front-end development a better experience for developers. You can not only use it with React, but it also supports other frameworks like Angular or Ember.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Website: &lt;/strong&gt; &lt;a href="https://react.semantic-ui.com/"&gt;https://react.semantic-ui.com/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Material-UI
&lt;/h3&gt;

&lt;p&gt;If you want to build a React app that complies with Google’s Material Design, this might be worth a look. Material-UI provides a bunch of React components that you can use and customize for your app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Website:&lt;/strong&gt;  &lt;a href="http://www.material-ui.com"&gt;http://www.material-ui.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Rebass
&lt;/h3&gt;

&lt;p&gt;Rebass provides React components for primitive UI elements like grid layouts, buttons or boxes. It is built with &lt;a href="https://www.styled-components.com/"&gt;&lt;em&gt;styled-components&lt;/em&gt;&lt;/a&gt;, which allows you to customize all the provided elements and use &lt;strong&gt;real CSS&lt;/strong&gt; in your code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Website:&lt;/strong&gt;  &lt;a href="http://jxnblk.com/rebass/"&gt;http://jxnblk.com/rebass/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Atlaskit
&lt;/h3&gt;

&lt;p&gt;Atlaskit is Atlassian’s (Jira, Confluence, Trello) official UI library and complies to their own Design Guidelines. It is also built with &lt;em&gt;styled-components&lt;/em&gt; and provides a lot of customizable UI elements in form of React components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Website:&lt;/strong&gt;  &lt;a href="https://bitbucket.org/atlassian/atlaskit-starter/"&gt;https://bitbucket.org/atlassian/atlaskit-starter/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Get your hands dirty
&lt;/h2&gt;

&lt;p&gt;All of the above-mentioned libraries are a bit different. The best way to find out which library actually suits your needs, or which one you’re the most comfortable with, is to &lt;strong&gt;try them out&lt;/strong&gt; :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find a mini example project (e.g.: Build a static Twitter card component)&lt;/li&gt;
&lt;li&gt;Open your Code Editor of Choice&lt;/li&gt;
&lt;li&gt;Build the example project once with each of the above libraries&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Spend some time with each of the libraries. You’ll soon see which library you’re the most comfortable with, or which one you like best. If you create the same project with each of them, you can directly compare the differences.&lt;/p&gt;

&lt;p&gt;Did you like this article? Sign up for my &lt;a href="https://www.andreasreiterer.at/dev-newsletter/"&gt;newsletter&lt;/a&gt; so you get all my future posts delivered directly to your inbox!&lt;/p&gt;


</description>
      <category>webdev</category>
      <category>bootstrap</category>
      <category>css</category>
      <category>javascript</category>
    </item>
    <item>
      <title>A  React Beginners Roadmap through the React Eco System</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Thu, 26 Oct 2017 17:22:50 +0000</pubDate>
      <link>https://dev.to/a_reiterer/a--react-beginners-roadmap-through-the-react-eco-system-2f9</link>
      <guid>https://dev.to/a_reiterer/a--react-beginners-roadmap-through-the-react-eco-system-2f9</guid>
      <description>&lt;p&gt;&lt;em&gt;React beginners often have a hard time learning React, since the React ecosystem seems huge. You will inevitably stumble upon stuff like Redux, Webpack or Babel very early. At this point I was so overwhelmed because I had no clue where to start first.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It was even more confusing that I didn’t even know the problems that those tools tried to solve. &lt;/em&gt;&lt;em&gt;Today I know better and I’m going to tell you that there is no need for any of these – at least not yet.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post appeared first on &lt;a href="http://www.andreasreiterer.at/web-development/react-beginners-roadmap/" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I’m writing this for everyone who is just starting out – &lt;strong&gt;React beginners, or newbies in web development&lt;/strong&gt;. However, I assume that you have at least some basic knowledge of HTML, CSS and JavaScript.&lt;/p&gt;

&lt;p&gt;This post is &lt;strong&gt;designed to be a roadmap&lt;/strong&gt; that guides you step by step from the entry level to more sophisticated topics – it helps you to focus on things that matter – instead of wasting time worrying about things that you don’t need yet. The roadmap will &lt;strong&gt;not contain detailed explanations&lt;/strong&gt; of all of the different topics. In-depth content will be covered in future posts and extra detailed content about all of this will be part of my new project: &lt;strong&gt;&lt;a href="https://www.reactfornoobs.com" rel="noopener noreferrer"&gt;React for Noobs&lt;/a&gt;&lt;/strong&gt; – a no-confusion guide for React beginners.&lt;/p&gt;

&lt;p&gt;But enough of that – the next sections will show you, what to focus on to improve your knowledge in React and it’s eco system without pain.&lt;/p&gt;

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

&lt;p&gt;Before starting with React, you should have at least a basic knowledge about HTML, CSS and JavaScript. That said, have a look at ECMAScript6 (ES6) – when working with react, you will need at least&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions" rel="noopener noreferrer"&gt;Arrow Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes" rel="noopener noreferrer"&gt;Classes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals" rel="noopener noreferrer"&gt;Template Literals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let" rel="noopener noreferrer"&gt;let&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const" rel="noopener noreferrer"&gt;const&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This should be enough to begin with the basics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basics for React Beginners
&lt;/h2&gt;

&lt;p&gt;When getting started with React, I strongly suggest that you start with the &lt;a href="https://reactjs.org/tutorial/tutorial.html" rel="noopener noreferrer"&gt;official tutorial&lt;/a&gt;. You’ll learn everything you need to understand the bare basics of React.&lt;/p&gt;

&lt;p&gt;In the next sections, I’ll provide some additional thoughts to some of the stuff that you see in said tutorial.&lt;/p&gt;

&lt;h3&gt;
  
  
  create-react-app
&lt;/h3&gt;

&lt;p&gt;This awesome tool makes bootstrapping a new application a LOT easier. It saves you from fiddling around in configurations that might just confuse you a lot. So for now, stick with create-react-app and be happy to bootstrap a new application with just one command.&lt;/p&gt;

&lt;h3&gt;
  
  
  Components
&lt;/h3&gt;

&lt;p&gt;In React, everything is structured in components. So as soon as you know how to work with JSX, make yourself comfortable with passing data into components:  &lt;strong&gt;&lt;em&gt;props&lt;/em&gt;&lt;/strong&gt; , managing  &lt;strong&gt;&lt;em&gt;state&lt;/em&gt;&lt;/strong&gt; inside a component and also  &lt;strong&gt;&lt;em&gt;lifting&lt;/em&gt; &lt;em&gt;state&lt;/em&gt;&lt;/strong&gt; to a parent component. Learn how to properly &lt;strong&gt;handle events&lt;/strong&gt; or pass functions as props (see &lt;a href="https://reactjs.org/tutorial/tutorial.html#lifting-state-up" rel="noopener noreferrer"&gt;“Lifting State Up”&lt;/a&gt;). Also don’t forget that a little &lt;strong&gt;styling&lt;/strong&gt; won’t hurt :)&lt;/p&gt;

&lt;p&gt;The next step is then to learn how to do &lt;strong&gt;&lt;a href="https://reactjs.org/docs/conditional-rendering.html" rel="noopener noreferrer"&gt;conditional rendering&lt;/a&gt; &lt;/strong&gt;and how to render &lt;a href="https://reactjs.org/docs/lists-and-keys.html" rel="noopener noreferrer"&gt;&lt;strong&gt;Lists&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://reactjs.org/docs/forms.html" rel="noopener noreferrer"&gt;&lt;strong&gt;Forms&lt;/strong&gt;&lt;/a&gt;, as well as how to &lt;strong&gt;&lt;a href="https://reactjs.org/docs/composition-vs-inheritance.html" rel="noopener noreferrer"&gt;compose your components&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you learned all of the above, you should create a small application on your own to strengthen what you’ve learned. Something small and simple. Back then, I just built a Tweet component that should look exactly like the one you see on Twitter. All you have to do is to create the necessary components, put in some static data and play around with it. You could also extend it to be a list of tweets.&lt;/p&gt;

&lt;p&gt;If you feel comfortable enough, you can focus on more advanced stuff.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced React
&lt;/h2&gt;

&lt;p&gt;Now that you’re able to create useful components, it’s time to have a look at &lt;strong&gt;&lt;a href="https://www.andreasreiterer.at/web-development/react-proptypes/" rel="noopener noreferrer"&gt;PropTypes&lt;/a&gt;&lt;/strong&gt;. Also, as you might want to get your data from a remote server or API, make yourself familiar with &lt;strong&gt;&lt;a href="https://www.andreasreiterer.at/web-development/reactjs-lifecycle-methods/" rel="noopener noreferrer"&gt;Life Cycle Methods&lt;/a&gt;&lt;/strong&gt; and how to &lt;strong&gt;&lt;a href="https://daveceddia.com/ajax-requests-in-react/" rel="noopener noreferrer"&gt;fetch data from a server&lt;/a&gt;&lt;/strong&gt;. Note: You can use React with any REST backend / API you want – there is no restriction.&lt;/p&gt;

&lt;p&gt;You might want to split your app into different screens. To handle the transitions, you will need &lt;strong&gt;&lt;a href="https://medium.com/@pshrmn/a-simple-react-router-v4-tutorial-7f23ff27adf" rel="noopener noreferrer"&gt;React Router&lt;/a&gt;&lt;/strong&gt;. Alternative solutions for that exist, however, I suggest to start with react router, which seems to be the go-to solution in the community.&lt;/p&gt;

&lt;p&gt;If you don’t have already, also have a look at how to handle &lt;strong&gt;&lt;a href="https://reactjs.org/docs/forms.html" rel="noopener noreferrer"&gt;forms&lt;/a&gt;,&lt;/strong&gt; especially have a look at &lt;strong&gt;&lt;a href="https://reactjs.org/docs/uncontrolled-components.html" rel="noopener noreferrer"&gt;uncontrolled components&lt;/a&gt;,&lt;/strong&gt; which you’ll need most of the time when working with forms.&lt;/p&gt;

&lt;p&gt;Tip: Use &lt;strong&gt;&lt;a href="https://www.andreasreiterer.at/web-development/react-functional-components/" rel="noopener noreferrer"&gt;functional components over class components&lt;/a&gt;&lt;/strong&gt; everywhere you can. This also means, you should try to keep as many components stateless as you can. You’ll be thankful later when you didn’t spread your state over your whole application – especially if you have to do some refactoring. For further reading, I recommend Dan Abramov’s article about &lt;strong&gt;&lt;a href="https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0" rel="noopener noreferrer"&gt;Presentational and Container Components&lt;/a&gt;.  &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://reactjs.org/docs/error-boundaries.html" rel="noopener noreferrer"&gt;Error Boundaries&lt;/a&gt; &lt;/strong&gt;were introduced with React 16. They catch JavaScript errors and avoid crashing the whole application. Instead, React will display a fallback UI instead of the failed child component tree.&lt;/p&gt;

&lt;p&gt;To avoid errors in the first place, have a look at &lt;strong&gt;&lt;a href="https://facebook.github.io/jest/docs/en/getting-started.html" rel="noopener noreferrer"&gt;Jest&lt;/a&gt;&lt;/strong&gt; – . All the apps that were created with create-react-app support testing with Jest. The cool thing with Jest is, that you can test snapshots of React component trees. That makes creating tests a lot easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  What now?
&lt;/h2&gt;

&lt;p&gt;Now you know a lot about React. You should be able to create some complex applications that are communicating with a backend server or API.&lt;/p&gt;

&lt;p&gt;At a certain point you might realise that managing state gets harder, the more complex your application is. You should now have a look at &lt;strong&gt;state management with Redux or MobX.&lt;/strong&gt;  I’d suggest you try both and decide what fit’s best. Many React Beginners stumble over Redux relatively early in their React career and then try to put it into all of their applications. Most of the time this will just add complexity – instead of taking it away.&lt;/p&gt;

&lt;p&gt;In some cases you want to render your React App at a Server. This might be necessary out of SEO or performance reasons. I put this here, because I truly believe that React Beginners shouldn’t be thinking about &lt;strong&gt;Server Side Rendering&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you want to change something in the build process or in the hidden configuration files (thanks to create-react-app), you can &lt;a href="https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-run-eject" rel="noopener noreferrer"&gt;&lt;strong&gt;eject&lt;/strong&gt;&lt;/a&gt; it. I do not suggest that for React Beginners, because you will instantly add a lot of complexity to your application. So prepare yourself for that by familiarizing yourself with &lt;strong&gt;Webpack&lt;/strong&gt; and &lt;strong&gt;Babel&lt;/strong&gt;. Their configuration files will lie around in your application folder, you should know what they do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;React Beginners often have a hard time getting into it. Once you got the basics and an idea how to proceed step by step, it gets a lot easier. I hope that this guide helped you to understand what you – as a beginner – really need, and what you should postpone to later.&lt;/p&gt;

&lt;p&gt;I want to make the life of React beginners a bit easier. That’s why I recently started working on React For Noobs. It is a step by step guide that takes React Beginners by the hand and guides them step by step from Noob to Pro.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.reactfornoobs.com" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.reactfornoobs.com%2Fimg%2FReactForNoobs.jpg" alt="React for Noobs - A Guide from Noob to Pro"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.reactfornoobs.com" rel="noopener noreferrer"&gt;React for Noobs - A Guide from Noob to Pro&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  There is even more ...
&lt;/h2&gt;

&lt;p&gt;Do you want to learn more about React and it's ecosystem? Good news! Subscribe to my &lt;a href="https://www.andreasreiterer.at/weekly-webdev-newsletter/" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt; to get more articles delivered right into your inbox, for free!&lt;/p&gt;




&lt;p&gt;Thanks for reading the article until the end! I would appreciate if you let me know what you think. Leave a comment, contact me on &lt;a href="https://www.twitter.com/a_reiterer" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or send an email to &lt;a href="//mailto:hi@andreasreiterer.at"&gt;hi@andreasreiterer.at&lt;/a&gt; – I’m happy to respond to any message I get.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>learning</category>
      <category>react</category>
    </item>
    <item>
      <title>Validating Props easily with React PropTypes</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Fri, 22 Sep 2017 18:32:05 +0000</pubDate>
      <link>https://dev.to/a_reiterer/validating-props-easily-with-react-proptypes-plg</link>
      <guid>https://dev.to/a_reiterer/validating-props-easily-with-react-proptypes-plg</guid>
      <description>&lt;p&gt;&lt;em&gt;React PropTypes are a good way to help you catching bugs by validating data types of values passed through props. They also offer possibilities to flag props as mandatory or set default values. They provide a great benefit with little effort.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Header Photo by &lt;a href="https://unsplash.com/photos/2qLT_Rq-2tk?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Fré Sonneveld&lt;/a&gt; on &lt;a href="https://unsplash.com/?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you want to pass any value to a component, you won’t get around &lt;em&gt;props&lt;/em&gt;. They are the component’s interface to get data passed down the component tree. When your application got bigger and more complex, you or one of your colleagues may want to reuse some existing components. Maybe you wrote the component a long time ago, so you have to find out, how to use it and which props are required.&lt;/p&gt;

&lt;p&gt;To find that out, you have a look the source code of said component and have a look at all occurrences of &lt;em&gt;this.props&lt;/em&gt;. You can imagine that this can take some time, depending on the component. But now that you know which props are mandatory, you can start – but wait, do you know &lt;em&gt;what&lt;/em&gt; to pass down? Is it just a &lt;em&gt;string&lt;/em&gt; or is an &lt;em&gt;object&lt;/em&gt; required? You see, it’s not that easy. But there is a solution for that: &lt;strong&gt;React PropTypes&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the heck are React PropTypes?
&lt;/h2&gt;

&lt;p&gt;PropTypes not only help you to catch bugs, but also serve as a handy documentation on how a component has to be used in terms of passing props. Let’s have a look at a small example:&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 PropTypes from 'prop-types';

const Person = (props) =&amp;gt; &amp;lt;div&amp;gt; 
  &amp;lt;h1&amp;gt;{props.firstName} {props.lastName}&amp;lt;/h1&amp;gt;
  {props.country ? &amp;lt;p&amp;gt;Country: {props.country}&amp;lt;/p&amp;gt; : null}
&amp;lt;/div&amp;gt;;

Person.propTypes = {
  firstName:PropTypes.string,
  lastName:PropTypes.string,
  country:PropTypes.string
};

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

&lt;/div&gt;



&lt;p&gt;As you can see, I created a little component to show a person’s name and it’s country. We show the first and last name as a header, and add a paragraph with the country, but only if it is set. For the beginning, I just created the &lt;em&gt;PropTypes&lt;/em&gt; to define the type without caring about mandatory props.&lt;/p&gt;

&lt;p&gt;So what does it do? &lt;em&gt;PropTypes&lt;/em&gt; define the type of a &lt;em&gt;prop&lt;/em&gt;. So each time, a value is passed through a prop, it get’s validated for it’s type. If you pass a value through a &lt;em&gt;prop&lt;/em&gt; with a different data type than it is specified in the &lt;em&gt;PropTypes&lt;/em&gt;, an error message will be printed in the console of your browser:&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/http%3A%2F%2Fwww.andreasreiterer.at%2Fwp-content%2Fuploads%2F2017%2F09%2FpropTypes_wrong_type_error.jpg" 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/http%3A%2F%2Fwww.andreasreiterer.at%2Fwp-content%2Fuploads%2F2017%2F09%2FpropTypes_wrong_type_error.jpg" alt="Warning: Failed prop type: Invalid prop  raw `country` endraw  of type  raw `number` endraw  supplied to  raw `Person` endraw , expected  raw `string` endraw ."&gt;&lt;/a&gt;The error message you get, when you pass a value with a wrong data type&lt;/p&gt;

&lt;p&gt;If you set up your &lt;em&gt;PropTypes&lt;/em&gt; like that, you already got the first benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can easily catch bugs caused by passing data in the wrong data type (e.g.: An object instead of a string)&lt;/li&gt;
&lt;li&gt;Someone that uses your component can see all available &lt;em&gt;props&lt;/em&gt; including their desired data type at one place.&lt;/li&gt;
&lt;li&gt;Some code editors support code completion for props, so you can see the available props while typing in the component in a tooltip.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we know what PropTypes are, and what they are good for, let’s see what else they could do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Type Validation
&lt;/h2&gt;

&lt;p&gt;As shown in the example above, &lt;em&gt;React PropTypes&lt;/em&gt; are used for data type validation. That’s what they are made for. I think most of the time you might check for basic data types.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic data types, arrays and objects
&lt;/h3&gt;

&lt;p&gt;Here is an example for basic data types and :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Person.propTypes = {
  email: PropTypes.string,
  age: PropTypes.number,
  worksRemote: PropTypes.bool,
  updateCallback: PropTypes.func
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Further types that can be used are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PropTypes.array,
PropTypes.arrayOf(PropTypes.string),
PropTypes.object,
PropTypes.objectOf(PropTypes.number)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Complex data types
&lt;/h3&gt;

&lt;p&gt;It is even possible to validate a plain JavaScript object against a certain shape:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Person.propTypes = { 
  car: PropTypes.shape({
    registrationNumber: PropTypes.string,
    year: PropTypes.number
  })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What you’ve just seen is an example on how to validate an object, that has been passed through the &lt;em&gt;“car” prop.&lt;/em&gt; If you pass an object with a field “year” that is a string, you’ll get an error message. That’s great!&lt;/p&gt;

&lt;h3&gt;
  
  
  Specifying a Range of Valid Prop Values
&lt;/h3&gt;

&lt;p&gt;From time to time you might want to have a prop value passed, that is exactly one out of a given set of values. Doing so, could 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;Person.propTypes = {
  gender: PropTypes.oneOf([
    'female', 'male'
  ])
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you now pass &lt;em&gt;“unknown”&lt;/em&gt; through the &lt;em&gt;“gender” prop,&lt;/em&gt; your browser will show an error because it didn’t comply with the specified &lt;em&gt;PropTypes:&lt;/em&gt;&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/http%3A%2F%2Fwww.andreasreiterer.at%2Fwp-content%2Fuploads%2F2017%2F09%2FpropTypes_oneOf_error.jpg" 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/http%3A%2F%2Fwww.andreasreiterer.at%2Fwp-content%2Fuploads%2F2017%2F09%2FpropTypes_oneOf_error.jpg" alt="Warning: Failed prop type: Invalid prop  raw `gender` endraw  of value  raw `unknown` endraw  supplied to  raw `Person` endraw , expected one of ["&gt;&lt;/a&gt;The passed property did not comply to the specified set of values&lt;/p&gt;

&lt;p&gt;Apart from specifying the exact values, you can also specify a set of types for the &lt;em&gt;prop&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PropTypes.oneOfType([
  PropTypes.string,
  PropTypes.number
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Flagging Props as Mandatory
&lt;/h2&gt;

&lt;p&gt;This is another important topic. If you want to require anyone who uses your component to always pass a certain prop, you can flag it as mandatory. See how you could do that – it’s easy! 😊&lt;/p&gt;

&lt;p&gt;You remember the example right at the beginning of this post? I’ll show it again, but now we mark the first and the last name as &lt;strong&gt;required&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 Person = (props) =&amp;gt; &amp;lt;div&amp;gt; 
  &amp;lt;h1&amp;gt;{props.firstName} {props.lastName}&amp;lt;/h1&amp;gt;
  {props.country ? &amp;lt;p&amp;gt;Country: {props.country}&amp;lt;/p&amp;gt; : null}
&amp;lt;/div&amp;gt;;

Person.propTypes = { 
  firstName:PropTypes.string.isRequired, 
  lastName:PropTypes.string.isRequired, 
  country:PropTypes.string 
}; 

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

&lt;/div&gt;



&lt;p&gt;That was easy, wasn’t it? You can chain “isRequired” to any given PropType data type, which will result in an error message, if the &lt;em&gt;prop&lt;/em&gt; is not provided.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set Default Prop Values
&lt;/h2&gt;

&lt;p&gt;If you want certain props to have default values if nothing is provided, you can do this by defining &lt;em&gt;defaultProps&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Person.defaultProps = {
  country: 'Austria' 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above makes sure that every time someone usese the &lt;em&gt;“Person”&lt;/em&gt; component and does not provide a country, the person automatically becomes an Austrian! 😁&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;React PropTypes&lt;/em&gt; are a great way to validate your component inputs. But also, if someone is going to use your component, it provides a quick overview of all the available props and the expected data types. &lt;/p&gt;

&lt;p&gt;So in your next component, try to use &lt;em&gt;PropTypes&lt;/em&gt; to avoid some nasty bugs and improve the reusability of the component a lot.&lt;/p&gt;

&lt;p&gt;Did you like this post and want to read more? Sign up for my &lt;a href="https://www.andreasreiterer.at/dev-newsletter/" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt;. &lt;br&gt;
You will get all my future posts delivered directly into your inbox, and you can unsubscribe at any time. &lt;/p&gt;




&lt;p&gt;The post &lt;a href="http://www.andreasreiterer.at/web-development/react-proptypes/" rel="noopener noreferrer"&gt;Validating Props easily with React PropTypes&lt;/a&gt; appeared first on &lt;a href="http://www.andreasreiterer.at" rel="noopener noreferrer"&gt;Andreas Reiterer&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>proptypes</category>
      <category>react</category>
    </item>
    <item>
      <title>A developer’s way into modern web development</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Thu, 21 Sep 2017 19:27:24 +0000</pubDate>
      <link>https://dev.to/a_reiterer/a-developers-way-into-modern-web-development</link>
      <guid>https://dev.to/a_reiterer/a-developers-way-into-modern-web-development</guid>
      <description>&lt;p&gt;&lt;em&gt;I developed enterprise software (.NET) for about six years, when I started to dive into web development. Today I’m going to tell you about the journey, what I could have done better, and what I plan for theÂ future.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  How everything started
&lt;/h1&gt;

&lt;p&gt;I always liked the idea of becoming a web developer. At 14–18, when I was still in schoolÂ¹, I liked to fiddle around with HTML and CSS and build smaller websites with it. Altough I wasn’t very good at it, it catched my interest. Then I started working as a software developer for enterprise software and didn’t do much regarding web development for the next few years, until I had to write a thesis for my part time study, where I chose to write about JavaScript Frameworks, and how they affect software quality of single page applications in comparison to using Vanilla JavaScript.&lt;/p&gt;

&lt;h1&gt;
  
  
  Angular and the MEANÂ stack
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;I thought if I just focus on doing one tutorial after another, I would be able to create something on my own soon&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I really liked what I learned while writing that thesis, so I continued learning about AngularJS (back then it was Angular 1) and focused on doing tutorials to learn more about it. At some point I decided that I want to know how to build a backend to a simple angular app. This was also the point when I discovered something called the MEAN stack. I was amazed how all of this works together. I liked it. So I did a bunch of full stack tutorials. I thought if I just focus on doing one tutorial after another, I would be able to create something on my own soon. Oh boy I was soooo wrong.&lt;/p&gt;

&lt;h1&gt;
  
  
  Angular 2
&lt;/h1&gt;

&lt;p&gt;After spending some months doing tutorials in the evening some days a week, Angular 2 was in late beta and there were rumors that the first release candidate would be published soon. Okay… new is always better (&lt;a href="https://www.youtube.com/watch?v=rxBvgCyERkw"&gt;wink ;)&lt;/a&gt;), so I had a look at Angular 2 which was a great experience. I liked the concept of components and the whole thing felt a bit cleaner to work with. So again, I did a bunch of tutorials until a colleague told me about an idea and asked if it was possible to build it. The first time since I started learning about web development, I could build something real. Something that could be useful some day. Almost every workday after work I would come home and spend the evening trying to build that web application with that MEAN stack I learned before. And suddenly I realized that I don’t know shit about how to do all of those things. Basically everything I learned in all those tutorials was lost.&lt;/p&gt;

&lt;p&gt;I told myself that was because I just have to get used to all that new stuff and it will get better if I build more and more things. Over the next months, I spent a massive amount of time on relearning everything, and growing my application step by step. All of that, while having to keep up with regular updates to the Angular 2 release candidate. Almost each of them had a lot of breaking changes, which meant a lot of work for me as a beginner.&lt;/p&gt;

&lt;h1&gt;
  
  
  React
&lt;/h1&gt;

&lt;p&gt;One sunny day when another Angular 2 RC was released with a backlog of breaking changes, it was enough. I threw everything away, just to learn ReactJS and start all over again. My friends told me good things about React before and it is backed by Facebook, so what could possibly go wrong? (I feared that small frameworks may get abandoned, so I focused on the ones that are backed by big companies) Learning React was awesome. Within short time I was able to build some cool little stuff and run it locally. Learning was a lot easier and faster this time because I already knew the JavaScript basics. Now that I was comfortable with the new framework, I focused on learning how to make a production ready web application with React, MongoDB, NodeJS and Express. (Of course that was too much new technologies for a beginner)&lt;/p&gt;

&lt;p&gt;I was overwhelmed of how much stuff you need to get that thing up and running. Apparently, Webpack and Babel were things you must know to create a state of the art application so I tried to get into that. Fortunately that was around the time when create-react-app was released. Yippieh! No longer fumbling around with cryptical configs! Now I could just focus on learn how to get this thing running because create-react-app can also be used for creating production ready applications.&lt;/p&gt;

&lt;p&gt;Since that day I’m building little things for myselfâ€Š–â€Šthings that never got published because I never finished them. All for the purpose of learning and building experience. And although I still don’t know everything about React, web development, backend development, I’m comfortable enough with it to create something real, something shippable, which I’d like to do in near future.&lt;/p&gt;

&lt;p&gt;Now why did I tell you all of this? Because I made a terrible mistake. A mistake that held me back from gaining experience and growing faster into my desired role of a web developer: I did one tutorial after another. As soon as I finished one, I already started the next one without even thinking about what I just learned.&lt;/p&gt;

&lt;h1&gt;
  
  
  Takeaways - What could I have done better?
&lt;/h1&gt;

&lt;p&gt;I wrote this article for people who think about becoming a web developer but don’t know where to start. As the way I did it in the beginning was not a good one, I want to tell you how to do better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't do too much tutorials
&lt;/h2&gt;

&lt;p&gt;Takeaway number one of this article: Don’t focus too much on tutorials. They are great for getting a glimpse of a concept. But after you finished it, think about what you learned. Try to build something small on your own where you use this newly acquired skills. E.g.:Â &lt;a href="https://medium.com/u/3f6706a859f8"&gt;Dave Ceddia&lt;/a&gt;Â wrote in one of hisÂ &lt;a href="https://daveceddia.com/learn-react-with-copywork/"&gt;articles&lt;/a&gt;, you should copy existing things. Start small, but build something. It’s all about the practice.Â When following a tutorial you will think you understand, but as long as you don’t build something on your own, you don’t.Â Newly acquired skills have to be practiced, otherwise you will simply forget them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get an overview, start with the basics and build!
&lt;/h2&gt;

&lt;p&gt;Before starting, think about what you want to do. Which framework do you want to learn? What about the eco system?&lt;/p&gt;

&lt;p&gt;For example, if you want to learn React, I would recommend to start at the basics. If you don’t have experience withÂ &lt;strong&gt;JavaScript&lt;/strong&gt;, learn the basics. LearnÂ &lt;strong&gt;ES2015/ES6&lt;/strong&gt;, you will need it. After that, make yourself familiar with theÂ &lt;strong&gt;NPM package manager&lt;/strong&gt;Â orÂ &lt;strong&gt;Yarn&lt;/strong&gt;. Choose the one which feels the best for you.&lt;br&gt;
Now that you covered the basics, have a look at the official tutorial forÂ &lt;strong&gt;React&lt;/strong&gt;Â it covers a lot of things that you’ll need later. As soon as you finished, think of something small, and build it. Visit a page you are familiar with (e.g. Twitter, Facebook, etc.) and build a component that shows a tweet or a facebook entry. Try to copy it as exact as possible. Then, try to render a list of posts / tweets. If you managed to do that, you could try to connect your app to the Twitter or Facebook API (or maybe you find another API). That’s a good point to learn about how APIs work and how to use them within your project.&lt;/p&gt;

&lt;p&gt;The key is to know when you need to know what. Start at the basics and try to use as much as you can in smaller projects. Don’t think of a fully grown scalable full stack application ready to use by a hundred thousand users. Just create something small for practicing and throw it away afterwards. If you are comfortable enough with what you do, level up and add some difficulty. Be it a connection to an API or server side rendering with code splitting. Just keep improving and growing.&lt;/p&gt;

&lt;h1&gt;
  
  
  Wrapping up
&lt;/h1&gt;

&lt;p&gt;I love to learn about new technologies. But at some you are just overwhelmed by the amount of new frameworks and libraries. That was the moment for me where I realized that I just can’t learn and try everything. So I sticked to the basics and improved all the way until today. And there is still so much to learn. I never really learned about server side rendering, so that’s what I’m going to do in the future. It looks like I will have to learn about Webpack and Babel first, because Webpack’s code splitting functionality. And the most important thing: I will learn all of this by building my own stuff. As soon as I’m comfortable with it, I’m going to write about it. That’s how I manage to keep things stuck in my head.&lt;/p&gt;

&lt;p&gt;EDIT: As a more detailed extension to this article, I would totally recommend to readÂ &lt;a href="https://goshakkk.name/next-steps-official-react-tutorial/"&gt;this article&lt;/a&gt;Â byÂ &lt;a href="https://medium.com/@goshakkk"&gt;Gosha Arinich&lt;/a&gt;. (TL;DR: Learn one thing at a time, practice with learning projects, don’t care about best practices in the beginning, don’t try to keep up with every new library or concept) I wish I had read such an article before I started :-D&lt;/p&gt;

&lt;p&gt;PS: I also want to add, that although I switched from Angular to React, I don’t think that Angular is bad. Both are great frameworks and I think each of them solves a lot of problems. Also I’m going to have a look at Angular in the next months since I get to use it in my daytime job. I’m really looking forward to get a better comparison now that I have more experience.&lt;/p&gt;

&lt;h1&gt;
  
  
  Call to Action
&lt;/h1&gt;

&lt;p&gt;You also want to get a better developer? I'm sharing what I know on my &lt;a href="http://www.andreasreiterer.at"&gt;blog&lt;/a&gt;, and if you subscribe to my weekly &lt;a href="http://www.andreasreiterer.at/weekly-webdev-newsletter"&gt;newsletter&lt;/a&gt;, I will deliver tips and tricks about web development right into your inbox. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was originally published at &lt;a href="http://www.andreasreiterer.at/web-development/developers-way-web-development/"&gt;http://www.andreasreiterer.at&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;[1] I went to a school called HTL (Hoehere Technische Lehranstalt) is a secondary education school that permits students to acquire the university entry qualification and professional training at the same time. Iâ€˜m not sure if something like this exists outside of Austriaâ€Š–â€Šat least not with that name.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tips</category>
    </item>
    <item>
      <title>How to use React Lifecycle Methods</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Fri, 15 Sep 2017 20:43:59 +0000</pubDate>
      <link>https://dev.to/a_reiterer/how-to-use-react-lifecycle-methods</link>
      <guid>https://dev.to/a_reiterer/how-to-use-react-lifecycle-methods</guid>
      <description>&lt;p&gt;&lt;em&gt;React lifecycle methods can be confusing if you don’t know which one to use for your particular use case. Today I’m going to show you, which lifecycle methods exist, and how to use them correctly.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;React components have several “lifecycle methods” that allow us to execute actions (e.g.: fetching data from a server) at particular times. When I started learning React, I found it hard to figure out which lifecycle method i should use for certain actions. If this is the case with you too, this article should serve as a handy guide.&lt;/p&gt;

&lt;p&gt;I will start with an overview of all lifecycle methods and explain in which order they are called. Then I’ll handle each of them with a short explanation and some example use cases. In the end, you should have a better understanding of when to use which life cycle method.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Lifecycle of a React Component
&lt;/h2&gt;

&lt;p&gt;Let’s begin with the lifecycle of a component according to the React docs. There are three particular stages in the lifecycle of a component, that are important for our lifecycle methods, which I will explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mount&lt;/li&gt;
&lt;li&gt;Update&lt;/li&gt;
&lt;li&gt;Unmount&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Mount
&lt;/h3&gt;

&lt;p&gt;When React creates an instance of a component and inserts it into the DOM (&lt;em&gt;mounting&lt;/em&gt;), the following methods are called:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;constructor()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;componentWillMount()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;render()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;componentDidMount()&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Update
&lt;/h3&gt;

&lt;p&gt;If props or state of a component are changed for whatever reason, an update of the component is performed. However, this means that the component has to be re-rendered, which causes the following methods to be called:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;componentWillReceiveProps()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;shouldComponentUpdate()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;componentWillUpdate()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;render()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;componentDidUpdate()&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Unmount
&lt;/h3&gt;

&lt;p&gt;At some point our components will be removed from the DOM again. That process is called &lt;em&gt;unmounting&lt;/em&gt; and means that the following method is called:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;componentWillUnmount&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  React Component Lifecycle Summary
&lt;/h3&gt;

&lt;p&gt;I hope I could give you a short overview of the life of a React component and the calling order of lifecycle methods. Just for a compact overview, here’s a list of all lifecycle methods in the correct order.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;componentWillMount&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;componentDidMount&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;componentWillReceiveProps&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;shouldComponentUpdate&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;componentWillUpdate&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;componentDidUpdate&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;componentWillUnmount&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can see, they’re not that many. However, it is important that you choose the right one for different use cases to prevent side effects or errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lifecycle Methods
&lt;/h2&gt;

&lt;p&gt;In this section, we are going to explore the different lifecycle methods. I will explain each of them in detail and I’ll do my best to provide different example use cases for a better understanding.&lt;/p&gt;

&lt;h3&gt;
  
  
  componentWillMount()
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentWillMount()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever React renders a component, it’s going to call c_omponentWillMount_ first. Note that this method is only called once in a life of a component, and this is right before it is initially. Therefore, there is &lt;strong&gt;no access to the DOM. &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Because componentWillMount is called before the &lt;em&gt;render()&lt;/em&gt; method, this is the only lifecycle method that is called on the server side, when you use serverside rendering.&lt;/p&gt;

&lt;p&gt;Alternatively to this lifecycle hook, the React docs recommend using the constructor instead.&lt;/p&gt;

&lt;h4&gt;
  
  
  State
&lt;/h4&gt;

&lt;p&gt;You can use &lt;em&gt;this.setState(…)&lt;/em&gt; inside this method. However, be aware that it &lt;a href="https://facebook.github.io/react/docs/react-component.html#componentwillmount"&gt;&lt;strong&gt;may not trigger a re-rendering&lt;/strong&gt;&lt;/a&gt; when you set the state &lt;strong&gt;synchronously&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you can, I would suggest to set the default state inside the constructor instead of setting the state here.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use cases
&lt;/h4&gt;

&lt;p&gt;I did not find much example use cases for componentWillMount. Some people suggest to use it for doing some configuration of the root component that you can only do at runtime (e.g.: &lt;a href="https://engineering.musefind.com/react-lifecycle-methods-how-and-when-to-use-them-2111a1b692b1"&gt;setting up a Firebase connection&lt;/a&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  componentDidMount
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentDidMount()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever this method is called, React has already rendered our component and put it into the DOM. Therefore, if there is any initialization you want to perform that relies on the DOM, do it here and now.&lt;/p&gt;

&lt;h4&gt;
  
  
  State
&lt;/h4&gt;

&lt;p&gt;You can set the state with &lt;em&gt;this.setState()&lt;/em&gt;. Whenever you do this, it will also trigger a re-render of the component.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use Cases
&lt;/h4&gt;

&lt;p&gt;You can use componentDidMount to &lt;strong&gt;fetch data from a server&lt;/strong&gt; with AJAX calls. Also if you need to  &lt;strong&gt;initialize anything that relies on the DOM,&lt;/strong&gt; you can do this here (e.g. initializing third party libraries like D3). And last but not least, you can &lt;strong&gt;add event listeners&lt;/strong&gt; inside componentDidMount.&lt;/p&gt;

&lt;h3&gt;
  
  
  componentWillReceiveProps
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentWillReceiveProps(nextProps)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever a component receives a new set of props, this method will be called first. Also, please note, that React calls this method, even when the props have not changed. So whenever you use this method, be sure to compare &lt;em&gt;this.props&lt;/em&gt; to &lt;em&gt;nextProps&lt;/em&gt; to avoid setting the state unnecessarily.&lt;/p&gt;

&lt;p&gt;React doesn’t call this method in the &lt;strong&gt;mount&lt;/strong&gt; process. Instead, it only calls this method, if some of the component’s props may update.&lt;/p&gt;

&lt;h4&gt;
  
  
  State
&lt;/h4&gt;

&lt;p&gt;You can set the state by using &lt;em&gt;this.setState()&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Use Cases
&lt;/h4&gt;

&lt;p&gt;If you have a state that is a calculation from multiple props, you could do the calculation here. Don’t forget to check if your relevant props have really changed (compare &lt;em&gt;this.props&lt;/em&gt; to &lt;em&gt;nextProps&lt;/em&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  shouldComponentUpdate
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shouldComponentUpdate(nextState, nextProps)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, this method is not implemented, so every update of state or props causes a render, even if the props didn’t change. However, if you want to avoid possible unnecessary renders, you could handle this here. Returning &lt;em&gt;false&lt;/em&gt; means, that React will not execute &lt;em&gt;componentWillUpdate()&lt;/em&gt;, &lt;em&gt;render()&lt;/em&gt; and &lt;em&gt;componentDidUpdate()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This method is not called for the initial render.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; According to the React docs, React may treat shouldComponentUpdate like a hint instead of strictly following it’s return value. This means, it could be possible that the method returns &lt;em&gt;false&lt;/em&gt; but React still decides to re-render the component.&lt;/p&gt;

&lt;h4&gt;
  
  
  State
&lt;/h4&gt;

&lt;p&gt;You can’t call &lt;em&gt;setState&lt;/em&gt; here. Also, it wouldn’t make much sense to do so. If you want to set the state because of changing props, use &lt;em&gt;componentWillReceiveProps&lt;/em&gt; instead.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use Case
&lt;/h4&gt;

&lt;p&gt;As already mentioned, you can check, if the update of props or state really affects the output of the component. To do so, you could do a comparison of the current props/state to the next props/state. If the component shouldn’t update, just return false and the component won’t update.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: &lt;/strong&gt; This might lead to serious side effects. React also provides another solution for this use case: If you notice that a certain component is slow, you can inherit it from &lt;em&gt;React.PureComponent&lt;/em&gt; instead of &lt;em&gt;React.Component&lt;/em&gt;. It will perform a shallow comparison for props and state, which might work for most of the use cases I can imagine right now.&lt;/p&gt;

&lt;h3&gt;
  
  
  componentWillUpdate
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentWillUpdate(nextProps, nextState)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method is invoked right before rendering. Like &lt;em&gt;shouldComponentUpdate&lt;/em&gt;, it is called whenever new props are passed to the component, or the state is changed.&lt;/p&gt;

&lt;p&gt;This method is not called for the initial render.&lt;/p&gt;

&lt;h4&gt;
  
  
  State
&lt;/h4&gt;

&lt;p&gt;You can’t call &lt;em&gt;setState&lt;/em&gt; here. Again, if you want to set the state because of changing props, use &lt;em&gt;componentWillReceiveProps&lt;/em&gt; instead.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use Cases
&lt;/h4&gt;

&lt;p&gt;You can perform preparations that need to be done before updating the component. This lifecycle method is called right before render(), so you should not do anything that relies on the DOM – it will soon be outdated.&lt;/p&gt;

&lt;p&gt;Common use cases seem to be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;set a variable based on state changes&lt;/li&gt;
&lt;li&gt;dispatching events&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/33075063/what-is-the-exact-usage-of-componentwillupdate-in-reactjs/33075514#33075514"&gt;starting animations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  componentDidUpdate
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentDidUpdate(prevProps, prevState)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yay! Everything went well, and React updated our component. Directly after rendering, React also calls &lt;em&gt;componentDidUpdate&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This method is not called for the initial render.&lt;/p&gt;

&lt;h4&gt;
  
  
  State
&lt;/h4&gt;

&lt;p&gt;You can use &lt;em&gt;setState&lt;/em&gt; here.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use Cases
&lt;/h4&gt;

&lt;p&gt;If there is something you have to do with the DOM right after the component has been updated, this is the time and place for it. A good example for this would be the update of a 3rd party UI library like D3 to pass on the new data.&lt;/p&gt;

&lt;p&gt;It is also a good place to &lt;strong&gt;perform network requests&lt;/strong&gt; , as long as you compare the current state/props with the previous state/props to avoid unnecessary network requests.&lt;/p&gt;

&lt;h3&gt;
  
  
  componentWillUnmount
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentWillUnmount()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Right before React unmounts and destroys our component, it invokes &lt;em&gt;componentWillUnmount&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  State
&lt;/h4&gt;

&lt;p&gt;You can’t set state before unmounting the component.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use Cases
&lt;/h4&gt;

&lt;p&gt;Use this hook to perform clean up actions. This could be&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;removing event listeners you added in &lt;em&gt;componentDidMount&lt;/em&gt; (or elsewhere)&lt;/li&gt;
&lt;li&gt;cancelling active network requests&lt;/li&gt;
&lt;li&gt;invalidating timers&lt;/li&gt;
&lt;li&gt;cleaning up DOM elements that you created in &lt;em&gt;componentDidMount&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Today you’ve learned, that the lifecycle of a React component consists of three stages: Mounting, Updating and Unmounting.&lt;/p&gt;

&lt;p&gt;Also you’ve learned that React calls a certain set of lifecycle methods at each of those stages. You can use them according to the use case you want to fulfill.&lt;/p&gt;

&lt;p&gt;Thank you for reading this article. I really hope you enjoyed it. Also, I would really appreciate it if you share this article with your friends.&lt;/p&gt;

&lt;p&gt;If there is something you want to add, or if you just want to chat about dev stuff, hook me up on &lt;a href="https://www.twitter.com/a_reiterer"&gt;Twitter&lt;/a&gt;, or send an email to &lt;a href="//mailto:hi@andreasreiterer.at"&gt;hi@andreasreiterer.at&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Call to Action
&lt;/h1&gt;

&lt;p&gt;You also want to become a better developer? I'm sharing what I know on my blog, and if you subscribe to my &lt;a href="//www.andreasreiterer.at/weekly-webdev-newsletter/"&gt;weekly newsletter&lt;/a&gt;, I will deliver more tips and tricks about React and other web development articles right into your inbox.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The post &lt;a href="http://www.andreasreiterer.at/web-development/reactjs-lifecycle-methods/"&gt;How to use React Lifecycle Methods&lt;/a&gt; appeared first on &lt;a href="http://www.andreasreiterer.at"&gt;my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>A Simple Cheatsheet for Flexbox</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Wed, 16 Aug 2017 15:00:38 +0000</pubDate>
      <link>https://dev.to/a_reiterer/a-simple-cheatsheet-for-flexbox-76b</link>
      <guid>https://dev.to/a_reiterer/a-simple-cheatsheet-for-flexbox-76b</guid>
      <description>&lt;p&gt;&lt;em&gt;An overview on how to use the flexible box layout (short: flexbox) &lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I spent the last days wrapping my head around  &lt;strong&gt;flexbox&lt;/strong&gt; , and how to use it correctly. Maybe you can relate to when I say, that it is not that easy to understand how it exactly works, or why some CSS rules don’t behave as expected under certain circumstances.&lt;/p&gt;

&lt;p&gt;For me the following articles helped a lot to understand how flexbox works and what to consider when using it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://medium.freecodecamp.org/understanding-flexbox-everything-you-need-to-know-b4013d4dc9af" rel="noopener noreferrer"&gt;Understanding Flexbox: Everything you need to know&lt;/a&gt; (&lt;a href="https://medium.com/@ohansemmanuel" rel="noopener noreferrer"&gt;Ohans Emmanuel&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://hackernoon.com/11-things-i-learned-reading-the-flexbox-spec-5f0c799c776b" rel="noopener noreferrer"&gt;11 things I learned reading the flexbox spec&lt;/a&gt; (&lt;a href="https://medium.com/@david.gilbertson" rel="noopener noreferrer"&gt;David Gilbertson&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, I just want to give a summary of what I think is important to know. So please keep in mind that this will not be an exhaustive description of all possible CSS rules and scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s flexbox?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Flexbox is a layout mode which is designed for laying out more complex applications and webpages.â€Š–â€Š&lt;a href="https://www.w3.org/TR/css-flexbox-1/#intro" rel="noopener noreferrer"&gt;w3.org&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When using the flexbox layout model we distinguish between the two main elements: flex container and flex items.&lt;/p&gt;

&lt;p&gt;The flex container is the parent element of your flex items. You set &lt;code&gt;display: flex&lt;/code&gt; or &lt;code&gt;display: inline-flex&lt;/code&gt; on this element to activate the flex layout mode.&lt;/p&gt;

&lt;p&gt;Before outlining the details of a flex container or flex items, I want to talk about the terminology of the flex box layout model, which I found somehow confusing in the beginning. Here’s what I found about that in the &lt;a href="https://www.w3.org/TR/css-flexbox-1/#box-model" rel="noopener noreferrer"&gt;flexbox specification&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1250%2F1%2AdH2D2U0yOf4a2Z_OYxRsKg.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%2Fcdn-images-1.medium.com%2Fmax%2F1250%2F1%2AdH2D2U0yOf4a2Z_OYxRsKg.png"&gt;&lt;/a&gt;An illustration of the various directions, sizing and positioning terms as applied to a â€˜row’ flex container.&lt;/p&gt;

&lt;p&gt;First of all, in flexbox layout there is no &lt;em&gt;horizontal&lt;/em&gt; or &lt;em&gt;vertical&lt;/em&gt;. Instead, we orientate ourselves by a &lt;em&gt;main axis&lt;/em&gt; and a &lt;em&gt;cross axis&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Flex items are always laid out along the  &lt;strong&gt;main-axis&lt;/strong&gt;. In the image above, you can see, that the main-axis for a &lt;em&gt;row&lt;/em&gt; flex container goes from the left end of the flex container ( &lt;strong&gt;&lt;em&gt;main start&lt;/em&gt;&lt;/strong&gt; ) to the right end ( &lt;strong&gt;&lt;em&gt;main end&lt;/em&gt;&lt;/strong&gt; ).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Basically, a column flex container is just a row flex container tilted by 90 degrees to the right.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F750%2F1%2A_G8vYJ2P_l1CeRETSx43NQ.jpeg" 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%2Fcdn-images-1.medium.com%2Fmax%2F750%2F1%2A_G8vYJ2P_l1CeRETSx43NQ.jpeg"&gt;&lt;/a&gt;An illustration of the various directions, sizing and positioning terms as applied to a â€˜column’ flex container.&lt;/p&gt;

&lt;p&gt;For a &lt;em&gt;column&lt;/em&gt; flex container things would look a bit different.&lt;/p&gt;

&lt;p&gt;As you can see in the image on the left, as soon as you use &lt;code&gt;flex-direction: column&lt;/code&gt;, the &lt;em&gt;main-axis&lt;/em&gt; points towards the bottom of the screen, while the &lt;em&gt;cross-axis&lt;/em&gt; goes from the right end to the left end of the flex container. So basically the whole picture is just tilted to the right, by 90 degrees.&lt;/p&gt;

&lt;p&gt;This helped me a lot to orientate myself within a column flex container, since the CSS properties change their effective direction between &lt;em&gt;row_and _column&lt;/em&gt; flex containers.&lt;/p&gt;

&lt;p&gt;Now that’s all we need for now. In the following sections I’m going to describe the different CSS rules which are applicable for flex container and flex items.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Flex Container CSS Rules
&lt;/h3&gt;

&lt;p&gt;Let’s start with the CSS rules which are used on the flex container. I included some screenshots and put the respective CSS setting into the caption of the images.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;flex-direction&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This rule determines if the flex-items are aligned in a row or in a column. Additionally, there is an option to align them in reverse order, heading from main-end to main-start.&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AamKZ2KxBD-1huE56oIHHiQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AamKZ2KxBD-1huE56oIHHiQ.png"&gt;&lt;/a&gt;flex-direction: row; (default)&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2ALSl2HQ6FcCfqwVTIpHsiHA.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2ALSl2HQ6FcCfqwVTIpHsiHA.png"&gt;&lt;/a&gt;flex-direction: column;&lt;/p&gt;

&lt;h4&gt;
  
  
  flex-wrap
&lt;/h4&gt;

&lt;p&gt;If you add more flex-items to a line, you will get to the point where there is not enough space for more. Since the flex-items shrink by default (see: flex-shrink the flex-items section) the items will get smaller the more items you add. By using flex-wrap you can control this behaviour if the items should stay in the same line (this is the default behaviour; &lt;code&gt;flex-wrap: nowrap;&lt;/code&gt;) or wrap to the next line ( &lt;code&gt;flex-wrap: wrap;&lt;/code&gt; or &lt;code&gt;flex-wrap: wrap-reverse;&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AQ2Qc5OiAJrWhgWF6BHYIVQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AQ2Qc5OiAJrWhgWF6BHYIVQ.png"&gt;&lt;/a&gt;flex-wrap: nowrap; as as applied to a â€˜row’ flex-container&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A4mpb0H6Dn2UK5uLFD9_rHg.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A4mpb0H6Dn2UK5uLFD9_rHg.png"&gt;&lt;/a&gt;flex-wrap: wrap;&lt;/p&gt;

&lt;h4&gt;
  
  
  flex-flow
&lt;/h4&gt;

&lt;p&gt;This is the first shorthand I will show you today. You can combine the CSS rules &lt;em&gt;flex-direction&lt;/em&gt; and &lt;em&gt;flex-wrap&lt;/em&gt; within one single CSS rule. The following list should speak for itself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;flex-flow: row wrap;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;flex-flow: row wrap-reverse;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;flex-flow: column wrap;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;flex-flow: column wrap-reverse;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  justify-content
&lt;/h4&gt;

&lt;p&gt;To position the flex-items alongside the main axis, you can use &lt;em&gt;justify-content&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AGoU8Z5Jk0VZvgchlAoFZgQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AGoU8Z5Jk0VZvgchlAoFZgQ.png"&gt;&lt;/a&gt;justify-content: flex-start; (default behaviour)&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AGcJfvAx72fkZJaWiAxL6ug.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AGcJfvAx72fkZJaWiAxL6ug.png"&gt;&lt;/a&gt;justify-content: flex-end;&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AbqgVSv8CYbd5sbqnHomfcQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AbqgVSv8CYbd5sbqnHomfcQ.png"&gt;&lt;/a&gt;justify-content: center;&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2Axn5xnkoUNMtXcfsIs92JNw.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2Axn5xnkoUNMtXcfsIs92JNw.png"&gt;&lt;/a&gt;justify-content: space-between;&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2ADSt6QAuAXPl4EpPKGpR7Gw.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2ADSt6QAuAXPl4EpPKGpR7Gw.png"&gt;&lt;/a&gt;justify-content; space-around;&lt;/p&gt;

&lt;h4&gt;
  
  
  align-items
&lt;/h4&gt;

&lt;p&gt;Now that you learned about justify-content to lay out items along the main axis, this is an easier one. &lt;em&gt;align-items&lt;/em&gt; is used to align items  &lt;strong&gt;along the cross-axis&lt;/strong&gt;   &lt;strong&gt;on the current line&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A_1XwY-xQJU6v82fKGueYwg.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A_1XwY-xQJU6v82fKGueYwg.png"&gt;&lt;/a&gt;align-items: flex-start; (default behaviour)&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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AMQyjqpuA3eIMUJEgUuecCg.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AMQyjqpuA3eIMUJEgUuecCg.png"&gt;&lt;/a&gt;align-items: flex-end;&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2Ai9K2YX_EBDtxxmHP0FT2sg.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2Ai9K2YX_EBDtxxmHP0FT2sg.png"&gt;&lt;/a&gt;align-items: center;&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2Ad3yXuFZNDYal8GDv_NFUJA.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2Ad3yXuFZNDYal8GDv_NFUJA.png"&gt;&lt;/a&gt;align-items: stretch;&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2ADqQEdsCtWhfjWseSz8P_xQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2ADqQEdsCtWhfjWseSz8P_xQ.png"&gt;&lt;/a&gt;align-items: baseline;&lt;/p&gt;

&lt;p&gt;As you see, &lt;em&gt;align-items&lt;/em&gt; also allows to stretch items to use the available space in the current line, as well as &lt;code&gt;align-items: baseline;&lt;/code&gt;aligns items along the bottom line of their first line of text.&lt;/p&gt;

&lt;p&gt;But what do you do if you don’t want to align the items within one line, but rather align the whole bunch of items within the available space of the flex-container? That’s what &lt;em&gt;align-content&lt;/em&gt; is used for.&lt;/p&gt;

&lt;h4&gt;
  
  
  align-content
&lt;/h4&gt;

&lt;p&gt;Let’s have a look how we can align the whole bunch of &lt;em&gt;flex-items&lt;/em&gt; within our flex container:&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AaK6Y1x6s6tmNY68wjgEA9w.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AaK6Y1x6s6tmNY68wjgEA9w.png"&gt;&lt;/a&gt;align-content: flex-start; (default behaviour)&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AN3qM1kVTqPyqB6yWzcqPFg.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AN3qM1kVTqPyqB6yWzcqPFg.png"&gt;&lt;/a&gt;align-content: flex-end;&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AhX2F70c0MdZf2Xhof6E37g.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AhX2F70c0MdZf2Xhof6E37g.png"&gt;&lt;/a&gt;align-content: center;&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AiTMcb2NayM5YdDhWYCSEsA.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AiTMcb2NayM5YdDhWYCSEsA.png"&gt;&lt;/a&gt;align-conent; stretch;&lt;/p&gt;

&lt;p&gt;That was a rather short overview of what you are capable to do with flex-container CSS rules. In the next section I’m going to show you how you can adjust the behaviour of flex-items.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Flex Item CSS Rules
&lt;/h3&gt;

&lt;p&gt;In the last section you learned how to align all flex items at onceâ€Š–â€Šeither within their line or within the available space of their flex container. Now it’s time to have a look at &lt;code&gt;flex-grow&lt;/code&gt; ,&lt;code&gt;flex-shrink&lt;/code&gt; ,&lt;code&gt;flex-basis&lt;/code&gt; and the &lt;code&gt;flex&lt;/code&gt; shorthand for those three rules.&lt;/p&gt;

&lt;h4&gt;
  
  
  flex-grow
&lt;/h4&gt;

&lt;p&gt;Defines if items cang row to use the available space along the main-axis. The default value is 0, which means that flex-items don’t grow automatically. In the following pictures you can see which effect flex-grow has on flex items in a â€˜row’ and in a â€˜column’ container.&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A8I6rq-lPe9cUWyxtOU6ZsA.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A8I6rq-lPe9cUWyxtOU6ZsA.png"&gt;&lt;/a&gt;flex-grow: 0; (default behaviour) in a â€˜row’ container&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AlSrfcRMbjsWOzZEmPIG-MQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AlSrfcRMbjsWOzZEmPIG-MQ.png"&gt;&lt;/a&gt;flex-grow: 1; in a â€˜row’ container&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AX0P2Z46934QObV7nskeGCQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AX0P2Z46934QObV7nskeGCQ.png"&gt;&lt;/a&gt;flex-grow: 0; (default behaviour) in a â€˜column’ container&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AAk6iZ36xtiO98loOmWUEfA.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AAk6iZ36xtiO98loOmWUEfA.png"&gt;&lt;/a&gt;flex-grow: 1; in a â€˜column’ container&lt;/p&gt;

&lt;p&gt;With &lt;em&gt;flex-grow&lt;/em&gt; it is also possible to let certain items grow more than others.&lt;/p&gt;

&lt;p&gt;Example: We have three items. Two of them have flex-grow set to 1, while one of them got flex-grow set to 2.&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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AWFNQhpqX_7GPh0SD4kJ3Tg.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AWFNQhpqX_7GPh0SD4kJ3Tg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What happened here? First of all, all flex-grow values of a line are added, which results in a total of 4 which represents 100% of our available space.&lt;/p&gt;

&lt;p&gt;Then each item is sized according to the relational size of its flex-grow setting to the sum of all flex-grow settings of said line. According to that calculation, the first two items get 25% of the available space and the third one gets 50%. Please note that I did not consider the padding and margin which is set in the image.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;flex-shrink&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;By default, the shrink rule is set to 1. This means that as soon as the window is resized, the flex items will shrink if necessary. Possible values are 0 and 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;  The flex item of the following picture has a width of 500px. With flex-shrink set to it’s default value 1, resizing the window means that the item will shrink to less than 500px width. When setting flex-shrink to 0, the item will not shrink and would exceed the window size and a scrollbar appears.&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2Ay-xL7-dpnWZs7oGwbpUUIQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2Ay-xL7-dpnWZs7oGwbpUUIQ.png"&gt;&lt;/a&gt;The item has a width of 500px. (Sidenote: flex-grow is switchedÂ off)&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AyCL3eUCd-KC-emVKyPigJA.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AyCL3eUCd-KC-emVKyPigJA.png"&gt;&lt;/a&gt;flex-shrink is turned onâ€Š–â€Šresizing the window also resizes theÂ item.&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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AxcDvBTj8-fsuiqPlHE%2520__sw.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AxcDvBTj8-fsuiqPlHE%2520__sw.png" alt=""&gt;&lt;/a&gt;flex-shrink turned off. The item exceeds the window (scrollbars omitted in thisÂ picture)&lt;/p&gt;




&lt;h4&gt;
  
  
  flex-basis
&lt;/h4&gt;

&lt;p&gt;The flex basis defines the initial &lt;em&gt;main-size&lt;/em&gt; of a flex item. This setting will be evaluated before the available space is distributed to flex-grow, or the items are resized by flex-shrink.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The initial main size of the flex item before free space is distributed to the flex factors.â€Š–â€Š&lt;a href="https://www.w3.org/TR/css-flexbox-1/#flex-basis-property" rel="noopener noreferrer"&gt;w3.org&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;flex-basis accepts either a length in &lt;code&gt;px || em || rem&lt;/code&gt; or percentages, as well as &lt;code&gt;auto&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If flex-basis is set to &lt;code&gt;auto&lt;/code&gt;, the width will be calculated based on content size. So the more you increase the content of a flex item, the bigger it gets.&lt;/p&gt;

&lt;p&gt;As soon as you set a fixed size to flex-basis, the content of the item will adjust to fit into the item.&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AjiePFv0eUl_DqRjCiP7Q2Q.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AjiePFv0eUl_DqRjCiP7Q2Q.png"&gt;&lt;/a&gt;flex-basis: auto;&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AkCLeXxe2uimOLt0rjEZEGQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AkCLeXxe2uimOLt0rjEZEGQ.png"&gt;&lt;/a&gt;flex-basis: auto; with moreÂ content&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AwH-iTB9Hm2kDckwJYajaYw.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AwH-iTB9Hm2kDckwJYajaYw.png"&gt;&lt;/a&gt;flex-basis: 100px;&lt;/p&gt;

&lt;h4&gt;
  
  
  The flex shorthand
&lt;/h4&gt;

&lt;p&gt;Because it is way more comfortable to set the above rules all at once, there is a shorthand to do so: The default setting for this is &lt;code&gt;flex: 0 1 auto;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With the shorthand you define  &lt;strong&gt;&lt;em&gt;flex-grow&lt;/em&gt;&lt;/strong&gt;  with the  &lt;strong&gt;first&lt;/strong&gt; ,  &lt;strong&gt;&lt;em&gt;flex-shrink&lt;/em&gt;&lt;/strong&gt;  with the  &lt;strong&gt;second&lt;/strong&gt; , and &lt;strong&gt; &lt;em&gt;flex-basis&lt;/em&gt; &lt;/strong&gt; with the  &lt;strong&gt;third argument.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is also possible to omit the last parameter: &lt;code&gt;flex: 0 1&lt;/code&gt; which defaults flex-basis to zero_._&lt;/p&gt;

&lt;p&gt;The following pictures show some examples how you can arrange flex-items using the flex shorthand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;flex: 0 1 auto (default behaviour)&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The item size is defined by its content.&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A-_OHfGSjTwuF5GMDEuYMJg.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A-_OHfGSjTwuF5GMDEuYMJg.png"&gt;&lt;/a&gt;flex: 0 1 auto; (default behaviour)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;flex: 1 1 auto&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AdtjRc5zRutFWrHSsvSMugQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AdtjRc5zRutFWrHSsvSMugQ.png"&gt;&lt;/a&gt;flex: 1 1Â auto;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;flex: 0 0 auto&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
This is the same as the example in the above section “flex-shrink”. Shrinking is turned off, which leads to the item overflowing the window size.&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AC0yZxme_Vj0dIZXUtR1GYA.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AC0yZxme_Vj0dIZXUtR1GYA.png"&gt;&lt;/a&gt;flex: 0 0Â auto;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Relative size of flex-items&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
To use the same “relative” sizing technique I described in the section “flex-grow”, you have to set flex-basis to zero. In the next picture, the first item has &lt;code&gt;flex: 2 1 0;&lt;/code&gt; while the second item has &lt;code&gt;flex: 1 1 0;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AqFW-emQqaJGV1ePBrgdFzQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AqFW-emQqaJGV1ePBrgdFzQ.png"&gt;&lt;/a&gt;The first item got 2/3 of the available size while the second one gotÂ 1/3&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Well that article grew a bit bigger than it was planned, but in the end, I hope you enjoyed the read. Maybe this article can serve as a handy guide to look up the different CSS rules if you want to brush up your knowledge, as this is something one might not memorize completely.&lt;/p&gt;

&lt;p&gt;As I said in the beginning, this was never meant to be an exhaustive list of scenarios and rules which can apply, if you are interested in this in detail, I would suggest to read the articles I mentioned in the beginning of the article, as well as parts of the specification (if you don’t want to read the whole thing)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://medium.freecodecamp.org/understanding-flexbox-everything-you-need-to-know-b4013d4dc9af" rel="noopener noreferrer"&gt;Understanding Flexbox: Everything you need to know&lt;/a&gt; (&lt;a href="https://medium.com/@ohansemmanuel" rel="noopener noreferrer"&gt;Ohans Emmanuel&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://hackernoon.com/11-things-i-learned-reading-the-flexbox-spec-5f0c799c776b" rel="noopener noreferrer"&gt;11 things I learned reading the flexbox spec&lt;/a&gt; (&lt;a href="https://medium.com/@david.gilbertson" rel="noopener noreferrer"&gt;David Gilbertson&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/TR/css-flexbox-1/" rel="noopener noreferrer"&gt;CSS Flexible Box Layout Module Level 1&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At first, I planned to also create this as a handy cheatsheet, but then I found a  &lt;strong&gt;really good one&lt;/strong&gt;  by &lt;a href="https://medium.com/@chriscoyier" rel="noopener noreferrer"&gt;Chris Coyier&lt;/a&gt; which I want to share with you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://css-tricks.com/snippets/css/a-guide-to-flexbox/" rel="noopener noreferrer"&gt;A Complete Guide to Flexbox&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Call to Action
&lt;/h3&gt;

&lt;p&gt;You want to learn more about web development and become a better developer? Good news! Subscribe to my &lt;a href="//www.andreasreiterer.at/weekly-webdev-newsletter/"&gt;weekly newsletter&lt;/a&gt; and I will deliver high quality content about web development right into your inbox.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The post &lt;a href="http://www.andreasreiterer.at/web-development/flexbox-cheatsheet/" rel="noopener noreferrer"&gt;A Simple Cheatsheet for Flexbox&lt;/a&gt; appeared first on &lt;a href="http://www.andreasreiterer.at" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>cheatsheet</category>
      <category>css</category>
      <category>flexbox</category>
    </item>
    <item>
      <title>How to create responsive UI with styled-components</title>
      <dc:creator>Andreas Reiterer</dc:creator>
      <pubDate>Wed, 26 Jul 2017 15:05:43 +0000</pubDate>
      <link>https://dev.to/a_reiterer/how-to-create-responsive-ui-with-styled-components</link>
      <guid>https://dev.to/a_reiterer/how-to-create-responsive-ui-with-styled-components</guid>
      <description>&lt;p&gt;&lt;em&gt;There are several approaches on creating responsive web apps with ReactJS. Apart from using whole frameworks like Bootstrap, I wanted to create a responsive grid view with styled-components, which was amazingly painless.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now that &lt;a href="http://www.andreasreiterer.at/web-development/how-to-build-a-simple-hackernews-feed-with-styled-components/" rel="noopener noreferrer"&gt;I built an actual web app&lt;/a&gt; (admittedly, a tiny one) with ReactJS and styled-components, I wondered how easy it would be to create something more responsive. More precisely, I wondered how one would create a responsive page layout like it is easily done with Bootstrap, just to name an example. And that’s what we are going to build along this article, a simple grid view which allows us to split a web site into columns which will be stacked as soon as it is opened from a phone or tablet.&lt;/p&gt;

&lt;p&gt;Until this point, whenever I built a responsive web page layout, I always relied on Bootstrap, or react-bootstrap, when working with ReactJS. Why? Because I found it easy to use and it made development a little bit faster.&lt;/p&gt;

&lt;p&gt;But not this time. So I had a look on &lt;a href="https://www.w3schools.com/css/css_rwd_grid.asp" rel="noopener noreferrer"&gt;w3schools tutorial on building a responsive grid view&lt;/a&gt; to find out what happens behind the curtains of all those fancy libraries:&lt;/p&gt;

&lt;h4&gt;
  
  
  How does a responsive grid view work?
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;First of all, I want to give a short introduction how responsive grid views work. If you already know this, please feel free to skip this section.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Usually such grids have twelve columns which have a total width of 100 percent. Depending on the website’s layout you may not need all of them, so it should be possible to merge some columns together to get a bigger one. What really happens in the background is that there is a bunch of &lt;code&gt;div&lt;/code&gt; tags with equal width &lt;code&gt;float&lt;/code&gt;ing to the left, so they appear in a row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.column {
  width: 8.33%;
  float: left;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;EDIT: As stated by &lt;/em&gt;&lt;a href="https://medium.com/@ocorsomauro" rel="noopener noreferrer"&gt;&lt;em&gt;Mauro Lionel Ocorso&lt;/em&gt;&lt;/a&gt;&lt;em&gt; and &lt;/em&gt;&lt;a href="https://medium.com/@duthon.jean" rel="noopener noreferrer"&gt;&lt;em&gt;Jean Duthon&lt;/em&gt;&lt;/a&gt;&lt;em&gt; it is also possible to use &lt;/em&gt; &lt;strong&gt;&lt;em&gt;flexbox &lt;/em&gt;&lt;/strong&gt; &lt;em&gt;instead of &lt;/em&gt;&lt;code&gt;float: left;&lt;/code&gt;&lt;em&gt;. However there might be some compatibility issues if you want to support older browsers also. Thank you guys for pointing this out!&lt;/em&gt;&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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A6Pld5ZuXMOylEXP_1bSPjA.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A6Pld5ZuXMOylEXP_1bSPjA.png"&gt;&lt;/a&gt;A grid with twelveÂ columns&lt;/p&gt;

&lt;p&gt;Since the whole grid has a width of 100 percent, each of the columns are exactly 8.33% wide. Now let’s say we want a bigger column at the beginning of the grid, in the size of three columns or 25 percent of the screen.&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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2ALHlTLJZuNcrgOGg0ernv5A.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2ALHlTLJZuNcrgOGg0ernv5A.png"&gt;&lt;/a&gt;The first column spans across the first threeÂ columns&lt;/p&gt;

&lt;p&gt;In the picture above, you can see the first column spanning across three columns, which caused the second and third column to disappear. What really happened is not a &lt;em&gt;real&lt;/em&gt; column span. The first column got a width of 25 percent and columns 2 and 3 were deleted now that they are not needed anymore.&lt;/p&gt;

&lt;p&gt;To achieve this, some changes have to be made. Instead of having one CSS-class for each column, there is one for each possible column span. This is also reflected in the class names I used: While col-1 spans across one column, col-3 spans across three and col-12 would have a width of 100 percent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[class*="col-"] {
  float: left;
}

.col-1{
 width: 8.33%;
}

.col-2 {...}

.col-3{
 width: 25%;
}

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

&lt;/div&gt;



&lt;p&gt;The only thing missing now, is how we get to stack the columns as soon as we view our page from a smaller device. This can be achieved by using media queries (learn more about them &lt;a href="https://www.w3schools.com/css/css_rwd_mediaqueries.asp" rel="noopener noreferrer"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AONwpRoHIJA7FRnIErifF9A.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AONwpRoHIJA7FRnIErifF9A.png"&gt;&lt;/a&gt;The columns should stack vertically when viewed from a smallÂ device&lt;/p&gt;

&lt;p&gt;Let’s say we want to stack the columns as soon as the width is smaller than 768px.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[class*="col-"] {
  /*Mobile first: set the width to 100% by default*/
  width: 100%;
}

@media only screen and (min-width: 768px) {
 /* For everything bigger than 768px */
 .col-1{
   width: 8.33%;
 }

 .col-2 {...}

 .col-3{
  width: 25%;
 }
}

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

&lt;/div&gt;



&lt;p&gt;First of all, I set the initial width of all columns to 100 percent. Because that is how it should look like on a smaller screen, it also improves the performance of the page display for them. By setting the width to 100 percent, the columns automatically stack vertically, so yep, there is not much more to do.&lt;/p&gt;

&lt;p&gt;The second important thing is the &lt;code&gt;@media&lt;/code&gt; rule I added: As soon as the width exceeds 768px, the overall widths from the last example are used.&lt;/p&gt;

&lt;p&gt;Now having covered the basics, let’s see, how this is done with React and styled-components!&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating the basic grid view with styled-components
&lt;/h4&gt;

&lt;p&gt;As a starter, we will build the exact same grid, like I covered in the last section:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It will have twelve possible columns&lt;/li&gt;
&lt;li&gt;A column can span across the width of 1–12 columns.&lt;/li&gt;
&lt;li&gt;If the total width is smaller than 768px, all of the columns will stack vertically.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the following screenshot you will see, that I created a component called &lt;em&gt;Row&lt;/em&gt; which is needed to clear the float after the columns. Then I added a &lt;em&gt;Column&lt;/em&gt; component, which has by default &lt;code&gt;float: left&lt;/code&gt; set, and a width of 100% (mobile first, you remember?).&lt;/p&gt;

&lt;p&gt;But have a look on your own:&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AS_jt8B668fUQBkW4vVOVUA.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AS_jt8B668fUQBkW4vVOVUA.png"&gt;&lt;/a&gt;Our grid built with styled-components&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Column&lt;/em&gt; component takes a prop &lt;code&gt;span&lt;/code&gt; which represents the amount of columns to span across. If this prop is set, we calculate the desired width in percent. If no column span is set, we default to 8.33 percent, which is the default width for a column span of 1.&lt;/p&gt;

&lt;h4&gt;
  
  
  Extending the grid: Different breakpoints!
&lt;/h4&gt;

&lt;p&gt;Taking Bootstrap as an example, we want to have the possibility to define different column spans for different viewports. This would give us the possibility to use different column spans on different screen sizes.&lt;/p&gt;

&lt;p&gt;To achieve this, &lt;em&gt;Column&lt;/em&gt; will no longer take a &lt;code&gt;span&lt;/code&gt; prop. Instead there will be one for each breakpoint:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;xs&lt;/em&gt;&lt;/strong&gt; _ _for screen sizes up to 768px&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;sm&lt;/em&gt;&lt;/strong&gt;  for sizes up to 992px&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;md&lt;/em&gt;&lt;/strong&gt; _ _for sizes up to 1200&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;lg&lt;/em&gt;&lt;/strong&gt; _ _for everything bigger than that&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AoQ57567nklR574tNd4o2tA.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AoQ57567nklR574tNd4o2tA.png"&gt;&lt;/a&gt;Our new Column component&lt;/p&gt;

&lt;p&gt;So what did we do here? Since we do not have a fallback width for column spans bigger than  &lt;strong&gt;&lt;em&gt;xs&lt;/em&gt;&lt;/strong&gt; _ _we no longer calculate the value of the width, but the whole text instead.&lt;/p&gt;

&lt;p&gt;For example: If you set a column span of 6 for &lt;em&gt;md&lt;/em&gt;, you will get &lt;code&gt;width: 50%;&lt;/code&gt;on medium devices. if you do not set a column span for &lt;em&gt;md&lt;/em&gt; and the screen width exceeds 992px, we will just not set any width. This way the column gets always the width for the next smaller set column span, or 100% if nothing is set.&lt;/p&gt;

&lt;p&gt;Now let’s see how it works:&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AJbcS7nAE-ynTqUGlk9y78Q.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AJbcS7nAE-ynTqUGlk9y78Q.png"&gt;&lt;/a&gt;The above example should render a grid with two columns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Extra small screens&lt;/strong&gt;  (&amp;lt;768px)&lt;br&gt;&lt;br&gt;
On extra small screens, the first column takes the whole width, while column number 2 is shifted to the next row.&lt;/p&gt;

&lt;p&gt; &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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2Ar6IfYjLc1K8S7hG1NpTfnQ.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2Ar6IfYjLc1K8S7hG1NpTfnQ.png"&gt;&lt;/a&gt;Our example rendered on an extra smallÂ screen&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Small screens&lt;/strong&gt;  (&amp;gt;768px)&lt;br&gt;&lt;br&gt;
The first column should span across 6 columns and should therefore get a width of 50 percent. In this case the second column does not have a column span for small screens set, so the width of the next smallest break point is assigned: 50 percent.&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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A5xjJWyQNYtgeGxWU9kl1Fg.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2A5xjJWyQNYtgeGxWU9kl1Fg.png"&gt;&lt;/a&gt;Small screenâ€Š–â€ŠIf no column span for a certain screen size is set, the next smaller one is assigned.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Medium screens&lt;/strong&gt;  (&amp;gt;992px)&lt;br&gt;&lt;br&gt;
This should be easy: The first column spans across 8 columns (66.66 percent) and the second one spans across 4 columns (33.33 percent)&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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AGfDfNujlo0p94k9N58gy5A.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AGfDfNujlo0p94k9N58gy5A.png"&gt;&lt;/a&gt;Medium screen&lt;/p&gt;

&lt;p&gt;Since we did not set a column span for large screens, both columns look the same like they look on medium screens.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;What we built here was an example of a responsive grid with styled-components with basic functionality. You could possibly extend the components to support more styling like margins, paddings or borders, just to name a few.&lt;/p&gt;

&lt;p&gt;I think the most advanced responsive grid view I could find in my research for this article, is &lt;a href="http://jxnblk.com/grid-styled/" rel="noopener noreferrer"&gt;grid-styled&lt;/a&gt; from &lt;a href="https://medium.com/@jxnblk" rel="noopener noreferrer"&gt;Brent Jackson&lt;/a&gt;. It even allows you to set different font sizes for four different breakpoints and much more. For this article however, I tried to cover the basics of responsive grids and how to build one. And what I really liked was the fact how easy it was to consider the different breakpoints since we can use real CSS. I know, there are several alternatives out there and I already had a look on some of them. But in terms of using media queries most blog posts recommend using libraries like &lt;a href="https://github.com/contra/react-responsive" rel="noopener noreferrer"&gt;react-responsive&lt;/a&gt; or &lt;a href="https://github.com/akiran/react-responsive-mixin" rel="noopener noreferrer"&gt;react-responsive-mixin&lt;/a&gt; which may not be bad, but just did not feel right to me.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The post &lt;a href="http://www.andreasreiterer.at/web-development/responsive-ui-with-styled-components/" rel="noopener noreferrer"&gt;How to create responsive UI with styled-components&lt;/a&gt; appeared first on &lt;a href="http://www.andreasreiterer.at" rel="noopener noreferrer"&gt;my website&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Call To Action
&lt;/h1&gt;

&lt;p&gt;Do you want to learn more about styled-components, responsive design or other web development tips and tricks? Good news: Subscribe to my &lt;a href="//www.andreasreiterer.at/weekly-webdev-newsletter/"&gt;weekly newsletter&lt;/a&gt; to learn something new once in a week :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
