<?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: Fredrik Bergqvist</title>
    <description>The latest articles on DEV Community by Fredrik Bergqvist (@fredrikbergqvist).</description>
    <link>https://dev.to/fredrikbergqvist</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%2F168501%2Fdfe7fe05-8fab-42eb-8f98-3417f45e0deb.png</url>
      <title>DEV Community: Fredrik Bergqvist</title>
      <link>https://dev.to/fredrikbergqvist</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fredrikbergqvist"/>
    <language>en</language>
    <item>
      <title>API Considerations for Beginners</title>
      <dc:creator>Fredrik Bergqvist</dc:creator>
      <pubDate>Fri, 05 Sep 2025 00:00:00 +0000</pubDate>
      <link>https://dev.to/fredrikbergqvist/api-considerations-for-beginners-2j0j</link>
      <guid>https://dev.to/fredrikbergqvist/api-considerations-for-beginners-2j0j</guid>
      <description>&lt;p&gt;When you’re starting out designing an API, there are a few things you’ll want to think about early on. Some of these will save you time down the road, others will help you avoid common mistakes that lead to bugs, confusion, or security holes. Here are the main points I keep in mind.&lt;/p&gt;




&lt;h3&gt;
  
  
  Versioning
&lt;/h3&gt;

&lt;p&gt;Think about how you want to handle versioning from the start. Even if you don’t expect to support multiple versions right away, it’s often a good idea to include a version prefix in your paths, such as &lt;code&gt;/v1/&lt;/code&gt;. That way, if you ever need to introduce breaking changes, you won’t have to redesign your whole URL structure.&lt;/p&gt;

&lt;h3&gt;
  
  
  Documentation
&lt;/h3&gt;

&lt;p&gt;Take the time to document your API. Swagger (OpenAPI) is the most common way and comes with several benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It’s easy to import into tools like Postman.&lt;/li&gt;
&lt;li&gt;You can generate frontend methods, complete with TypeScript types and helpers.

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://kubb.dev/" rel="noopener noreferrer"&gt;Kubb.dev&lt;/a&gt; has many plugins for automatic generation.&lt;/li&gt;
&lt;li&gt;I’ve also used &lt;a href="https://github.com/acacode/swagger-typescript-api" rel="noopener noreferrer"&gt;swagger-typescript-api&lt;/a&gt; to generate types and &lt;a href="https://tanstack.com/query/latest" rel="noopener noreferrer"&gt;TanStack Query&lt;/a&gt; hooks.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Swagger includes an SDK generator: &lt;a href="https://swagger.io/tools/swagger-codegen/" rel="noopener noreferrer"&gt;swagger.io/tools/swagger-codegen&lt;/a&gt;.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;On top of that, it’s worth keeping a simple README file with instructions on how to set up, build, and run your API server. It’s easy to forget these details, even for your own projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Caching
&lt;/h3&gt;

&lt;p&gt;Caching can give you a big performance boost, but how you do it depends on the size and reliability of your system.&lt;/p&gt;

&lt;p&gt;For small apps where it doesn’t matter if data has to be re-fetched on a restart, &lt;strong&gt;in-memory caching&lt;/strong&gt; is usually enough. For bigger systems, or anything running multiple instances, you’ll want something more robust like Redis or Memcached so your caches stay in sync.&lt;/p&gt;




&lt;h2&gt;
  
  
  Use correct HTTP methods and responses
&lt;/h2&gt;

&lt;p&gt;Using the right HTTP methods and returning the right status codes makes your API predictable and easier to use. Here’s the basic breakdown:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GET&lt;/strong&gt; to fetch an entity → &lt;code&gt;200 OK&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;POST&lt;/strong&gt; to create an entity → &lt;code&gt;201 Created&lt;/code&gt; with a &lt;code&gt;Location&lt;/code&gt; header pointing to the new resource’s URL&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PUT&lt;/strong&gt; to replace an entity → &lt;code&gt;200 OK&lt;/code&gt; or &lt;code&gt;204 No Content&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PATCH&lt;/strong&gt; to update specific attributes on an entity → &lt;code&gt;200 OK&lt;/code&gt; or &lt;code&gt;204 No Content&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DELETE&lt;/strong&gt; to remove an entity → &lt;code&gt;200 OK&lt;/code&gt; or &lt;code&gt;204 No Content&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should also decide whether you want to return the full model after an update, or just &lt;code&gt;204 No Content&lt;/code&gt;. Either way is fine — the important thing is to be consistent.&lt;/p&gt;

&lt;p&gt;Be careful with your error handling. Don’t return a generic &lt;code&gt;500&lt;/code&gt; if the problem is really that the resource wasn’t found — that should be a &lt;code&gt;404&lt;/code&gt;. Similarly, use &lt;code&gt;409 Conflict&lt;/code&gt; when two updates clash, and return &lt;code&gt;400 Bad Request&lt;/code&gt; for validation errors.&lt;/p&gt;

&lt;p&gt;Finally, pick a consistent error response format and stick with it. A simple structure like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "code": "invalid_input",
  "message": "Email is not valid",
  "details": {}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;goes a long way toward making your API easier to consume.&lt;/p&gt;




&lt;h2&gt;
  
  
  Security
&lt;/h2&gt;

&lt;p&gt;Always use &lt;strong&gt;HTTPS&lt;/strong&gt;. This is the baseline for protecting data in transit.&lt;/p&gt;

&lt;p&gt;If your API will be called from browsers, set up &lt;strong&gt;CORS&lt;/strong&gt; to restrict which domains are allowed. Keep in mind that CORS is only a browser-level safeguard — other clients can still make requests — so you should always enforce security on the server too.&lt;/p&gt;

&lt;p&gt;Authentication is another must. At the simplest level, you can require an &lt;strong&gt;API key&lt;/strong&gt;. That’s easy to implement, but not very secure and only suitable for low-value data. For anything more important, go with &lt;strong&gt;JWTs&lt;/strong&gt; or &lt;strong&gt;OAuth&lt;/strong&gt; , which give you more control over permissions, expiry, and token management.&lt;/p&gt;

&lt;p&gt;When it comes to error handling, don’t leak sensitive details. Stack traces and debug output don’t belong in responses. Always sanitize and validate inputs on the server, no matter how much you “trust” the client.&lt;/p&gt;

&lt;p&gt;And finally, consider adding &lt;strong&gt;rate limiting or throttling&lt;/strong&gt;. This prevents brute-force attacks and stops clients from overwhelming your service.&lt;/p&gt;




&lt;h2&gt;
  
  
  Monitoring and observability
&lt;/h2&gt;

&lt;p&gt;If your API is critical, you’ll want visibility into how it’s behaving. Logging is the first step — make sure you record enough to debug issues later. But don’t stop there: set up alerts for spikes in errors or traffic, and keep an eye on key metrics like latency, error rates, and request volume. This will help you catch issues before users report them.&lt;/p&gt;




&lt;h2&gt;
  
  
  Listings
&lt;/h2&gt;

&lt;p&gt;When your API returns lists of entities, you should almost always implement &lt;strong&gt;pagination&lt;/strong&gt;. Dumping thousands of records in one response is wasteful and slow.&lt;/p&gt;

&lt;p&gt;Sometimes, you don’t even need the full model. For example, if you’re filling a dropdown menu, you probably only need an &lt;code&gt;id&lt;/code&gt; and a &lt;code&gt;name&lt;/code&gt;. In these cases, consider adding lightweight listing endpoints that only return the fields needed. This makes responses faster and easier to work with.&lt;/p&gt;




&lt;h2&gt;
  
  
  Updating several items at once
&lt;/h2&gt;

&lt;p&gt;Sometimes users want to perform bulk actions, like selecting multiple items and archiving them. There are a couple of ways to handle this.&lt;/p&gt;

