Comments (5)
Thanks for spotting this β I really should have mentioned that in the documentation (and added a warning/assert to the code).
And it is quite impossible to solve, I'm afraid, at least using the same conventions as in plain React.
That is because Cloact doesn't render the entire tree returned by a component function in one go. It just turns the top-level children into React components (wrapping "native" components as necessary), and passes their children as Clojure data-structures unmodified.
That implies all sorts of benefits: for example you don't have to pay for the conversion between e.g maps and objects unless the child component actually needs to render. And child components get to play directly with the vectors and maps passed to them (and are free to "change" them, pass them along, and cache them), lazy evaluation of seqs, etc. Basically all the advantages of immutable data.
The downside is that this breaks refs, since the children's components will actually be rendered by them (and quite possibly not rendered at all).
Now, we could invent an alternative, that hopefully would work just as well. For example, we could wrap the child that you want to ref in a function/component that registers its child with the grand-parent on mount (or render), so that your example would become something like:
(defn login [props children this]
[:form
(cloact/register-ref this :username [:input#username {:type "text"}])])
(def login-with-focus
(with-meta login
{:component-did-mount (fn [this] (-> this cloact/refs :username cloact/dom-node (.focus)))}))
Btw, your particular example could be solved without refs, as in the todomvc example:
(def todo-edit (with-meta todo-input
{:component-did-mount #(.focus (cloact/dom-node %))}))
from reagent.
Wrapped Children
It's good to know that you don't plan on changing that design decision to wrap all children in a component. Do you think a large application would have memory problems due to there being a component associated with every DOM element?
Refs
I think your style of wrapping the input in a component function is actually the best way. It's more reusable and less complex than adding new functions to the API.
As you said it would be helpful to document best practices around accessing DOM nodes and how cloact.core/render-component
converts functions and markup into components.
Let me know if I can help.
from reagent.
Wrapped Children
The memory overhead seems to be quite small: React has some optimization for single-children-components, and it is quite lean memory-wise anyway.
At least the Cloact demo page seems to work well on my phone - and that is actually quite heavy (as in thousands of components), since every single parenthesis in the code examples is its own component. :)
Refs
Definitely agree that some more docs (and especially examples) should be written. Even it might be a bit early to claim "best practices"...
from reagent.
But say, what if I want to focus and blur an input component from elsewhere? Either through a keyboard handler bound to the document in the parent component, or simply through a different component altogether?
from reagent.
String refs are legacy feature in React and are likely to be removed in future version: https://reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs
Ref callbacks work.
from reagent.
Related Issues (20)
- Bad react key warning when using false as key
- examples/material-ui not work HOT 1
- examples/material-ui not work HOT 1
- Docs on βChanged?β inaccurate? HOT 5
- React 18 does not live reload with Shadow-CLJS in non-trivial projects HOT 5
- Prop types
- `TypeError` when using function components with `r/create-class` with `dom.server/render-to-string`
- ReactDOM.render is no longer supported in React 18. Use createRoot instead. HOT 2
- shadow-cljs emits a warning when using with-let HOT 4
- Korean input broken on :input and :textarea HOT 1
- Update docs? HOT 1
- MUI ThemeProvider not working HOT 1
- @@ -62,6 +62,45 @@ class _Config { } } // eslint-disable-next-line valid-jsdoc /** * fetch problem title, level via solved.ac api * @see {@link https://solvedac.github.io/unofficial-documentation/#/operations/getProblemByIdArray} * @param {string[]} pids - list of problem id (up to 100) * @param { ({ problemId, titleKo, level }) => void } callback */ function fetchProblemsFromSolvedAc(pids, callback) { const query = encodeURIComponent(pids.join(',')); console.log(`https://solved.ac/api/v3/problem/lookup?problemIds=${query}`); fetch(`https://solved.ac/api/v3/problem/lookup?problemIds=${query}`) .then((res) => { console.log('fetchProblemsFromSolvedAc', res); return res; }) .then((res) => res.json()) .then(callback) .catch(() => callback(null)); }
- Readme says npm i or add deps to .edn but (in my case at least) needed both HOT 1
- Any interest in being able to replace `react/createElement` with a custom function? HOT 7
- The cursor disappears at the end of a controlled input on Chrome
- Reusable components following HTML semantics of Opional Attributes and Variadic Children HOT 3
- Controlled input loses cursor under ShadowRoot in React 18 HOT 3
- snake_case mentioned instead kebab-case in documentation of create-class function.
- Class name composition is harder than it needs to be
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reagent.