<?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: Timo Prüße</title>
    <description>The latest articles on DEV Community by Timo Prüße (@timopruesse).</description>
    <link>https://dev.to/timopruesse</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%2F103721%2F01025ffb-cf1a-494a-b61f-c8730c703b8c.jpeg</url>
      <title>DEV Community: Timo Prüße</title>
      <link>https://dev.to/timopruesse</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/timopruesse"/>
    <language>en</language>
    <item>
      <title>React: Stacking Context</title>
      <dc:creator>Timo Prüße</dc:creator>
      <pubDate>Wed, 08 Apr 2020 08:46:41 +0000</pubDate>
      <link>https://dev.to/timopruesse/react-stacking-context-f34</link>
      <guid>https://dev.to/timopruesse/react-stacking-context-f34</guid>
      <description>&lt;p&gt;Just the other day, I encountered an unusual challenge when working with the React Context API. I’m eager to share what I learned and how I worked around the restrictions that it has.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ptZbQ-Hp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/12032/0%2Ai4sdL1zRaKIFLYpb" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ptZbQ-Hp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/12032/0%2Ai4sdL1zRaKIFLYpb" alt="Photo by [Claudia Wolff](https://unsplash.com/@kaimantha?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Scenario
&lt;/h1&gt;

&lt;p&gt;Take a look at the following simplified code snippet:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The context’s value is dependant on the &lt;code&gt;name&lt;/code&gt; prop. That means the values for &lt;code&gt;outer_context&lt;/code&gt; and &lt;code&gt;inner_context&lt;/code&gt; differ.&lt;/p&gt;




&lt;h1&gt;
  
  
  Problem
&lt;/h1&gt;

&lt;p&gt;The most inner component &lt;code&gt;ComponentThatUsesContextFromBothProviders&lt;/code&gt; needs the values of both context providers it is wrapped in. However, by default, it is only possible for us to get the context of the closest provider, which would be &lt;code&gt;inner_context&lt;/code&gt; in this case.&lt;/p&gt;

&lt;p&gt;First things that might come to your mind now are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pass the value of the outer context as a prop to the inner component&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We don’t want to do that because we would end up with prop drilling again in more complex scenarios where we would need to pass it down the whole tree. The React team introduced the Context API to prevent precisely that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use some kind of state management&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We don’t want to overcomplicate things with state management since our use case is straightforward, and we don’t want to pollute our global state. Only the components wrapped inside of the context providers need to know their values.&lt;/p&gt;




&lt;h1&gt;
  
  
  Solution
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Finding a leverage point&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The value of our context is dependant on the &lt;code&gt;name&lt;/code&gt; prop we set for the provider. That means the context that is computed for each name should be different. We know that we might need all of the individual key/value pairs when a component is wrapped inside of multiple providers.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;How does that help us?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Oversimplified our structure needs to look like this to provide that functionality:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;outer_context -&amp;gt; Component A -&amp;gt; inner_context -&amp;gt; Component B

Component A Context = { outer_context: 'outer_value' }

Component B Context = {
  outer_context: 'outer_value',
  inner_context: 'inner_value'
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Maybe you did already see where I’m heading with this. As the title of this article suggests, it makes sense to stack our context in that case. If we now introduce a new component &lt;code&gt;C&lt;/code&gt;, it needs to have all the context of the component &lt;code&gt;B&lt;/code&gt; plus the new context that is provided.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;How can we achieve a stacking context?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It’s just a few lines of code as seen in the following snippet:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;There’s no magic involved. Instead of saving the context’s value directly, we create an object that is indexed by the &lt;code&gt;name&lt;/code&gt; prop in this example. Every context provider now only needs to use the existing context and ‘push’ it’s own value to the stack. Now we can get the values like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can also create a custom hook to make it more convenient to get the value and handle the case of a non-existent key.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Sometimes it makes sense to ‘bend’ the React world a bit and break out of the boundaries set by it. Do what fits best to the scope of your project. Just remember that everything is just JavaScript in the end. ;)&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Benefits of using Dart (and Flutter)</title>
      <dc:creator>Timo Prüße</dc:creator>
      <pubDate>Mon, 17 Feb 2020 18:54:55 +0000</pubDate>
      <link>https://dev.to/timopruesse/benefits-of-using-dart-and-flutter-3n1h</link>
      <guid>https://dev.to/timopruesse/benefits-of-using-dart-and-flutter-3n1h</guid>
      <description>&lt;p&gt;Programmers don’t like to deal with stuff that is not directly related to their actual problems that they’re trying to solve. So the more comfortable it is to use a programming language, the easier it is to focus on the substantive difficulties.&lt;/p&gt;

&lt;p&gt;The following list shows different ways that support you in writing maintainable code quickly when using Dart.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ccBoU-ct--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/878/1%2AEGd22O-yJPVH3cmTtfmWMQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ccBoU-ct--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/878/1%2AEGd22O-yJPVH3cmTtfmWMQ.png" alt="© commons.wikimedia.org"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Extensions
&lt;/h1&gt;

