|
| 1 | +# ReactNativeWebHelloWorld |
| 2 | + |
| 3 | +A way to share application logic between a React Web app and a React Native app, |
| 4 | +while keeping the individual component rendering unique to each platform. |
| 5 | + |
| 6 | +For a thorough discussion of this project, please read my [Blog Post][bg] about |
| 7 | +it. |
| 8 | + |
| 9 | +## The app |
| 10 | + |
| 11 | +React Native | React Web |
| 12 | +:----------------------------------------:|:-------------------------------------: |
| 13 | + |  |
| 14 | + |
| 15 | +The app itself is a very simple Hello World (ish) app. Not only does it show |
| 16 | +"Hello World"... but when you click (or tap) it... it changes from red to blue! |
| 17 | +woah! |
| 18 | + |
| 19 | +## Directory Structure |
| 20 | + |
| 21 | +`android` houses the Android project files, `ios` houses the iOS project files, |
| 22 | +and `web` houses the `webpack` configs and `index.html` (it is also the |
| 23 | +destination of our minified bundle). |
| 24 | + |
| 25 | +The `app` itself is primarily based on my [React/Webpack/Redux Boilerplate][bp], |
| 26 | +for more info on that head over there. The only key difference can be found in |
| 27 | +`app/native`, and `app/web`. What's going on here is both the native app and |
| 28 | +web app are sharing their core application logic, while keeping the individual |
| 29 | +rendering separate. |
| 30 | + |
| 31 | +The reasoning for this is twofold - for one, React Native/React wasn't designed |
| 32 | +to be a "write once, run everywhere" framework. Facebook constantly calls it a |
| 33 | +"learn once, write everywhere" framework - the idea being that you tailor your |
| 34 | +implementation to the platform you're writing for. The second is that React |
| 35 | +Native and React are ultimately... different and the code wouldn't be reusable |
| 36 | +without some crazy aliases. |
| 37 | + |
| 38 | +## Configured Scripts |
| 39 | + |
| 40 | +### Running in dev/production |
| 41 | + |
| 42 | +There are 8 defined scripts in [package.json][pg]: |
| 43 | + |
| 44 | + 1. `start` |
| 45 | + 1. `ios-bundle` |
| 46 | + 1. `ios-dev-bundle` |
| 47 | + 1. `android-bundle` |
| 48 | + 1. `android-dev-bundle` |
| 49 | + 1. `web-bundle` |
| 50 | + 1. `web-dev` |
| 51 | + |
| 52 | +### start |
| 53 | + |
| 54 | +`start` is used when running/bundling the native application. When you open |
| 55 | +either the xcode project or the android studio project and hit "run", it |
| 56 | +kicks off a node server via the `start` command. Every time you make a |
| 57 | +JavaScript change, instead of needing to rebuild and recompile your application, |
| 58 | +you simply refresh the app and the changes are magically there. As this is not |
| 59 | +a React Native guide I will not be going into more detail than that - further |
| 60 | +information can be found on Facebook's [React Native Getting Started][gs] guide. |
| 61 | + |
| 62 | +### bundlin |
| 63 | + |
| 64 | +For `ios-bundle`, `ios-dev-bundle`, `android-bundle`, and `android-dev-bundle`, |
| 65 | +the script builds the JavaScript bundle (either minified or not-minified |
| 66 | +depending on the presence of `dev` or not), and places it where the |
| 67 | +corresponding project expects it to be for running locally on your device. |
| 68 | +Again, you can find more info on running on your device on Facebook's |
| 69 | +[React Native Getting Started][gs]. |
| 70 | + |
| 71 | +### web town |
| 72 | + |
| 73 | +`web-dev` kicks off a webpack server on port 3001, it utilizes hot reloading |
| 74 | +with some redux-time-machine-magic to have a crazy awesome dev experience where |
| 75 | +you can rewind and revert actions in your application. |
| 76 | + |
| 77 | +`web-bundle` creates a minified JavaScript bundle (that also houses the minified |
| 78 | +css) and places it next to the `index.html` in `web/public` that you can serve |
| 79 | +with any static file server. |
| 80 | + |
| 81 | +### clear-cache |
| 82 | + |
| 83 | +Every now and then, when React Native is doing it's thing, you'll swear that |
| 84 | +you've changed something, but alas it is still causing your app to break! oh |
| 85 | +noes! what do we do! |
| 86 | + |
| 87 | +I'm glad you asked! Just run `npm run clear-cache`! |
| 88 | + |
| 89 | +## Further Configuration |
| 90 | + |
| 91 | +Webpack sets the `PLATFORM_ENV` environment variable to be `web`. You can use |
| 92 | +this check to conditionally load different files depending on if you're building |
| 93 | +your native or web app. For example - you can abstract out the difference |
| 94 | +between local storage mechanisms. |
| 95 | + |
| 96 | +[gs]: https://facebook.github.io/react-native/docs/getting-started.html |
| 97 | +[bp]: https://github.com/kauffecup/react-redux-webpack-boilerplate |
| 98 | +[bg]: http://jkaufman.io/react-web-native-codesharing/ |
0 commit comments