Not reviewed yet: this page may be outdated or inconsistent with the other guides. The API reference is generated from the current source.
Upgrading to 1.0
This guide covers upgrading from Reagent 0.9 or 0.10. Reagent 0.10 deprecated the functions that 1.0 removes, so if your code compiles without deprecation warnings on 0.10, most of the work is already done. All changes are listed in the changelog.
1. Use reagent.dom for DOM functions
The DOM related functions were removed from reagent.core. Use the versions in
the reagent.dom namespace instead:
Removed from reagent.core |
Use instead |
|---|---|
render |
reagent.dom/render |
unmount-component-at-node |
reagent.dom/unmount-component-at-node |
force-update-all |
reagent.dom/force-update-all |
dom-node |
reagent.dom/dom-node |
(ns example.core
(:require [reagent.dom :as rdom]))
(defn ^:export run []
(rdom/render [view] (.getElementById js/document "app")))
This keeps react-dom out of reagent.core, so that non-DOM environments like
React Native don't need it.
reagent.core/render still exists as a deprecated stub: it causes a
deprecation warning when compiling and throws an error about the moved function
when called.
2. Replace reagent.interop
The reagent.interop namespace ($, $!, unchecked-aget and
unchecked-aset macros) was removed in 0.10. Use ClojureScript's own interop
instead, e.g. (.-foo obj), (.foo obj) and (set! (.-foo obj) value), or
goog.object/get and goog.object/set for string keys.
3. Replace component-path
reagent.core/component-path was removed in 0.10. The component's
displayName gives nearly the same information.
4. Check error handling
Since 0.10, "Error rendering component" messages no longer contain the component stack. To get the stack, use an error boundary.
5. Update React
Reagent 1.0 depends on React 17.0.1, through the Cljsjs packages or the npm
packages declared in its deps.cljs. If you declare the React dependencies in
your project, update them:
npm i react@17.0.1 react-dom@17.0.1
New in 1.0
-
Function components: Reagent can create React function components instead of class components for Reagent components, which lets you use React hooks in them. Class components are still the default. Turn this on with a compiler object:
(def compiler (r/create-compiler {:function-components true})) (r/set-default-compiler! compiler)See Reagent Compiler.
-
:f>creates a function component from a ClojureScript function in Hiccup, so you can use hooks in that component even with the default compiler:[:f> my-component args]. -
:r>uses a React component like:>does, but passes the props object as is, without converting it from ClojureScript.