A scalable typescript React Native app with React Native Navigation + Redux + TSLint
Table of Contents
- Make sure your dev server is running at
localhost:3000
. - This app uses yarn. Run
yarn install
to install dependencies. - In
src/config/index.ts
, add the root of your dev server toROOT
. For simulators (ios),localhost:3000
is fine. If using a simulator or connecting a device, use your host's IPv4 address instead of 'localhost'.
- Set up your development environment with React Native CLI.
- Connect your device or run your emulator/simulator.
- Run the Metro Bundler from the terminal with
npm start
, and launch with eitherreact-native run-android
orreact-native run-ios
(Alternatively use VScode'sReact Native Tools
commands).
To run tslint on the application:
yarn lint
To fix most tslint issues automatically
yarn lint:fix
Unit tests are under __test__
directory at root.
To run unit test on the application:
npm run test
To find unit test coverage for the application:
npm run test:coverage
/
├── android Android Native code
├── ios iOS Native Code
├── shared
│ ├── redux Application state logic
│ │ ├── constants
│ │ ├── actions
│ │ ├── api
│ │ ├── reducers
│ │ | ├── assets
│ │ | ├── components
│ │ | └── user
│ │ └── store
│ └── socket.ts Socket server
├── src
│ ├── view UI
│ │ ├── assets
│ │ ├── components Custom UI components
│ │ ├── forms Typography
│ │ └── screens Isolated views--screens, modals, overlays
| | ├── Modals
| | └── ...
│ ├── config App Configuration
│ ├── constants Screens, Localization
│ └── navigators Router, Navigation
├── __tests__ Unit Tests
│ ├── presentation
│ └── redux
├── .babelrc
├── .gitignore
├── .travis.yml Travis CI
├── tsconfig.json TypeScript Configuration
├── tslint.js TSLint configuration - extending AirBnb
├── tsconfig.json
├── app.json
├── index.js Application Entry point
├── package.json
└── README.md
shared
Everything related to application business logic (store) resides under this directory.
src
Only presentation layer for the app, styles, images, icons are meant to be under this.
Make sure node version installed is >=8.11.x <=9
yarn install
This app uses Redux Thunk middleware and Redux Persist for a persistent store.
Navigation is handled with React Native Navigation v2. Check out src/navigators/index.js
for the root of the application.
To run tslint on the application:
yarn lint
To fix most tslint issues automatically
yarn lint:fix
Unit tests are under __test__
directory at root.
To run unit test on the application:
npm run test
To find unit test coverage for the application:
npm run test:coverage
-
Add folder with screen name, ex.
MyScreen/
insrc/view/screens
-
Follow the convention structure:
MyScreen/ Component.tsx // React Native component Index.tsx // Redux - Link action creators, map state to props Styles.tsx // Styles
-
-
Add component to constants list (centralized for good practice) in
src/constants/screen.tsx
-
Register screen in
src/view/screens/index.tsx
import * as MyScreen from './MyScreen'; ... export function registerScreens(redux: any) { registerComponentWithRedux(redux)(SCREENS.MyScreen, MyScreen.default); }
-
Create and export navigation function in
src/navigators/navigation.tsx
export const goToMyScreen = () => { Navigation.setRoot({ root: { component: { name: SCREENS.MyScreen }, }, }); };
-
Now you can travel to your screen from any component by importing and calling
goToMyScreen()
!
Note: Place modal screens in src/view/screens/Modals
-
Create your new reducer in
shared/redux/reducers
-
Add action types to
ACTION_TYPES
, make and export action creators inshared/redux/actions/index.tsx
-
Link up redux to your screen
- In
MyScreen/index.tsx
, define the state you want to use inmapStateToProps
, and actions to dispatch inactionCreators
import { connect } from 'react-redux'; import Component from './Component'; import { myAction } from '../../../../shared/redux/actions/index'; const mapStateToProps = state => { return { myState: ... }; }; const actionCreators = { myAction }; const screenContainer = connect( mapStateToProps, actionCreators )(Component); export default screenContainer;
- In