&lt;p&gt;For small updates, you can just use &lt;strong&gt;PATCH&lt;/strong&gt; and only update the relevant property. The server should always apply the change against the latest version of the data.&lt;/p&gt;

&lt;p&gt;For larger operations, a dedicated &lt;strong&gt;batch endpoint&lt;/strong&gt; is often better. That way you can update multiple records in a single call, keeping everything consistent and reducing round trips.&lt;/p&gt;

&lt;p&gt;Whichever approach you use, make sure the client has fresh data. A common pattern is to refetch whenever the browser tab regains focus, so users don’t accidentally update stale state.&lt;/p&gt;




&lt;h2&gt;
  
  
  Avoiding overwriting updates (concurrency)
&lt;/h2&gt;

&lt;p&gt;A common issue in APIs is two people editing the same resource at the same time. Without safeguards, whoever saves last overwrites the other person’s changes.&lt;/p&gt;

&lt;p&gt;One simple approach is to include a &lt;code&gt;lastUpdated&lt;/code&gt; timestamp (or a version number) in each model. The client includes this value in its update request, and the server checks it against the database. If it doesn’t match, you know the resource has changed since the client last fetched it, and you can reject the update.&lt;/p&gt;

&lt;p&gt;Another approach is to use &lt;strong&gt;ETags&lt;/strong&gt; with the &lt;code&gt;If-Match&lt;/code&gt; header. This is built into HTTP and works much the same way: the ETag identifies the version of the resource, and if it doesn’t match on update, the server returns &lt;code&gt;412 Precondition Failed&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For business-critical actions like payments, you should also consider supporting an &lt;strong&gt;Idempotency-Key&lt;/strong&gt; header. This ensures retries don’t cause duplicate side effects if the client resends the same request after a timeout or network issue.&lt;/p&gt;




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

&lt;p&gt;Even a little bit of testing can go a long way. Start with &lt;strong&gt;integration tests&lt;/strong&gt; that just check status codes. This is enough to make sure all your endpoints respond correctly and helps you catch routing or method errors early.&lt;/p&gt;

&lt;p&gt;For more important endpoints, add &lt;strong&gt;contract tests&lt;/strong&gt; that validate not just the status but the shape and content of the response. That way, if someone changes a field name or removes a property, you catch it before clients break. If you have multiple services talking to each other, consumer-driven contract testing tools like Pact can be very helpful.&lt;/p&gt;

&lt;p&gt;Automate tests where it makes sense. Even a small set of tests running in your CI pipeline can prevent regressions. Tools like Postman are great for exploration and manual checks, but don’t stop there — add &lt;strong&gt;unit tests&lt;/strong&gt; and automated &lt;strong&gt;integration tests&lt;/strong&gt; to round out your strategy.&lt;/p&gt;




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

&lt;p&gt;Designing APIs is partly about following conventions and partly about thinking ahead. By choosing consistent HTTP methods, handling concurrency, validating input, and writing at least some tests, you’ll avoid many of the pitfalls that trip up beginners.&lt;/p&gt;

&lt;p&gt;You don’t need to over-engineer everything on day one, but if you keep these practices in mind, you’ll end up with an API that’s easier to maintain, safer to use, and friendlier to clients.&lt;/p&gt;

</description>
      <category>api</category>
      <category>web</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The curious case of the paragraph with the bad CLS</title>
      <dc:creator>Fredrik Bergqvist</dc:creator>
      <pubDate>Tue, 03 Dec 2024 12:02:00 +0000</pubDate>
      <link>https://dev.to/fredrikbergqvist/the-curious-case-of-the-paragraph-with-the-bad-cls-2ge4</link>
      <guid>https://dev.to/fredrikbergqvist/the-curious-case-of-the-paragraph-with-the-bad-cls-2ge4</guid>
      <description>&lt;p&gt;I recently migrated my personal website from Next.js to &lt;a href="https://www.11ty.dev/" rel="noopener noreferrer"&gt;11ty&lt;/a&gt;, it was a great experience, which I will write more about in a later blog post.&lt;/p&gt;

&lt;p&gt;There was just one issue. The PageSpeed performance score actually went down. 😨&lt;/p&gt;

&lt;blockquote&gt;
&lt;a href="https://pagespeed.web.dev/analysis/https-www-bergqvist-it-bad-cls/qyexo75vas?form_factor=desktop" rel="noopener noreferrer"&gt;Link to PageSpeed.web.dev&lt;/a&gt;
&lt;/blockquote&gt;

&lt;p&gt;Mobile performance was looking great, a 4 x 100 score, but desktop performance was hurting. The problem also only occurred on the blog post list page, not on the individual blog post pages, and only when the page width was greater than 960 pixels, when the max with of the content was set to a fixed value and margin auto.&lt;/p&gt;

&lt;p&gt;After some digging, the culprit seemed to be a paragraph at the end of the blog post list, containing a link to older posts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcn4oipate3jik1cesqu8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcn4oipate3jik1cesqu8.png" alt="List of elements affected by the layout shift taken from devtools" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a version of the start page with the paragraph: &lt;a href="https://www.bergqvist.it/bad-cls/" rel="noopener noreferrer"&gt;Click for some bad CLS&lt;/a&gt; and the version without: &lt;a href="https://www.bergqvist.it/" rel="noopener noreferrer"&gt;Click for no bad CLS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When fiddling with it, I found that any block element (or even inline-block), would cause the issue, but using a span would not trigger it.&lt;/p&gt;

&lt;p&gt;Now, I do understand that this, in reality, is a non issue, and really only proves the point that blindly following performance scores is not always the best way to go.&lt;/p&gt;

&lt;p&gt;But I am still curious about why this is happening. If you have any insights, please let me know.&lt;/p&gt;

&lt;p&gt;To be continued... (I hope)&lt;/p&gt;

</description>
      <category>performance</category>
      <category>webperf</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Minified React error in production with Vite.js</title>
      <dc:creator>Fredrik Bergqvist</dc:creator>
      <pubDate>Thu, 03 Nov 2022 16:02:24 +0000</pubDate>
      <link>https://dev.to/fredrikbergqvist/minified-react-error-in-production-with-vitejs-210p</link>
      <guid>https://dev.to/fredrikbergqvist/minified-react-error-in-production-with-vitejs-210p</guid>
      <description>&lt;p&gt;At work I inherited a really old website running on React.&lt;/p&gt;

&lt;p&gt;The code was good but by old i mean really old; last commit was 3 years ago and most of the code was 5-6 years old…&lt;/p&gt;

&lt;p&gt;So I converted it to TypeScript and ripped out the old babel/webpack build system and installed Vite to handle all the builds.&lt;/p&gt;

&lt;p&gt;Everything went pretty smoothly and I got the site up and running with no errors in development mode.&lt;/p&gt;

&lt;p&gt;When going to production is when things went sideways. Some pages crashed with the following error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error: Minified React error #130; visit https://reactjs.org/docs/error-decoder.html?invariant=130&amp;amp;args[]=object&amp;amp;args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
    at vc (vendor.93c27df6.js:23)
    at d (vendor.93c27df6.js:23)
    at vendor.93c27df6.js:23
    at vendor.93c27df6.js:23
    at Ts (vendor.93c27df6.js:23)
    at ul (vendor.93c27df6.js:23)
    at ic (vendor.93c27df6.js:23)
    at tc (vendor.93c27df6.js:23)
    at ql (vendor.93c27df6.js:23)
    at zl (vendor.93c27df6.js:23)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some quick googling led me to a &lt;a href="https://github.com/vitejs/vite/issues/2139" rel="noopener noreferrer"&gt;GitHub issue&lt;/a&gt; showing me that I was not the only one experiencing this bug.&lt;/p&gt;

