Skip to content

Commit

Permalink
Add a react 18 preload namespace
Browse files Browse the repository at this point in the history
React 18 introduces a new render API:

https://react.dev/blog/2022/03/29/react-v18#react-dom-client

The reagent.dom.client namespace wraps this new API. reagent.dom still
wraps the old one. React throws a deprecation warning if you use the
old API with React 18.

If a user uses the new API in their project, they will still get the
warning because of re-frame-10x using the old API. This is confusing.
It looks as if their code is suspect, when actually ours is.

re-frame-10x depends on the user's react version. That means we can't
predict whether the user will use React 17 or React 18, or which API
they will invoke. We can't just load reagent.dom.client, because it
only works with React 18. We can't load it conditionally based on the
react version, since it's a general limitation of cljs that we can't
require a namespace at runtime.

Added a second preload namespace. Since only the user can know which
API to use, we let them declare it themselves. The original preload
remains unchanged:

{:preloads [day8.re-frame-10x.preload]}

It calls the old API, which works in all cases. It throws the
deprecation warning if the user has React 18.

The new preload only works with React 18, using the new API properly:

{:preloads [day8.re-frame-10x.preload.react-18]}

Fixes #367
  • Loading branch information
kimo-k committed Oct 23, 2023
1 parent 14d55bc commit 80cc83f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This change

## Unreleased

#### Added

- New preload namespace, compatible with React 18: `day8.re-frame-10x.preload.react-18`. See #367

#### Changed