&lt;p&gt;When working with third-party libraries, you often wish that a certain functionality exists. In most cases, it’s tightly coupled to the current project and wouldn’t qualify as a pull-request to the library’s repository. In Dart, it is possible to extend third-party libraries.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Furthermore, it’s even possible to extend native types like &lt;strong&gt;String&lt;/strong&gt; or &lt;strong&gt;List&lt;/strong&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can also utilize generics. Check out the code snippet below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;By extending the capabilities of the language, it is also more comfortable for your colleagues to find the new functionalities you implemented. They don’t have to check some directory you probably named ‘util’ or in a similar fashion. When working with the code, they’ll explore all the new bits naturally through their IDE (auto-complete / auto-suggest).&lt;/p&gt;




&lt;h1&gt;
  
  
  Named Parameters
&lt;/h1&gt;

&lt;p&gt;Don’t we all always have to look up parameters for specific functions or methods? When using Dart, you can define named parameters. These are a convenient way to make your API more natural to use. However, it can also encourage lousy design decisions by overloading functions with a ton of parameters. Just beware not to go overboard with it. Just use it as a tool to make your code more readable and easier to use!&lt;/p&gt;

&lt;p&gt;If you want to declare a parameter as necessary for the execution of the function, you can add an annotation (see &lt;em&gt;@required&lt;/em&gt; in the code example below).&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;h1&gt;
  
  
  Named Constructors
&lt;/h1&gt;

&lt;p&gt;Named constructors can come in handy when an object typically gets constructed with the same parameters in multiple places. Or maybe you want to make it more clear what exactly you’re initializing.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;h1&gt;
  
  
  LINQ-like Collection Methods
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;‘Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language.’&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;— Microsoft C# documentation&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Take a look at the following example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It feels natural, and you instantly know what the code achieves here. There are a lot more convenient methods that you’ll automatically find when writing your code. Yet, you might notice the ‘.toList()’ method. That is required because Iterables are lazy in Dart and we have to convert it to a list.&lt;/p&gt;




&lt;h1&gt;
  
  
  Operator Overloading
&lt;/h1&gt;

&lt;p&gt;Merging multiple objects is a common task in web programming. Make your life easier and overload the operators! You decide what happens when someone tries to interact with your objects. This way, your colleagues can use your objects within the natural constraints of the language. They don’t need to worry about deep-merging or other issues.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;h1&gt;
  
  
  Streams / StreamBuilder
&lt;/h1&gt;

&lt;p&gt;Handling with asynchronicity is always a source of frustration. Instead of only knowing when a promise got resolved or rejected, we also get the current progress of the function with streams. Writing good user interfaces is extremely straightforward because you can always communicate the current state to the user. With streams, you get constant feedback for free.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The StreamBuilder is a Flutter widget that makes dealing with streams smooth. If you’re only interested in the outcome of an asynchronous function, you can stick to Futures, which work precisely like Promises in JavaScript.&lt;/p&gt;




&lt;h1&gt;
  
  
  Optimizations included
&lt;/h1&gt;

&lt;p&gt;In web apps, you usually reach for Webpack or similar tools to bundle your codebase for production. The setup can become very cumbersome and hard sometimes. Dart includes optimizations like tree-shaking out-of-the-box. You don’t need to configure anything as opposed to all the other tools that claim that they are ‘zero-config’.&lt;/p&gt;




&lt;h1&gt;
  
  
  Compile it to ANYTHING
&lt;/h1&gt;

&lt;p&gt;With the help of Flutter, it’s possible to compile your codebase to work with various platforms (Android, iOS, Web, macOS, Linux, Windows). However, the native implementations (macOS, Linux, Windows) are still in alpha or technical preview status.&lt;/p&gt;

&lt;p&gt;The Flutter team has implemented all of the native control elements from scratch. That means that you can write an app that looks the same on iOS and Android. So you can use Material Design widgets for iOS and vice-versa. Of course, it’s also possible to have distinct widgets for each platform. It is an excellent example of WYSIWYG because of the custom implementation of the renderer (Skia).&lt;/p&gt;




&lt;h1&gt;
  
  
  Insanely fast Hot-Reload
&lt;/h1&gt;

&lt;p&gt;You’ll love Flutter’s hot-reload feature right away. This mechanic exists in other frameworks like React, too. Nevertheless, they currently can’t compete with the one in Flutter. E.g., if you change a color, the change is displayed immediately. There’s not even a sign of a hot-reload taking place. It is a feature that you have to see with your own eyes.&lt;/p&gt;




&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;With Dart, it’s possible to keep a fast pace while developing your app and still produce maintainable code. All the listed benefits automatically decrease the barrier for your colleagues to understand your intentions. If you’re currently using TypeScript or a comparable language, it’s worth a try to implement Dart in one of your new projects. You’ll feel an improvement even if only a subset of the advantages mentioned in this article concerns you.&lt;/p&gt;

</description>
      <category>dart</category>
      <category>flutter</category>
    </item>
  </channel>
</rss>
