UIx provides a way to perform side effects in components via React’s effect hook. Effects are useful for making HTTP requests, interacting with the DOM, or performing any kind of impure operation that mutates some global state and might need to be cleaned up at some point in the future.
The uix.core/use-effect
function wraps React's useEffect
hook and takes care of handling certain inconsistencies between JS and Clojure worlds so that you don't have to. More information about effect hook is available in React documentation on effect hook.
In the example below, the effect's callback will execute and update the document’s title after every update of the component.
(defui example []
(let [[clicks set-clicks!] (uix.core/use-state 0)]
(uix.core/use-effect
(fn []
(set! (.-title js/document)
(str "Number of clicks: " clicks))))
($ :button {:on-click #(set-clicks! inc)}
"increment")))
An optional dependencies vector allows conditional execution of an effect. In the example above, dependencies are not passed into the effect causing it to execute on every update of the component. Read more about conditional effect execution in React docs.
In the example below the effect will be executed every time when a number of clicks
becomes a multiple of 5.
(defui example []
(let [[clicks set-clicks!] (uix.core/use-state 0)]
(uix.core/use-effect
(fn []
(set! (.-title js/document)
(str "Number of clicks: " clicks)))
[(zero? (mod clicks 5))])
($ :button {:on-click #(set-clicks! inc)}
"increment")))
Can you improve this documentation? These fine people already did:
Shaun Mahood & roman01laEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close