Comments (6)
By default, props that are passed to components are shallowly converted into a JS object. This means that when you pass a map into the :style
prop here, the props object will look like:
{"style": <...CLJS map obj>}
I do special handling of the :style
prop for "native" components (e.g. "div"
and "button"
), where I convert the entire structure into a JS object.
I'm not sure how best to handle this case. React Native components are semantically "native", but it's impossible to tell that from a functional component that you might want to grab the :styles
prop from and manipulate it in some way with clojure core functions.
For now, you can write your View
s and whatnot like:
($ View {:style #js {:background "blue"}})
or in the case where you want to pass in an array of styles:
($ View {:style #js [#js {:background "blue"}]})
Note that if you use React Native's StyleSheet function that it should just work, e.g.:
(def styles (.create StyleSheet #js {:red #js {:color "red"})))
($ View {:style (.-red styles)})
You could probably create a thin wrapper around StyleSheet
to make this a little more ergonomic.
from helix.
ah I see, in snippet below you're checking if it's a string or a keyword, and imagine function names are symbols so it's being interpreted as non-native, hence why props' nested object is not converted (makes sense that you'd only want to do that on primitives)
https://github.com/Lokeh/helix/blob/0984dfd9add46885da7314ef1e4dc8d22c9e1573/src/helix/core.clj#L89
appreciate you showing the above example. i also see how you dynamically handled that macro creation for dom elements. very cool to see macros at work 👍
from helix.
A (currently undocumented) feature of the $
macro is that you can do "spread props" by passing in a map to a special &
key.
(let [extra-props {:a :extra-a
:b :extra-b
:c :extra-c}]
($ MyComponent {:c :c
:d :d
& extra-props}))
MyComponent
will receive the props:
{:a :extra-a
:b :extra-b
:c :extra-c
:d :d}
Keys in the map passed to &
will override keys passed in literally, so that you can give a default value and allow it to be dynamically overridden, just like spread props in JSX.
from helix.
oh yea, I was looking at code I thought conversion was being done for the second argument of every component called with $
, but didn't realize it was only for dom primitives
I guess the best solution would be to do create my own wrappers and do same conversion for primitives I plan to use
from helix.
yes, that would probably be the best option. Something akin to:
(defmacro view
[& args]
(cond
(map? (first args)) `^js/React.Element (create-element
view-type
~(helix.core/props (first args) true)
~@(rest args))
:else `^js/React.Element (create-element view-type nil ~@args))))
from helix.
mind explaining what the '&
does here? why is it necessary to check if that symbol is contained in the clj-map, I don't see where it's being inserted elsewhere
from helix.
Related Issues (20)
- Any good way to make multimethods work as component functions? HOT 8
- gensym / anonymous function syntax breaks fast refresh HOT 2
- Include resources in deps.edn :paths
- $ clj-kondo hook fails to lint & right when component is not a symbol HOT 1
- The $ macro and new JSX transform HOT 14
- `wrap-fx` is public HOT 1
- Wrappers for useTransition, useDeferredValue, etc. HOT 1
- Is it possible to bypass the camel-casing of properties? HOT 7
- Add wrapper around createRef with atom protocols HOT 2
- How to use with uikit? HOT 2
- Running tests in nodejs with jsdom HOT 4
- Companies using Helix HOT 1
- indent metadata on helix.dom macros HOT 3
- Create use-isomorphic-layout-effect HOT 6
- Make self-hosted CLJS compatible HOT 2
- Fix indentation with CIDER HOT 3
- Release request HOT 1
- Warning: React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead. HOT 2
- `helix.dom/tags` removed HOT 2
- defnc taking pre- & post-conditions HOT 1
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 helix.