- 1. Introduction
- 2. Installation
- 3. Usage
- 4. REPL
- 5. Configuration
- 6. Build Configuration
- 7. Targeting the Browser
- 8. Targeting JavaScript Modules
- 9. Targeting React Native
- 10. Targeting node.js
- 11. Embedding in the JS Ecosystem The :npm-module Target
- 12. Testing
- 13. JavaScript Integration
- 14. Generating Production Code All Targets
- 15. Editor Integration
- 16. Troubleshooting
- 17. Publishing Libraries
- 18. What to do when things don’t work?
- 19. Hacking
9. Targeting React Native
The :target :react-native
produces code that is meant to integrate into the default react-native
tooling (eg. metro
). Tools like expo
which wrap those tools should automatically work and require no additional setup.
You will need the same basic main configuration as in other targets (like :source-paths
), the build specific config is very minimal and requires at least 2 options (besides :target
itself)
:init-fn | (required). The namespace-qualified symbol of your apps init function. This function will be called once on startup and should probably render something. |
:output-dir | (required). The directory used to write output files. |
{:source-paths [...]
:dependencies [...]
...
:builds
{:app
{:target :react-native
:init-fn demo.app/init
:output-dir "app"}}}
When compiled this results in a app/index.js
file intended to be used as an entry point for the react-native
tools. During development the :output-dir
will contain many more files but you should only reference the generated app/index.js
directly. A release
build will only generate the optimized app/index.js
and requires no additional files.
9.1. React Native
There are two ways to use react-native
, "plain" react-native
, which allows you to use native code and libraries and the one "wrapped" in expo (described below). All the steps described above are sufficient to start using shadow-cljs with the plain react-native
. See this example repo:
9.2. Expo
expo makes working with react-native
quite easy. There are two provided example setups.
Both examples were generated using expo init …
and the only adjusted change in the config was adding the proper entryPoint
to the generated app.json
.
{
"expo": {
"name": "hello-world",
"slug": "reagent-expo",
...
"entryPoint":"./app/index.js",
...
}
}
expo
requires that a React Component is registered on startup which can be done manually or by using the shadow.expo/render-root
function which takes care of creating the Component and instead directly expects a React Element instance to start rendering.
(defn start
{:dev/after-load true}
[]
(expo/render-root (r/as-element [root])))
(defn init []
(start))
init
is called once on startup. Since the example doesn’t need to do any special setup it just calls start
directly. start
will be called repeatedly when watch
is running each time after the code changes were reloaded. The reagent.core/as-element
function can be used to generate the required React Element from the reagent hiccup markup.
9.3. Hot Code Reload
React native requires to reload not only compiled files and files explicitly requiring those, but also their transitive dependents, for changes to take effect. To accomplish this, use :reload-strategy
option as in Hot Reload of Transitive Dependents.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论