&lt;h2&gt;
  
  
  Package problem number one
&lt;/h2&gt;

&lt;p&gt;It seems that esbuild is checking for __esModule at runtime and rollup (which is used internally in Vite) is not.&lt;/p&gt;

&lt;p&gt;So it will happen to any library that has an entry point with: &lt;code&gt;module.exports = require("./module");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since a file exported this way does not include the ‘magic string’ __esModule as checked by generate-exports.js it’s not converted correctly.&lt;/p&gt;

&lt;p&gt;In my case I had two librarys acting up; react-datetime which was imported locally like this: &lt;code&gt;import DateTime from "react-datetime";&lt;/code&gt; which worked fine in Vite dev but not production.&lt;/p&gt;

&lt;p&gt;Props to &lt;a href="https://github.com/vitejs/vite/issues/2139#issuecomment-806416318" rel="noopener noreferrer"&gt;ntkoopman for the explanation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;User &lt;a href="https://github.com/vitejs/vite/issues/2139#issuecomment-802981228" rel="noopener noreferrer"&gt;Haikyuu suggested&lt;/a&gt; using the following solution which is what I used in the end.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import DT from "react-datetime";
// @ts-ignore
const DateTime = DT.default ? DT.default : DT;
Package problem number two
The second package with this issue was react-dropzone which in turn used the real culprit; attr-accept
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the project used an old version (4.x.x, latest version is at the time of writing 14.x.x) and I got other issues upgrading to the latest version I could rule out a PR from me fixing it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution number two
&lt;/h2&gt;

&lt;p&gt;Instead I used patch-package and postinstall to create a patch-file with the fix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//from
import accepts from 'attr-accept';

//to
import A from "attr-accept";
const accepts = A.default ? A.default : A;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I how this can help if someone is stuck, and I hope it will be fixed soon because it is kind of a blocker for using Vite.js in production&lt;/p&gt;

</description>
      <category>vite</category>
      <category>react</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>CSS modules in next.js</title>
      <dc:creator>Fredrik Bergqvist</dc:creator>
      <pubDate>Sat, 13 Nov 2021 16:46:34 +0000</pubDate>
      <link>https://dev.to/fredrikbergqvist/css-modules-in-nextjs-34nm</link>
      <guid>https://dev.to/fredrikbergqvist/css-modules-in-nextjs-34nm</guid>
      <description>&lt;p&gt;On bergqvist.it I used &lt;a href="https://github.com/vercel/styled-jsx" rel="noopener noreferrer"&gt;styled jsx&lt;/a&gt; for styling my components. I preferred that to other css-in-js frameworks (like JSS) because of it actually using CSS syntax instead of JavaScript objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="c1"&gt;// styled jsx example with good old CSS&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt; &lt;span class="nx"&gt;jsx&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`
      .label { color: red; font-style: italic; }
      .article { padding: 0; }
    `&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/style&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
    &lt;span class="c1"&gt;//JSS example with CSS as a JS object&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useStyles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createUseStyles&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
        &lt;span class="na"&gt;fontStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;italic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="na"&gt;article&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I like Styled jsx but it has had an issue with &lt;a href="https://webkit.org/blog/66/the-fouc-problem/#:~:text=FOUC%20stands%20for%20Flash%20of,having%20any%20style%20information%20yet.&amp;amp;text=When%20a%20browser%20loads%20a,file%20from%20the%20Web%20site." rel="noopener noreferrer"&gt;FOUC&lt;/a&gt; in the &lt;a href="https://github.com/vercel/styled-jsx/issues/681" rel="noopener noreferrer"&gt;last few versions of Next.js&lt;/a&gt; though, and with Next 12 I decided to try something new and migrate to CSS Modules instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is CSS Modules?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;CSS Module&lt;/strong&gt; is a CSS file in which all class names and animation names are scoped locally by default. &lt;/p&gt;

&lt;p&gt;So the benefit are the same as css-in-js-frameworks but pure css (or in my case scss) files are used instead of keeping the styling in the javascript files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/css-modules/css-modules" rel="noopener noreferrer"&gt;Read more about CSS Modules here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why CSS Modules?
&lt;/h2&gt;

&lt;p&gt;I may be old school, but I actually prefer to keep the CSS and JavaScript separated for each other. I can see the benefits of keeping them tightly coupled and can agree that simple, self-contained components probably benefit from this approach, but it gets messy when the component has many styles with media queries.&lt;/p&gt;

&lt;p&gt;I also want to use &lt;a href="https://sass-lang.com/" rel="noopener noreferrer"&gt;SASS&lt;/a&gt;, which is fully supported with CSS Modules in Next.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Migrating from styled jsx to CSS Modules
&lt;/h2&gt;

&lt;p&gt;Since Styled jsx use regular CSS it’s actually just a matter of creating the &lt;code&gt;&amp;lt;component&amp;gt;.module.scss&lt;/code&gt;-file, importing it into the component and changing the classes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="c1"&gt;//styled jsx&lt;/span&gt;
    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;article&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;label&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt; &lt;span class="nx"&gt;jsx&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`
          .label { color: red; font-style: italic; }
          .article { padding: 0; }
        `&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/style&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;)
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//CSS Modules&lt;/span&gt;
    &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./MyComponent.module.scss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;article&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;)
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using multiple modules in one component
&lt;/h2&gt;

&lt;p&gt;For reusability you might want to use a css module in more than one component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./MyComponent.module.scss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;secondaryStyles&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./secondary.module.scss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;article&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;secondaryStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;)
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are using TypeScript this approach probably cause an error: &lt;code&gt;TS2339: Property 'label' does not exist on type 'typeof import("*.module.scss")'.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The error can be can be mitigated by adding a &lt;code&gt;typings.d.ts&lt;/code&gt; -file to the root of your project with the following content&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="c1"&gt;// typings.d.ts&lt;/span&gt;
    &lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="kr"&gt;module&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;*.module.scss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IClassNames&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;classNames&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IClassNames&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;classNames&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Composition
&lt;/h2&gt;

&lt;p&gt;Instead of importing several different modules it’s possible to compose new classes from existing classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="c1"&gt;// secondary.module.scss&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
      &lt;span class="nx"&gt;font&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;italic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// MyComponent.module.scss&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;article&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;composes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./secondary.module.scss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// MyComponent.tsx&lt;/span&gt;
    &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./MyComponent.module.scss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;article&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;)
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Global styles
&lt;/h2&gt;

&lt;p&gt;As I already had a global css-file that I imported into my &lt;code&gt;_app.tsx&lt;/code&gt;, I really did not have to do anything to get my global classes working.&lt;br&gt;&lt;br&gt;
If you want to add a global class in a component file you can add it by using &lt;code&gt;:global()&lt;/code&gt; on the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nf"&gt;global&lt;/span&gt;&lt;span class="p"&gt;(.&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
      &lt;span class="nx"&gt;font&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;italic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Parting words
&lt;/h2&gt;

&lt;p&gt;I’m quite happy with CSS Modules, the site no longer gets FOUC and it looks great with JavaScript disabled as well!&lt;/p&gt;

&lt;p&gt;Hope this could be of any help to someone looking into CSS Modules.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>css</category>
      <category>react</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Tips and tricks for testing with Jest</title>
      <dc:creator>Fredrik Bergqvist</dc:creator>
      <pubDate>Mon, 29 Jun 2020 12:36:49 +0000</pubDate>
      <link>https://dev.to/fredrikbergqvist/tips-and-tricks-for-testing-with-jest-am3</link>
      <guid>https://dev.to/fredrikbergqvist/tips-and-tricks-for-testing-with-jest-am3</guid>
      <description>&lt;p&gt;Writing tests can be daunting when starting out, it’s hard to know exactly what to test and then learning the API for your test tool.&lt;/p&gt;

&lt;p&gt;I wanted to share some small tips that can be useful when starting out.&lt;/p&gt;

&lt;h2&gt;
  
  
  expect.objectContaining()
&lt;/h2&gt;

&lt;p&gt;In some cases you are only interested in the value of one or just a few properties in an object. To check for a specific property you can use &lt;code&gt;expect.objectContaining&lt;/code&gt; to check if the object contains a property with the expected value.&lt;/p&gt;

&lt;p&gt;In the code below we’re checking if a report dialog function has been called with the users name and email.&lt;br&gt;&lt;br&gt;
The actual object is much larger but we don’t really care about the other properties, in this case the user information is the moving parts in the object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;expect(showReportDialog).toHaveBeenCalledWith(
  expect.objectContaining({
    user: {
      name,
      email,
    }
  })
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  expect.anything()
&lt;/h2&gt;

&lt;p&gt;Callback functions or randomly generated values can sometimes be a hassle to handle in tests since they might change, but it is possible to ignore specific properties or arguments using &lt;code&gt;expect.anything&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function loadScript(scriptUrl:string, callback:() =&amp;gt; unknown) { ... }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When testing the above function we ’re not interested in the callback function and only wants to check if loadScript have been called with the correct script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it("should call loadScript", () =&amp;gt; {
  someFunctionUsingLoadScript();

  expect(loadScript).toHaveBeenCalledWith(
    "script.js",
    expect.anything()
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;expect.anything&lt;/code&gt; does not match null or undefined values&lt;/p&gt;

&lt;h2&gt;
  
  
  expect.any()
&lt;/h2&gt;

&lt;p&gt;Another way to match more broadly is &lt;code&gt;expect.any(constructor)&lt;/code&gt; where you can accept any match based on the constructor being passed to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;expect(someFn).toHaveBeenCalledWith({
  someNumber: expect.any(Number),
  someBoolean: expect.any(Boolean),
  someString: expect.any(String)
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  expect.assertions()
&lt;/h2&gt;

&lt;p&gt;When doing asynchronous tests it can be helpful to make sure that all assertions have been run when the test ends.&lt;br&gt;&lt;br&gt;
The &lt;code&gt;expect.assertions(Number)&lt;/code&gt; ensures that the correct number of assertions have been made.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test('prepareState prepares a valid state', () =&amp;gt; {
  expect.assertions(1);
  prepareState((state) =&amp;gt; {
    expect(validateState(state)).toBeTruthy();
  })
  return waitOnState();
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  test.each
&lt;/h2&gt;

&lt;p&gt;For some unit tests you may want run the same test code with multiple values. A great way to do this is using the &lt;code&gt;test.each&lt;/code&gt; function to avoid duplicating code.&lt;/p&gt;

&lt;p&gt;Inside a template string we define all values, separated by line breaks, we want to use in the test. The first line is used as the variable name in the test code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test.each`
  someId
  ${undefined}
  ${null}
  ${""}