- Upgrade re-frame to 1.3.0
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,15 @@ If you don't meet those pre-requisites, see the docs on [advanced setups](/docs/
Reagent Versions | React Versions | re-frame-10x Artifact | Status |
----------------- | ------------------- | --------------------- | ------ |
`1.0.x` | `17.x` | [![clojars](https://img.shields.io/clojars/v/day8.re-frame/re-frame-10x?style=for-the-badge&logo=clojure&logoColor=fff)](https://clojars.org/day8.re-frame/re-frame-10x) | [![ci](https://github.com/day8/re-frame-10x/workflows/ci/badge.svg)](https://github.com/day8/re-frame-10x/actions?workflow=ci) |
`1.2.x` | `17.x` - `18.x` | [![clojars](https://img.shields.io/clojars/v/day8.re-frame/re-frame-10x?style=for-the-badge&logo=clojure&logoColor=fff)](https://clojars.org/day8.re-frame/re-frame-10x) | [![ci](https://github.com/day8/re-frame-10x/workflows/ci/badge.svg)](https://github.com/day8/re-frame-10x/actions?workflow=ci) |
`1.0.x` | `17.x` | `[day8.re-frame/re-frame-10x "1.8.1"]` | Frozen |
`0.10.x` | `16.13.x` | `[day8.re-frame/re-frame-10x "0.7.0"]` | Frozen |
`0.9.x` | `16.9.x` | `[day8.re-frame/re-frame-10x "0.5.2"]` | Frozen |
`0.8.x` | `16.x.x` - `16.8.6` | `[day8.re-frame/re-frame-10x "0.4.3"]` | Frozen |
`0.6.x` - `0.7.x` | `15.x` | `[day8.re-frame/re-frame-10x "0.3.7"]` | Frozen |
**For React 18:** Consider using `{:preloads [day8.re-frame-10x.preload.react-18]}`. This will use [React's new render API](https://react.dev/blog/2022/03/29/react-v18#react-dom-client). Otherwise, expect deprecation warnings from React.
**For versions < 0.4.0:** If your project uses React 16 and Reagent 0.8.0-alpha2 (or higher) then you will need to add the qualifier `-react16` to the version, e.g. `[day8.re-frame/re-frame-10x "VERSION-react16"]`.
**Note**: If also using [re-com](https://github.com/day8/re-com) then on upgrading reagent you may also need to upgrade re-com.
Expand Down
14 changes: 0 additions & 14 deletions src/day8/re_frame_10x.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns day8.re-frame-10x
(:require
[day8.re-frame-10x.inlined-deps.reagent.v1v2v0.reagent.core :as r]
[day8.re-frame-10x.inlined-deps.reagent.v1v2v0.reagent.dom :as rdom]
[day8.re-frame-10x.inlined-deps.re-frame.v1v3v0.re-frame.core :as rf]
[day8.re-frame-10x.inlined-deps.re-frame.v1v3v0.re-frame.db]
[day8.re-frame-10x.inlined-deps.spade.git-sha-93ef290.container.dom :as spade.dom]
Expand All @@ -10,7 +9,6 @@
[day8.reagent.impl.component :refer [patch-wrap-funs patch-custom-wrapper]]
[day8.re-frame-10x.tools.datafy :as tools.datafy]
[day8.re-frame-10x.tools.shadow-dom :as tools.shadow-dom]
[day8.re-frame-10x.events :as events]
[day8.re-frame-10x.components.re-com :as rc]
[day8.re-frame-10x.navigation.views :as container]
[day8.re-frame-10x.panels.settings.subs :as settings.subs]
Expand Down Expand Up @@ -134,21 +132,9 @@
{:panel-type :inline
:debug? debug?}]])

(defn inject!
[]
(rf/clear-subscription-cache!)
(let [shadow-root (create-shadow-root)]
(rdom/render (create-style-container shadow-root) shadow-root)))

(defn patch!
"Sets up any initial state that needs to be there for tracing. Does not enable tracing."
[]
(patch-custom-wrapper)
(patch-wrap-funs)
(patch-next-tick))

(defn init!
[]
(patch!)
(rf/dispatch-sync [::events/init {:debug? debug?}])
(inject!))
5 changes: 1 addition & 4 deletions src/day8/re_frame_10x/preload.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@
re-frame-10x; e.g.
{:compiler {:preloads [day8.re-frame-10x.preload] ...}}"
(:require
[day8.re-frame-10x :as re-frame-10x]))

(re-frame-10x/init!)
(:require [day8.re-frame-10x.preload.react-17]))
31 changes: 31 additions & 0 deletions src/day8/re_frame_10x/preload/react_17.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns day8.re-frame-10x.preload.react-17
"Use this namespace with the :preloads compiler option to perform the necessary setup for enabling
re-frame-10x; e.g.
{:compiler {:preloads [day8.re-frame-10x.preload] ...}}"
(:require
["react" :as react]
[clojure.string :as str]
[day8.re-frame-10x :as re-frame-10x]
[day8.re-frame-10x.inlined-deps.reagent.v1v2v0.reagent.dom :as rdom]
[day8.re-frame-10x.inlined-deps.re-frame.v1v3v0.re-frame.core :as rf]
[day8.re-frame-10x.events :as events]))

(let [react-major-version (first (str/split react/version #"\."))]
(when-not (= "17" react-major-version)
(js/console.warn "Re-frame-10x expects React 17, but React"
react-major-version
"is loaded. This causes deprecation warnings."
(when (= "18" react-major-version)
"To fix this, try declaring `day8.re-frame-10x.preload.react-18` in your shadow-cljs.edn (instead of `day8.re-frame-10x.preload`). See https://github.com/day8/re-frame-10x/README.md#compatibility-matrix"))))

(re-frame-10x/patch!)

(rf/dispatch-sync [::events/init {:debug? re-frame-10x/debug?}])

(rf/clear-subscription-cache!)

(def shadow-root (re-frame-10x/create-shadow-root))

(rdom/render (re-frame-10x/create-style-container shadow-root)
shadow-root)
25 changes: 25 additions & 0 deletions src/day8/re_frame_10x/preload/react_18.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(ns day8.re-frame-10x.preload.react-18
"Use this namespace with the :preloads compiler option to perform the necessary setup for enabling
re-frame-10x; e.g.
{:compiler {:preloads [day8.re-frame-10x.preload] ...}}
This namespace is intended for use with React 18.
It launches re-frame-10x with react's new dom client API.
See https://react.dev/blog/2022/03/29/react-v18#react-dom-client."
(:require
[day8.re-frame-10x :as re-frame-10x]
[day8.re-frame-10x.inlined-deps.re-frame.v1v3v0.re-frame.core :as rf]
[day8.re-frame-10x.inlined-deps.reagent.v1v2v0.reagent.dom.client :as rdc]
[day8.re-frame-10x.events :as events]))

(re-frame-10x/patch!)

(rf/dispatch-sync [::events/init {:debug? re-frame-10x/debug?}])

(rf/clear-subscription-cache!)

(def shadow-root (re-frame-10x/create-shadow-root))

(rdc/render (rdc/create-root shadow-root)
(re-frame-10x/create-style-container shadow-root))

0 comments on commit 80cc83f

Please sign in to comment.