DEV Community

JavaScript Joel
JavaScript Joel

Posted on

React: "I really wish this is how I could write components."

Challenge Accepted!

code for useMatchFetch down below.

import React from "react";
import { useMatchFetch } from "./effects/useMatchFetch";

export const Example = () => {
  const render = useMatchFetch("https://swapi.co/api/people/1/?format=json");

  return render({
    pending: () => <div>Loading</div>,
    error: err => <div>{err.toString()}</div>,
    data: data => <pre>{JSON.stringify(data, null, 2)}</pre>
  });
};
Enter fullscreen mode Exit fullscreen mode

Watch my Live Stream

Want to see my process on how I created this? Watch me on Twitch!

Twitch Screenshot

useMatchFetch

I actually really like this. I think I might end up using this in a few places.

import { useState, useEffect } from "react";

const render = data => match =>
  data.pending ? match.pending()
  : data.error ? match.error(data.error)
  : data.data  ? match.data(data.data)
  : null // prettier-ignore

export const useMatchFetch = url => {
  const [data, setData] = useState({ pending: true });

  useEffect(() => {
    fetch(url)
      .then(response => response.json())
      .then(data => setData({ data, pending: false }))
      .catch(error => setData({ error, pending: false }));
  }, [url]);

  return render(data);
};
Enter fullscreen mode Exit fullscreen mode

End

Follow me on Twitter @joelnet

Cheers!

Oldest comments (46)

Collapse
 
ben profile image
Ben Halpern • Edited

I really think this is such a cool DEV use-case: Taking a tweet and expanding on, replying to and generally going deeper on the idea.

I also think a browser extension which could add a button like this to Twitter would be kind of awesome if anyone wants to build that 😄

We actually have an existing Twitter extension we haven't touched in a while but would definitely welcome this added functionality if anyone wants to make a PR (along with some other needed updates 😬)

GitHub logo thepracticaldev / DevTwitter

Bringing dev.to headlines to your Twitter browsing experience.

DevTwitter Chrome Extension

Bringing dev.to headlines to your Twitter browsing experience.

Alt text

Installing

Go to The Chrome Store to download the extension.

To access development releases, simply download or clone this code and load as an unpacked extension.

Unpacked Extension

  • If you downloaded the code, unzip the file.
  • Open (chrome://extensions/) or select hamburger menu > More Tools > Extensions in the menu.
  • Enable the developer mode at top right.
  • Click Load unpacked extension... and select the chrome-extension folder.

Features

  • Display dev.to links on the sidebar of the Twitter desktop browser under other trends, bringing a bit more dev life into your Twitter browsing experience. Simple, eh?

Future features

Twitter is a great way to connect and collaborate with other developers, and it would be fun to build in features that did that. The dev.to team will be thinking of ways to do this, but contributions are super welcome.

It is important…

Anyway, sorry to go on a tangent, this just made me happy. 🙂

Collapse
 
joelnet profile image
JavaScript Joel

I really think this is such a cool DEV use-case: Taking a tweet and expanding on, replying to and generally going deeper on the idea.

I like this too. If someone has created a tweet, there's already some interest in that subject. I liked this tweet in particular because I kept thinking about it a few hours later.

Better integration would be awesome!

Collapse
 
philnash profile image
Phil Nash

Heh, I've done that a couple of times, about git and about starting Node projects. I like the spark that a tweet can form in your mind and send you down a rabbit hole following ideas and writing code and posts in response.

Thread Thread
 
joelnet profile image
JavaScript Joel

Absolutely!

The fastest way to get me to do something is to tell me it can't be done!

Collapse
 
vonheikemen profile image
Heiker

I, too, want pattern matching in javascript and i also want Maybes and Eithers.

Collapse
 
joelnet profile image
JavaScript Joel

Same. You can use Maybes and Eithers today with a package import. The barrier is getting people familiar with them. Right now there's a lot of push back against them just because of familiarity bias.

If the language adopted them as native though, people would jump on board.

People are silly like that.

Collapse
 
vonheikemen profile image
Heiker

If the language adopted them as native though, people would jump on board.

I think so too. Rust has them, they are called Option and Result. I secretly want Rust to become more popular so people can learn about them and how useful they are, and also start using them in other languages (and by other i mean javascript)

Thread Thread
 
kenbellows profile image
Ken Bellows

Interestingly, Rust has started to become more popular in Web Land as a language for writing WASM modules, in part because of the strong support from the Rust team themselves. See developer.mozilla.org/en-US/docs/W...

Collapse
 
patroza profile image
Patrick Roza

I like this one dev.to/_gdelgado/type-safe-error-h...
Currently im working on trying to reduce all the friction that happens when a language doesn’t support it more natively, hopefully have a post up with samples in the next month or so :)

