How to `useDeno` in a React Component?

Hi, let me introduce you to Aleph.js, a React Framework in Deno, inspired by Next.js.

Different with Next.js, Aleph.js don’t need webpack or other bundler since Aleph.js use the ESM imports syntax during development, like Vite and snowpack. Every module only needs to be compiled once and then cached on the disk. When a module changes, Aleph.js just recompile that single module, there’s no time wasted re-bundling every changes, and instant updates in the browser by HMR(Hot Module Replacement) with React Fast Refresh.

Aleph.js works in Deno, the modern and secure runtime for JavaScript and TypeScript. No package.json and node_modules directory needed, all dependencies are imported as URL and managed by Aleph.js:

import React from "https://esm.sh/react"export default function Page() {
return <h1>Hello World!</h1>
}

Features

  • Zero Config
  • Typescript in Deno
  • ES Module Ready
  • Import Maps
  • HMR with Fast Refresh
  • File-system Routing
  • Markdown Page
  • Built-in CSS(Less) Support
  • SSR/SSG
  • Plugins

Why Create It?

Well, as a full-stack developer and a user of Next.js, I use React in almost all of my projects and feel good with Vercel.

But there are still some defects, I would call it, let me down:

  • Over-complicated Webpack
  • Dirty AMD/UMD/CommonJS
  • Unnecessary package.json and ts.config.json
  • node_modules Hells

Maybe those are not real Pain Point for us and everybody knows, always can find a polyfill, but we deserve the best tools to make our life more easy.

Just as vite,snowpack,esbuild,deno,swc do: gives the best developer experience.

Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust. ¹

  • run javascript(es2020)/typescript without any config
  • WebAssembly
  • Permissions( — allow-read, — allow-write, — allow-net, etc)
  • Built-in high performance tools(bundler,fmt,doc,lint) written in Rust
  • Browser compatibility

For me, Deno is the prefect runtime to run full-stack framework, so I initiated Aleph.js.

`useDeno`

Most concepts of Aleph.js are same with Next.js, but add some different features I always wish Next do.

For exemaple, In Next.js, two functions called getStaticProps and getServerSideProps are used by the pages to fetch data at build time(SSR) or on each request. This solution isolates the data and view likes different roles of the back-end and front-end.

But for me, i prefer use Hook to fetch data during SSR, just like Aleph.js do: a hook called useDeno provided that allows you fetch data at build time(SSR) in a component with Deno runtime, that's more React Style likely:

import React from "https://esm.sh/react"
import { useDeno } from "https://deno.land/x/aleph/mod.ts"
export default function Page() {
const version = useDeno(() => {
return Deno.version
})
return (
<p>Powered by Deno v{version.deno}</p>
)
}

or fetch data asynchronously:

import React from "https://esm.sh/react"
import { useDeno, useRouter } from "https://deno.land/x/aleph/mod.ts"
export default function Post() {
const { params } = useRouter()
const post = useDeno(async () => {
return await (await fetch(`https://.../post/${params.id}`)).json()
})
return (
<h1>{post.title}</h1>
)
}

How It Works

The useDeno hook will receive a sync or async callback(the first parameter), during the build time(SSG) each callback of useDeno will be invoked and then cache the returned data, after in the browser the callbacks of useDeno will be ignored and the cached data will be used, that's it.

More usages please check: https://alephjs.org/docs

Status

Aleph.js in alpha stage, you can check our website built by Aleph.js in Vercel, and welcome to take a shot.

--

--

--

Creator of Aleph.js and esm.sh.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The dev.to project with GatsbyJS-13

A simple trick to rewrite your Cloud Functions URL

First-Class Functions

Nested & Auto-dismissable Layers In React

Nested & Auto-dismissable Layers In React

Deploy your Docz site with Heroku

Migrating to webpack 5 to improve build time and reduce chunk sizes

Learn Test Utils: renderFor

The Features that Make React Native More Native than Ever Before

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
X.

X.

Creator of Aleph.js and esm.sh.

More from Medium

10 reasons why I love Material-UI

Creating React “Widgets” that can be embedded on any website, by anyone

Basic Checklist for Creating a React Component

How to Create hello world react application?