`("$someId should reject promise", async ({ someId}) =&amp;gt; {
  expect.assertions(1);
  await expect(someFn(someId))
    .rejects.toEqual(errorMessage);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multiple input variables can be added separated by the pipe (|) character.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test.each`
  someId | anotherValue
  ${undefined} | ${a}
  ${null} | ${b}
  ${""} | ${c}
`("$someId with $anotherValue should reject promise", async ({ someId, anotherValue }) =&amp;gt; {
  expect.assertions(1);
  await expect(someFn(someId, anotherValue))
    .rejects.toEqual(errorMessage);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; it is also possible to define the values as arrays, &lt;a href="https://jestjs.io/docs/ro/23.x/api#testeachtablename-fn-timeout" rel="noopener noreferrer"&gt;read more in the official documentation.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  jest.requireActual
&lt;/h2&gt;

&lt;p&gt;This is just a reminder never to forget adding &lt;code&gt;jest.requireActual&lt;/code&gt; when mocking libraries. If you do forget, it can lead to weirdness that may take several hours to solve (talking from personal experience here 😁).&lt;/p&gt;

&lt;p&gt;So what does it do?&lt;/p&gt;

&lt;p&gt;When mocking a library you may only want to mock a specific function of the library and keep the rest of the library intact.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jest.mock("@material-ui/core", () =&amp;gt; ({
  ...jest.requireActual("@material-ui/core"),
  useMediaQuery: jest.fn()
}));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So in the code above we create a new mock object, using &lt;code&gt;jest.requireActual&lt;/code&gt; to spread all the functions of the library and only mock useMediaQuery in this case.&lt;/p&gt;

</description>
      <category>react</category>
      <category>testing</category>
      <category>jest</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Extending the theme in Material UI with TypeScript</title>
      <dc:creator>Fredrik Bergqvist</dc:creator>
      <pubDate>Fri, 26 Jun 2020 21:46:07 +0000</pubDate>
      <link>https://dev.to/fredrikbergqvist/extending-the-theme-in-material-ui-with-typescript-lba</link>
      <guid>https://dev.to/fredrikbergqvist/extending-the-theme-in-material-ui-with-typescript-lba</guid>
      <description>&lt;p&gt;When we started using Material UI (version 3) the support for extending the built-in theme was pretty lacking. The theme interface did not handle any additional color settings such as "success" or "warn" and trying to extend the theme did not work since parts of interfaces can't be overwritten.&lt;/p&gt;

&lt;p&gt;So instead of extending the theme we used a separate object with corresponding interface to handle the extra colors that we needed. Not ideal but as the colors only were used in a few places we could afford to wait for the support in MUI to get better.&lt;/p&gt;

&lt;p&gt;Flash forward a year and the support is here so extend the theme we did!&lt;br&gt;
The &lt;a href="https://material-ui.com/guides/typescript/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; tells us to use &lt;a href="https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation" rel="noopener noreferrer"&gt;module augmentation&lt;/a&gt; to merge our theme with the built-in theme by creating a &lt;code&gt;index.d.ts&lt;/code&gt; file and adding our properties in that.&lt;/p&gt;
&lt;h2&gt;
  
  
  The official way of doing it
&lt;/h2&gt;

&lt;p&gt;So if I want to extend the &lt;code&gt;typography&lt;/code&gt; object to accept a &lt;code&gt;secondaryFontFamily&lt;/code&gt; I would have to do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="kr"&gt;module&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@material-ui/core/styles/createTypography&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;TypographyOptions&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;secondaryFontFamily&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Typography&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;secondaryFontFamily&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then creating a custom theme factory function to create the theme.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createMuiTheme&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@material-ui/core/styles&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nf"&gt;createMyTheme&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="nx"&gt;CustomTheme&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;createMuiTheme&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;palette&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;createPalette&lt;/span&gt;&lt;span class="p"&gt;({}),&lt;/span&gt;
  &lt;span class="na"&gt;typography&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;secondaryFontFamily&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Georgia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works well but still uses the &lt;code&gt;Theme&lt;/code&gt; interface which makes it harder to know what has been extended.&lt;/p&gt;

&lt;h2&gt;
  
  
  Our project setup
&lt;/h2&gt;

&lt;p&gt;We package our code in different NPM packages and use &lt;a href="https://github.com/lerna/lerna" rel="noopener noreferrer"&gt;Lerna&lt;/a&gt; to handle the development environment.&lt;/p&gt;

&lt;p&gt;That means that the theme is used over several packages and when we implemented the solution above we quickly realized that we had to add the &lt;code&gt;index.d.ts&lt;/code&gt; file in every project, making it very cumbersome to add new attributes in the future. &lt;/p&gt;

&lt;p&gt;Back to the drawing board.&lt;/p&gt;

&lt;h2&gt;
  
  
  A different solution
&lt;/h2&gt;

&lt;p&gt;So we need an interface for our customised theme that we can share with our packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;CustomTypography&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Typography&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;secondaryFontFamily&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;CustomTheme&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Theme&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;typography&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CustomTypography&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nf"&gt;createMyTheme&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="nx"&gt;CustomTheme&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;createMuiTheme&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;palette&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;createPalette&lt;/span&gt;&lt;span class="p"&gt;({}),&lt;/span&gt;
  &lt;span class="na"&gt;typography&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;secondaryFontFamily&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Georgia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;That will unfortunately result in the following error:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu44owg20izwd3tdgh732.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu44owg20izwd3tdgh732.PNG" alt="Image of code with a typescript error: S2345: Argument of type '{ palette: Palette; typography: { secondaryFontFamily: string; }; }' is not assignable to parameter of type 'ThemeOptions'. Types of property 'typography' are incompatible." width="782" height="218"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TypeScript does not allow interfaces to be merged and since &lt;code&gt;CustomTheme extends Theme&lt;/code&gt; it seemed that we are out of luck.&lt;/p&gt;

&lt;p&gt;Then I discovered &lt;code&gt;Omit&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  TypeScript Omit to the rescue!
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/utility-types.html#omittk" rel="noopener noreferrer"&gt;&lt;code&gt;Omit&amp;lt;T,K&amp;gt;&lt;/code&gt;&lt;/a&gt; is an utility type that constructs a type by picking all properties from T and then removing K. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So by using &lt;code&gt;Omit&lt;/code&gt; we can create our own utility type &lt;code&gt;Modify&lt;/code&gt;. (&lt;a href="https://stackoverflow.com/questions/41285211/overriding-interface-property-type-defined-in-typescript-d-ts-file/55032655" rel="noopener noreferrer"&gt;Props to Qwerty&lt;/a&gt; )&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Modify&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which will merge two interfaces, removing any members on &lt;code&gt;T&lt;/code&gt; that exists in &lt;code&gt;R&lt;/code&gt; and then adding &lt;code&gt;R&lt;/code&gt; to the resulting type.&lt;/p&gt;

&lt;p&gt;So using &lt;code&gt;Modify&lt;/code&gt; we can do this instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Theme&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@material-ui/core&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Typography&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@material-ui/core/styles/createTypography&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CustomTypography&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Modify&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
  &lt;span class="nx"&gt;Typography&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;secondaryFontFamily&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CustomTheme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Modify&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
  &lt;span class="nx"&gt;Theme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;typography&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CustomTypography&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nf"&gt;createMyTheme&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="nx"&gt;CustomTheme&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;baseTheme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createMuiTheme&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;palette&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;createPalette&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;baseTheme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;typography&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;secondaryFontFamily&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Georgia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And use it in our app like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyApp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myTheme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createMyTheme&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ThemeProvider&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CustomTheme&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;myTheme&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CssBaseline&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SomeComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ThemeProvider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;I hope this can help get someone with the same problem some ideas and if you have solved the problem in another way, please let me know.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>materialui</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Mocking redux useSelector-hook</title>
      <dc:creator>Fredrik Bergqvist</dc:creator>
      <pubDate>Thu, 19 Mar 2020 22:54:57 +0000</pubDate>
      <link>https://dev.to/fredrikbergqvist/mocking-redux-useselector-hook-2ale</link>
      <guid>https://dev.to/fredrikbergqvist/mocking-redux-useselector-hook-2ale</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;em&gt;Update&lt;/em&gt;&lt;/em&gt;&lt;br&gt;
&lt;em&gt;There is an official way of using &lt;a href="https://testing-library.com/docs/example-react-redux#__docusaurus" rel="noopener noreferrer"&gt;RTL with redux&lt;/a&gt; as some people pointed out in the comments but I never got it to work.&lt;br&gt;
It may be me being incompetent or something in my project that causes issues so my solution to only mock &lt;code&gt;useSelector&lt;/code&gt; may still be of use.&lt;/em&gt; 🙄&lt;/p&gt;

&lt;p&gt;Recently I finally made the switch from &lt;a href="https://enzymejs.github.io/enzyme/" rel="noopener noreferrer"&gt;Enzyme&lt;/a&gt; to &lt;a href="https://github.com/testing-library/react-testing-library" rel="noopener noreferrer"&gt;React testing library (RTL)&lt;/a&gt; which also means that instead of rendering components using &lt;code&gt;shallow&lt;/code&gt; like Enzyme proposes, with React testing library the whole component and its child components is rendered, much like Enzymes &lt;code&gt;mount&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The switch to RTL coupled with using hooks instead of HOCs when using Redux got me writing a lot of new component tests but I did run in to some problem when I tried to use the &lt;code&gt;useSelector&lt;/code&gt;-hook from Redux multiple times expecting different responses.&lt;/p&gt;

&lt;p&gt;The component that I wanted to test as a search component that made calls similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MySearchComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rows&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
    &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;hasMore&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;useSelector&lt;/code&gt; takes a callback function that takes the state as an argument and returns a slice of the state.&lt;/p&gt;

&lt;p&gt;So my first approach when trying to test the component was to send two different responses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;requireActual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mockReturnValueOnce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockConfigState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mockReturnValueOnce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockSearchState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;

&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MySearchComponent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;afterEach&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mockClear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should render&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getByTestId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MySearchComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Which worked fine until I realised that a child component also calls useSelector and therefore crashed. 😱&lt;/p&gt;

&lt;p&gt;I knew I needed something that would support all possible selectors that I needed but still could be modified on a test by test basis. &lt;br&gt;
I had a mock state ready, but not the method to alter and inject it. &lt;br&gt;
Until I ran across &lt;code&gt;jest.fn().mockImplementation&lt;/code&gt;...&lt;/p&gt;
&lt;h2&gt;
  
  
  The solution to my problems
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useSelector&lt;/code&gt; takes a callback as its argument and all I had to do was to call that callback with a compatible state that would satisfy all my components needs and they would do the rest as implemented.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;requireActual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;

&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MySearchComponent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;beforeEach&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mockImplementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockAppState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nf"&gt;afterEach&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mockClear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should render a query&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getByTestId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MySearchComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;query_testId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockAppState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;should not render if query is empty&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;localMockState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;mockAppState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;mockShoppingState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="nx"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mockImplementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;localState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;queryByTestId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MySearchComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;queryByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;query_testId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toBeNull&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So in the code above I mock &lt;code&gt;useSelector&lt;/code&gt; from the react-redux npm package and replaces it with a function that executes any given callback function with my mocked state as an argument. This is done before every test.&lt;/p&gt;

&lt;p&gt;In the second test I create a second mocked state that I want to use for just that test so I override &lt;code&gt;useSelector&lt;/code&gt; to make sure it uses my updated state instead of the default mock state.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Parting words
&lt;/h2&gt;

&lt;p&gt;I hope this helped someone to learn a little bit more about how to test their code and what can be achieved with jest and tools like RTL (which is great, try it!)&lt;/p&gt;

&lt;p&gt;All typos my own and please leave a comment if you have a question or something does not make sense.&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>testing</category>
      <category>jest</category>
    </item>
    <item>
      <title>How to add an RSS-feed to your Next.js site</title>
      <dc:creator>Fredrik Bergqvist</dc:creator>
      <pubDate>Mon, 02 Dec 2019 12:15:52 +0000</pubDate>
      <link>https://dev.to/fredrikbergqvist/how-to-add-an-rss-feed-to-your-next-js-site-1h02</link>
      <guid>https://dev.to/fredrikbergqvist/how-to-add-an-rss-feed-to-your-next-js-site-1h02</guid>
      <description>&lt;p&gt;I love RSS-feeds (and still curse Google for cancelling Google Reader) and use them as my main news source for things I find interesting so with this article I would like to help people to add RSS-feeds to their blogs.&lt;/p&gt;

&lt;p&gt;If you read my article about how to add a &lt;a href="https://www.bergqvist.it/blog/2019/11/22/adding-sitemap-to-nextjs" rel="noopener noreferrer"&gt;sitemap.xml to your next.js site&lt;/a&gt; you will recognise much of the code, it's pretty much the same base but with slightly different XML-markup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the page
&lt;/h2&gt;

&lt;p&gt;First off we need a page that can return the XML. I suggest you name it &lt;em&gt;"rss"&lt;/em&gt;, &lt;em&gt;"feed"&lt;/em&gt; or something similar.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;getInitialProps&lt;/code&gt; we get our blog posts and set the "Content-Type"-header to  &lt;em&gt;"text/xml"&lt;/em&gt; so the browser knows that should expect XML instead of HTML.&lt;br&gt;
We then generate the XML and passes it on to the browser using &lt;code&gt;res.write&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default class Rss extends React.Component {
  static async getInitialProps({ res }: NextPageContext) {
    if (!res) {
      return;
    }
    const blogPosts = getRssBlogPosts();
    res.setHeader("Content-Type", "text/xml");
    res.write(getRssXml(blogPosts));
    res.end();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Generating the base XML for the feed
&lt;/h2&gt;

&lt;p&gt;For the base XML document you will need to add some basic information like the title of the log, a short description, link to your website and the language of the content.&lt;br&gt;
Title, link and description are mandatory elements in the &lt;a href="https://validator.w3.org/feed/docs/rss2.html" rel="noopener noreferrer"&gt;RSS specification&lt;/a&gt; but add as many optional elements as you find useful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getRssXml = (blogPosts: IBlogPost[]) =&amp;gt; {
  const { rssItemsXml, latestPostDate } = blogPostsRssXml(blogPosts);
  return `&amp;lt;?xml version="1.0" ?&amp;gt;
  &amp;lt;rss version="2.0"&amp;gt;
    &amp;lt;channel&amp;gt;
        &amp;lt;title&amp;gt;Blog by Fredrik Bergqvist&amp;lt;/title&amp;gt;
        &amp;lt;link&amp;gt;https://www.bergqvist.it&amp;lt;/link&amp;gt;
        &amp;lt;description&amp;gt;${shortSiteDescription}&amp;lt;/description&amp;gt;
        &amp;lt;language&amp;gt;en&amp;lt;/language&amp;gt;
        &amp;lt;lastBuildDate&amp;gt;${latestPostDate}&amp;lt;/lastBuildDate&amp;gt;
        ${rssItemsXml}
    &amp;lt;/channel&amp;gt;
  &amp;lt;/rss&amp;gt;`;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding XML for the blog items
&lt;/h2&gt;

&lt;p&gt;With the basic stuff out of the way all we need to go is generate some XML for the blog posts and figure out when the blog was updated.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;item&lt;/code&gt; element should at least contain a link to the blog post, the date when it was published and the text. You can either opt to put a short description and force the user to come visit your page or put the whole post in the XML.&lt;/p&gt;

&lt;p&gt;If you have HTML-markup in your text you need to enclose it within a  &lt;code&gt;&amp;lt;![CDATA[${post.text}]]&amp;gt;&lt;/code&gt;-tag or HTML encode the text.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const blogPostsRssXml = (blogPosts: IBlogPost[]) =&amp;gt; {
  let latestPostDate: string = "";
  let rssItemsXml = "";
  blogPosts.forEach(post =&amp;gt; {
    const postDate = Date.parse(post.createdAt);
    if (!latestPostDate || postDate &amp;gt; Date.parse(latestPostDate)) {
      latestPostDate = post.createdAt;
    }
    rssItemsXml += `
      &amp;lt;item&amp;gt;
        &amp;lt;title&amp;gt;${post.title}&amp;lt;/title&amp;gt;
        &amp;lt;link&amp;gt;
          ${post.href}
        &amp;lt;/link&amp;gt;

        &amp;lt;pubDate&amp;gt;${post.createdAt}&amp;lt;/pubDate&amp;gt;
        &amp;lt;description&amp;gt;
        &amp;lt;![CDATA[${post.text}]]&amp;gt;
        &amp;lt;/description&amp;gt;
    &amp;lt;/item&amp;gt;`;
  });
  return {
    rssItemsXml,
    latestPostDate
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Finishing up
&lt;/h2&gt;

&lt;p&gt;The last thing you need to do is to add a &lt;code&gt;link&lt;/code&gt; to your feed somewhere in the head of your document.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link 
  rel="alternate" 
  type="application/rss+xml" 
  title="RSS for blog posts" 
  href="https://www.bergqvist.it/rss" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will make it easier for browsers and plugins to auto discover your feed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/fredrikbergqvist/36704828353ebf5379a5c08c7583fe2d" rel="noopener noreferrer"&gt;The code is available as a gist on github&lt;/a&gt; and please leave a comment with any feedback.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>typescript</category>
      <category>tutorial</category>
      <category>react</category>
    </item>
    <item>
      <title>Adding a dynamic sitemap.xml to a next.js site</title>
      <dc:creator>Fredrik Bergqvist</dc:creator>
      <pubDate>Fri, 22 Nov 2019 13:52:07 +0000</pubDate>
      <link>https://dev.to/fredrikbergqvist/adding-a-sitemap-to-next-js-354g</link>
      <guid>https://dev.to/fredrikbergqvist/adding-a-sitemap-to-next-js-354g</guid>
      <description>&lt;p&gt;While building my blog with Next.js I naturally wanted to support sitemaps because it can supposedly help out the search engines. For my small blog it will not make any difference but for larger sites it's more important. &lt;a href="https://support.google.com/webmasters/answer/156184?hl=en#" rel="noopener noreferrer"&gt;Google has this to say&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Using a sitemap doesn't guarantee that all the items in your sitemap will be crawled and indexed, as Google processes rely on complex algorithms to schedule crawling. However, in most cases, your site will benefit from having a sitemap, and you'll never be penalized for having one.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Page for the sitemap
&lt;/h2&gt;

&lt;p&gt;The first thing we need to do is to create a &lt;em&gt;sitemap.xml.ts&lt;/em&gt; page in the "page"-folder. This will expose a &lt;em&gt;&lt;a href="https://yourdomain.com/sitemap.xml" rel="noopener noreferrer"&gt;https://yourdomain.com/sitemap.xml&lt;/a&gt;&lt;/em&gt; url that you can submit to search engines. if you want to you can omit the &lt;em&gt;.xml&lt;/em&gt; part and only use &lt;em&gt;/sitemap&lt;/em&gt;, Google search console will accept the url anyway.&lt;/p&gt;

&lt;p&gt;We want to make sure that we set the &lt;code&gt;Content-Type&lt;/code&gt; header to &lt;code&gt;text/xml&lt;/code&gt; and to write our xml output to the response stream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default class Sitemap extends React.Component {
  static async getInitialProps({ res }: any) {
    const blogPosts = getBlogPosts();
    res.setHeader("Content-Type", "text/xml");
    res.write(sitemapXml(blogPosts));
    res.end();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Generating the base xml
&lt;/h2&gt;

&lt;p&gt;For the site map we want to list all pages on the site, apart from the blog posts we have to add all additional pages that we want the search engines to find. &lt;/p&gt;

&lt;p&gt;I have an about page that I add manually together with the index page but if you have many pages I suggest you create an array and add them in a more automated way.&lt;/p&gt;

&lt;p&gt;I won't go into the inner workings of all the properties of a sitemap but I want to mention the &lt;code&gt;&amp;lt;priority&amp;gt;&lt;/code&gt;-tag that lets you set a value between 0 and 1 indicating how important you think the page is.&lt;br&gt;
&lt;code&gt;&amp;lt;lastmod&amp;gt;&lt;/code&gt; is used to indicate when the page was changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sitemapXml = (blogPosts: IBlogPostListItem[]) =&amp;gt; {
  const { postsXml, latestPost } = blogPostsXml(blogPosts);
  return `
  &amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
    &amp;lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&amp;gt;
    &amp;lt;url&amp;gt;
      &amp;lt;loc&amp;gt;https://www.bergqvist.it/&amp;lt;/loc&amp;gt;
      &amp;lt;lastmod&amp;gt;${latestPost}&amp;lt;/lastmod&amp;gt;
      &amp;lt;priority&amp;gt;1.00&amp;lt;/priority&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
      &amp;lt;loc&amp;gt;https://www.bergqvist.it/about&amp;lt;/loc&amp;gt;
      &amp;lt;priority&amp;gt;0.5&amp;lt;/priority&amp;gt;
    &amp;lt;/url&amp;gt;
    ${blogPostsXml}
  &amp;lt;/urlset&amp;gt;`;
};

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding xml for the blog posts
&lt;/h2&gt;

&lt;p&gt;As mentioned above I want to add the dynamic blog post pages to the site map as well. In the &lt;code&gt;blogPostsXml&lt;/code&gt;-function I generate xml for all posts and keep track of when the latest post was posted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const blogPostsXml = (blogPosts: IBlogPostListItem[]) =&amp;gt; {
  let latestPost = 0;
  let postsXml = "";
  blogPosts.map(post =&amp;gt; {
    const postDate = Date.parse(post.createdAt);
    if (!latestPost || postDate &amp;gt; latestPost) {
      latestPost = postDate;
    }
    postsXml += `
    &amp;lt;url&amp;gt;
      &amp;lt;loc&amp;gt;${post.url}&amp;lt;/loc&amp;gt;
      &amp;lt;lastmod&amp;gt;${postDate}&amp;lt;/lastmod&amp;gt;
      &amp;lt;priority&amp;gt;0.80&amp;lt;/priority&amp;gt;
    &amp;lt;/url&amp;gt;`;
  });
  return {
    postsXml,
    latestPost
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Last words
&lt;/h2&gt;

&lt;p&gt;Make sure to check that you do not add any pages to the sitemap that is blocked in your robots.txt. &lt;/p&gt;

&lt;p&gt;If you have a large site with many pages (100 or more) you can split up the sitemap into several smaller ones, for very large sites this is a must!&lt;/p&gt;

&lt;p&gt;I hope this could help out someone and please leave a comment. &lt;a href="https://gist.github.com/fredrikbergqvist/d2896af6563b38c3d880c0e135925ad1" rel="noopener noreferrer"&gt;A full gist of the code can be found here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>typescript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>SmashingConf New York 2019</title>
      <dc:creator>Fredrik Bergqvist</dc:creator>
      <pubDate>Wed, 20 Nov 2019 22:00:09 +0000</pubDate>
      <link>https://dev.to/fredrikbergqvist/smashingconf-new-york-2019-bcp</link>
      <guid>https://dev.to/fredrikbergqvist/smashingconf-new-york-2019-bcp</guid>
      <description>&lt;p&gt;A few weeks ago I had the privilege of attending &lt;a href="https://smashingconf.com/ny-2019/" rel="noopener noreferrer"&gt;SmashingConf in New York&lt;/a&gt;, travelling from a chilly Swedish autumn to a quite pleasant New York end of summer. I spent the weekend fighting jet lag by taking long walks and touristing so I could be fit for the first workshop I had signed up for. &lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;Great conference but not technical enough for my taste. &lt;/p&gt;

&lt;p&gt;Watch &lt;a href="https://vimeo.com/showcase/6539127/video/367872808" rel="noopener noreferrer"&gt;Wes Bos's talk&lt;/a&gt; (&lt;a href="https://wesbos.github.io/Slam-Dunk-JavaScript/#1" rel="noopener noreferrer"&gt;slides&lt;/a&gt;) for good javascript insights and all talks can be found here: &lt;a href="https://vimeo.com/showcase/6539127" rel="noopener noreferrer"&gt;SmashingConf NY 2019 videos&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lightning fast performance with Scott Jehl.
&lt;/h2&gt;

&lt;p&gt;Spending a whole day digging deep into performance sounded like a great way to spend a Monday so I had booked. &lt;br&gt;
The venue for the workshop was located at Microsoft, just a short walk from my hotel so I had plenty of time to get a good nights sleep, eat a steady breakfast and still make the 9 o'clock start without any problems.&lt;/p&gt;

&lt;p&gt;Scott Jehl was really easy going and the workshop covered tools to measure performance and how to mitigate issues. &lt;br&gt;
I would however have liked more nitty gritty details and less time spent on using online tools. &lt;/p&gt;

&lt;p&gt;The &lt;a href="https://vimeo.com/showcase/6539127/video/367887672" rel="noopener noreferrer"&gt;Move Fast &amp;amp; Don't Break Things-talk&lt;/a&gt; that Scott held on the second day of the conference was a distilled version of the workshop so check it out if you're interested.&lt;/p&gt;

&lt;p&gt;In the evening I attended a Shopify-hosted jam session with lightning talks from Shopify, Microsoft, Forestry (about TinaCMS!) and a few others talkers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conference day one
&lt;/h2&gt;

&lt;p&gt;The conference took place at the &lt;a href="https://newworldstages.com/" rel="noopener noreferrer"&gt;New world stages&lt;/a&gt; close to time square. &lt;br&gt;
The main stage was a musical theatre which worked well but the rest of the venue was quite cramped. &lt;/p&gt;

&lt;p&gt;Many of the talks were more of the UX/inspirational kind which can be nice, but I'm more of a hands on person so my favourites were the following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://vimeo.com/showcase/6539127/video/367853694" rel="noopener noreferrer"&gt;Marcy Sutton on Garbage Pail Components&lt;/a&gt; is a talk about accessibility with an 80's twist (and not even the first 80's flashback this conference).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://vimeo.com/showcase/6539127/video/367872808" rel="noopener noreferrer"&gt;Wes Bos on Slam Dunk Your JavaScript Fundamentals&lt;/a&gt; is jam packed with new APIs that you might not have heard of yet, especially the Intl-object.&lt;/p&gt;

&lt;p&gt;In the evening the SmashingConf party took place at Waylon bar where speakers and participants got to mingle. I met lots of friendly people from both sides of the pond and had a great time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conference day two
&lt;/h2&gt;

&lt;p&gt;With a slight hangover and not enough sleep I went to see the &lt;a href="https://vimeo.com/showcase/6539127/video/367875942" rel="noopener noreferrer"&gt;mystery speaker dina Amin&lt;/a&gt; who makes wonderful stop-motion shorts with old discarded electrical devices.&lt;/p&gt;

&lt;p&gt;Another fun talk was the &lt;a href="https://vimeo.com/showcase/6539127/video/367885607" rel="noopener noreferrer"&gt;Using a Modern Web to Recreate 1980s Horribly Slow &amp;amp; Loud Loading Screen&lt;/a&gt;-talk where Remy Sharp recreated the loading screens of his old 80's Sinclair Spectrum computer. All ending with a photo of the audience being transformed to the Spectrum image format and shown on the big screen&lt;/p&gt;

&lt;h2&gt;
  
  
  Next.js with Remy Sharp
&lt;/h2&gt;

&lt;p&gt;With the conference over I still had a workshop day to look forward to so I went back to the Microsoft office to learn some next.js!&lt;/p&gt;

&lt;p&gt;The workshop was good with all the basics of next.js covered, I had already tinkered with next.js (my homepage runs for example) so most things were familiar but luckily I had not read up on the changes in 9.1 so I did learn some new tricks.&lt;/p&gt;

&lt;p&gt;All in all I had a great week in NYC and I met lots of great people but I would have liked if the conference and workshops were a bit more technical.&lt;/p&gt;

</description>
      <category>techtalks</category>
      <category>nextjs</category>
      <category>javascript</category>
      <category>smashingconf</category>
    </item>
    <item>
      <title>Typing React with typescript</title>
      <dc:creator>Fredrik Bergqvist</dc:creator>
      <pubDate>Sat, 18 May 2019 20:22:16 +0000</pubDate>
      <link>https://dev.to/fredrikbergqvist/typing-react-with-typescript-53ma</link>
      <guid>https://dev.to/fredrikbergqvist/typing-react-with-typescript-53ma</guid>
      <description>&lt;p&gt;TypeScript has really taken off lately in the React world and rightly so, it's a great way to keep your code documented and help you keep some errors at bay.&lt;/p&gt;

&lt;p&gt;I have worked with TypeScript in a few projects the last few years, all involving Angular in some form but never with React so I had some new challenges in front of me when I started my current project with React and TypeScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functional components
&lt;/h2&gt;

&lt;p&gt;Let us start off easy with a basic functional component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;OwnProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;myProp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;OwnProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;MyButton&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ComponentType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;OwnProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create an interface called &lt;code&gt;OwnProps&lt;/code&gt; where we define all props we want the component to have. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;OwnProps&lt;/code&gt; is then used to define the component: &lt;code&gt;React.FC&amp;lt;OwnProps&amp;gt;&lt;/code&gt; as well as when we export the component &lt;code&gt;as React.ComponentType&amp;lt;OwnProps&amp;gt;&lt;/code&gt; to clearly signal what props are available.&lt;/p&gt;

&lt;p&gt;In this basic example it may seem unnecessary but, as we shall see further down, when components get more complex it will save us some headache.&lt;br&gt;
This approach will also help negate the following error:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;error TS2604: JSX element type 'MyComponent' does not have any construct or call signatures.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Wrapping another component
&lt;/h2&gt;

&lt;p&gt;In some cases you might want to wrap another component and include that components interface in your own. This is usually the case when working with base components from a library and since we use Material UI (MUI for short)as a base component library I will use that in the example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;OwnProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;buttonText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;OwnProps&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;ButtonProps&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;buttonText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;buttonText&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;MyButton&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ComponentType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Props&lt;/code&gt; type can be seen as the sum of all parts that the component will consist of. In this case we want to use &lt;code&gt;ButtonProps&lt;/code&gt; from MUIs Button component and merge it with our own and expose both props to the consumers of the component. &lt;/p&gt;

&lt;p&gt;This is still not very advanced but since we use MUI we also use JSS for styling so let us add that to the mix!&lt;/p&gt;

&lt;h2&gt;
  
  
  Using WithStyles and WithTheme
&lt;/h2&gt;

&lt;p&gt;Since we use MUI we handle styling with JSS and the generated CSS classes are injected via the &lt;code&gt;withStyles&lt;/code&gt; HOC. This caused some issues since a classes object containing the class names is injected into your props and to use classes you would need to include that object in your prop type.&lt;/p&gt;

&lt;p&gt;Luckily we have the WithStyles type to help us! &lt;br&gt;
&lt;code&gt;WithStyles&amp;lt;typeof styles&amp;gt;&lt;/code&gt; takes a generic type argument of your style object so you don't have to worry about it keeping your types DRY.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://material-ui.com/guides/typescript/" rel="noopener noreferrer"&gt;The typescript section at Material UI&lt;/a&gt; explains the problems with withStyles more in detail, ready up on it if you plan to use MUI and TypeScript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Theme&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;StyleRules&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;createStyles&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;spacing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unit&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;OwnProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;buttonText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PublicProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;OwnProps&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;ButtonProps&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;PublicProps&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;WithStyles&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;classes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;buttonText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;classes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;buttonText&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;withStyles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;MyButton&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ComponentType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PublicProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The addition we've done here is adding a &lt;code&gt;PublicProps&lt;/code&gt; type and using that instead of the Props type when exporting the component. This is of course because we also want to use WithStyles but not expose it to anyone using the button.&lt;/p&gt;

&lt;p&gt;Had we used the Props type instead of PublicProps we would get a pesky TypeScript error complaining about classes property missing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftkar36p4i2qa8m4qk82h.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftkar36p4i2qa8m4qk82h.PNG" alt="TS2741: Property 'classes' is missing in type '{buttonText: string}' but required in type '{ classes: Record&amp;lt;string, string&amp;gt;}'" width="728" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux connect and compose
&lt;/h2&gt;

&lt;p&gt;But what would React be without state handling? We use Redux for this so let us connect MyButton and get the buttonText prop from state instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Theme&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;StyleRules&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;createStyles&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;spacing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unit&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;StateProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;buttonText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;DispatchProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ThunkDispatch&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;IAppState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Action&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;OwnProps&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PublicProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;OwnProps&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;ButtonProps&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;PublicProps&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; 
             &lt;span class="nx"&gt;DispatchProps&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; 
             &lt;span class="nx"&gt;StateProps&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; 
             &lt;span class="nx"&gt;WithTheme&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;
             &lt;span class="nx"&gt;WithStyles&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;classes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;buttonText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;classes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;buttonText&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapStateToProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IAppState&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;StateProps&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;buttonText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buttonText&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;withStyles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;withTheme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StateProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;DispatchProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OwnProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;IAppState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapStateToProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;MyButton&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ComponentType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PublicProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;We have not started using hooks for our state so we go with the good old connect. Since we now use both &lt;code&gt;connect&lt;/code&gt; and &lt;code&gt;withStyles&lt;/code&gt; we need to use &lt;code&gt;compose&lt;/code&gt; to merge them.&lt;/p&gt;

&lt;p&gt;We create &lt;code&gt;StateProps&lt;/code&gt; as return type of &lt;code&gt;mapStateToProps&lt;/code&gt; and &lt;code&gt;DispatchProps&lt;/code&gt; which types the dispatch function that is returned by default if we don't add a &lt;code&gt;mapDispatchToProps&lt;/code&gt; function. In out case we use Thunk so if you use some other tool you need to use that instead.&lt;/p&gt;

&lt;p&gt;I have also added an example of using the MUI theme in the component, this is done by adding &lt;code&gt;{ withTheme: true }&lt;/code&gt; as a second argument to &lt;code&gt;withStyles&lt;/code&gt;. &lt;br&gt;
This will inject a theme property into your props so we need to specify that in our &lt;code&gt;Props&lt;/code&gt; type using &lt;code&gt;WithTheme&lt;/code&gt; from MUI.&lt;/p&gt;
&lt;h2&gt;
  
  
  Bonus: typing the useState hook
&lt;/h2&gt;

&lt;p&gt;Not really rocket science but good to know!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello world!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;`
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  End note
&lt;/h2&gt;

&lt;p&gt;TypeScript can be very frustrating when starting out with many errors that are not clear so I hope this article could be of some help or inspiration, it took us a while to settle on a TypeScript pattern that worked for us and helped us mitigate most of the errors that might occur when working with different libraries.&lt;/p&gt;

&lt;p&gt;Feel free to leave any suggestions in the comment field.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/fredrikbergqvist/dcff13628f9ebe3fdc2d28c67f24921c" rel="noopener noreferrer"&gt;A gist of the code examples are available at GitHub.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>redux</category>
      <category>materialui</category>
    </item>
  </channel>
</rss>