Collapse
 
kayis profile image
K
Collapse
 
joelnet profile image
JavaScript Joel

Z looks interesting for sure. It's a little limited though. But it would work fine for this use case.

Collapse
 
jasonsbarr profile image
Jason Barr

Reminds me a lot of this component package

Collapse
 
joelnet profile image
JavaScript Joel

I ran into a few different packages that did something very similar. This is the reason I didn't create an npm package. I think there are enough already.

But I thought the process would be fun, which is why I lived streamed it.

Hopefully people enjoy the process that went into making it happen.

Cheers!

🍻

Collapse
 
jasonsbarr profile image
Jason Barr

I hope it was fun! Sorry if my comment implied I didn't see value in this, I totally do. I dig seeing the thought process behind the code in addition to the code itself. Have a good one!

Thread Thread
 
joelnet profile image
JavaScript Joel

No your comment didn't imply that it wasn't fun. I got you ;)

You too!

Collapse
 
johnpaulada profile image
John Paul Ada

I'd probably use ReasonReact for this :D

Collapse
 
joelnet profile image
JavaScript Joel

ReactReason is a very interesting project. It's hard to convince large companies to switch to a totally different language though.

I'm still looking forward to playing with ReasonML for some personal projects.

Collapse
 
bilal_fazlani profile image
Bilal

Scala-js allows writing react components this way

Collapse
 
joelnet profile image
JavaScript Joel

This sounds pretty cool. I haven't seen scalajs but if it has pattern matching I'm down!

Collapse
 
eliseumds profile image
Eliseu Monar dos Santos • Edited

What if you want to render a loader while new data is loading? That means you'd render a loader + the current data at the same time. Same with errors.

Collapse
 
joelnet profile image
JavaScript Joel

Submit a pull request ;)

Collapse
 
nikolicstjepan profile image
Stjepan

First time that I "unicorned" article! Well played Sir!

Collapse
 
joelnet profile image
JavaScript Joel

I'm so honored 😁

Collapse
 
netojose profile image
José Neto

Wow!

Collapse
 
joelnet profile image
JavaScript Joel

Cheers!

🍻

Collapse
 
buinauskas profile image
Evaldas Buinauskas • Edited

Very functional and very Elm'ish.

I've watched this talk and it covered exactly that.

Collapse
 
joelnet profile image
JavaScript Joel

Thanks for the tip! I'm saving this video to my Watch Later.

Collapse
 
buinauskas profile image
Evaldas Buinauskas

Definitely worth watching. You might find some inspiration on how to improve your React code even more :)

Thread Thread
 
joelnet profile image
JavaScript Joel

There's always room for improvement!

Collapse
 
davidchase profile image
David Chase

Nice write up! :)

Reminds me of these posts using daggy from fantasy-land
medium.com/javascript-inside/slayi...
datarockets.com/blog/javascript-pa...

nice little library that wraps it up
github.com/devex-web-frontend/remo...

Collapse
 
joelnet profile image
JavaScript Joel

One of the reasons, I decided not to make this an npm package. I figured something was already out there. I thought it would be fun to show the process.

Looks like those other libs have the same idea. I always love seeing an article with daggy!

daggy is actually a perfect use case for this.

Cheers!

🍻

Collapse
 
davidchase profile image
David Chase

Yeah indeed a lot times there’s a solution but not a how to reach it write up or something like that so def keep coming with the process writes up for sure !! :)

Thread Thread
 
joelnet profile image
JavaScript Joel

That was one thing I found interesting about this process. Most of the time the ideal solution would be created ahead of time and then you would teach that process.

But it's interesting to see how you would think to get to that conclusion on your own.

I think that's the difference between live streams and tutorials. You get to see the thought process, which I really enjoy watching!

Cheers!

🍻

Thread Thread
 
davidchase profile image
David Chase

But it's interesting to see how you would think to get to that conclusion on your own.

yah that process is fun, we used to do FP lunch sessions at an old job and refactor some current/legacy code into something we just learned or found interesting right on a big screen tv so everyone could watch and we talk thru the process.