ARCHITECTURE

Architecture

:warning: WORK IN PROGRESS

The Starter Kit architecture is designed to support scalable, modularised applications. Built around Redux, it makes it simple to reason about your application's state, and as a result to write maintainable, error-free programs.

The downside of the Starter Kit architecture is that it involves a number of novel concepts, which might take a while to grok for the uninitiated. This document aims to explain the what, why and how of building apps the Starter Kit way.

Pieces of the puzzle

The application state and state changes are managed by Redux, a library that implements a pure, side-effect-free variant of the Facebook Flux architecture. Redux and Flux prescribe a unidirectional dataflow through your application. To understand Redux, check out this Cartoon guide by Lin Clark (it's great, not a joke!) and Dan Abramov's Redux course on egghead.io.

Redux helps us with synchronous updating of our state, but it doesn't provide an out-of-the-box solution for handling asynchronous actions. The Redux ecosystem has many possible solutions for this problem. In our application, we use the vanilla redux-thunk middleware for simple asynchronous actions, and redux-loop to handle more complex asynchronicity.

The state in Redux applications should never be mutated, but always cloned. To make this more natural for the programmer, and more fault-tolerant against accidental mutation, we use ImmutableJS data structures to hold our app's state.

Organising code

Let's take a look of how we organise our application.

Components

The components directory should contain React Native JSX components, which take their inputs in as props. In Flux/Redux parlance the components should be dumb/presentation components, meaning that components should not be connect()ed to the redux store directly, but instead used by smart/container components.

The components may be stateful if it makes sense, but do consider externalising state to the Redux store instead. If the state needs to be persisted, shared by other components, or inspected by a developer in order to understand the program state, it should go in the Redux store.

A component may be either written as a React.createClass constructor or as a plain JavaScript function component. Usage of ES6 class Foo extends React.Component should be generally speaking avoided.

If a component implementation differs between iOS and Android versions of the application, create separate .android.js and .ios.js files for the component. In minor cases the React.Platform.OS property can be used to branch between platforms.

Modules

The modules directory contains most of the interesting bits of the application. As a rule of thumb, this is where all code that modifies that application state or reads it from the store should go.

Each module is its own directory and represents a "discrete domain" within the application. There is no hard and fast rule on how to split your application into modules (in fact, this is one of the most difficult decisions in designing a Redux application), but here are some qualities of a good module:

  • Represents a screen in the application, or a collection of screens that form a feature.
  • Represents some technical feature that needs its own state (e.g. navigation).
  • Rarely needs to use data from other modules' states.
  • Doesn't contain data that is often needed by other modules.

Anatomy of a Module

At its simplest, a module contains three logical part: State, View(s) and Container(s). All of these are optional, i.e. a component may or may not a have a View. If a module consists only of a View, though, do consider making it a component instead.

State

The State encapsulates... err... well, the state of the application, and any actions that can modify that state. State can be data, for example fetched from a server or created by the user in-app, or it may be something transient, such as whether the user is logged into the application, or whether a particular UI element should be displayed or not.

The State part of the module is a Redux Duck - a file that contains a Reducer, Action Creators and the initial state of the application.

Let's take a simple example of an application that displays a number, which the user can increment by pressing a plus button, and decrement using a minus button.

// CounterState.js
import {Map} from 'immutable';

// INITIAL STATE
//
// We start by defining the initial state for this module. In most cases your
// module state will be an Immutable.Map. Even if your data is represented as a
// list, set or a primitive value, it's usually best to wrap it in a Map for
// maximum flexibility when refactoring your state

const initialState = Map({
  value: 0
});

// ACTION TYPES (Naming: SCREAMING_CASE)
//
// Let's define constants for the action types. The action types must be globally unique,
// so we namespace them with a prefix to avoid accidental collisions. It also helps to make
// the action name descriptive, as it helps with debugging. In most cases the action constants
// will be private to the State file, but in some advanced scenarios may be exported

const UPDATE_NUMBER = 'CounterState/UPDATE_NUMBER';

// ACTION CREATORS (Naming: camelCase)
//
// Action creators are functions whose responsibility is to encapsulate the creation of the
// messages passed to the reducer. Their API should be consumer-friendly and hide as much of
// the internal implementation of the state update as possible.
//
// At their simplest Action creators just construct a Flux Standard Action -compliant action.
// Other times they may call asynchronous services and rely on a Redux middleware.
//
// Action creators are always named exports, `export function name() {...}`, or `export const name = ...`

export function increment() {
  return {type: UPDATE_NUMBER, payload: +1};
}

export function decrement() {
  return {type: UPDATE_NUMBER, payload: -1};
}

// REDUCER (Naming: PascalCase)
//
// Reducer is responsible for handling all the actions defined in this module. The first
// parameter is the previous state of this module, and should default to the initial state.
//
// The reducer then examines the `action` object and decides whether any state should change in
// response to that action. The reducer must return the updated state, or if no changes are made,
// the previous state without modifications.
//
// The reducer is always an ES6 default export.

export default function CounterStateReducer(state = initialState, action) {
  switch (action.type) {
    case UPDATE_NUMBER:
      return state.update('value', value => value + action.payload);
    default:
      return state;
  }
}

The Redux Ducks pattern aims to keep the code portable, contained and easy to refactor by co-locating the reducer with action creators. For complex modules, the Duck can get quite long and make it difficult to maintain, in which case it should be split into smaller chunks, either by separating the reducer into its own file or by splitting the state into smaller Ducks and combining the reducers using standard Redux split/combine strategies.

View

Typically the View represents the screen in the application. A module may have multiple views, if the part of the application consists of multiple screens, or if the single view is too complex to write in a single file.

Technically speaking the View is identical to a component we define in the components directory. The difference is the way we use them. Ideally, the View's role is to orchestrate reusable components. The view can be aware of what the application state looks like and which actions update it, whereas a component should not dispatch things directly, and have their props API designed around the purpose of the component, not the state of the application.

The View usually has some presentational components and styling, but usually the leaner the view the better. If a view implementation needs to be very different on iOS and Android, separate .android.js and ios.js files may be written. However, for maintainability purposes, it is better if the platform-specific implementation can be done on component level, and the View can remain platform-agnostic.

A View should take all inputs as props, and should very, very rarely, if ever, be stateful. Instead, the state should be managed in Redux, and injected to the component props by the container.

To continue the Counter example, a view might look something like this:

import React, {PropTypes, StyleSheet, Text, View} from 'react-native';
import ActionButton from '../../components/ActionButton';
import * as CounterState from './CounterState';

const CounterView = React.createClass({
  // state (value) and action dispatcher are provided as props
  propTypes: {
    value: PropTypes.number.isRequired,
    dispatch: PropTypes.func.isRequired
  },

  render() {
    const {value, dispatch} = this.props;
    // use reusable components (ActionButton) to dispatch actions created by CounterState action creators
    return (
      <View style={styles.container}>
        <Text style={styles.counter}>{value}</Text>
        <ActionButton onPress={() => dispatch(CounterState.increment())} text='+' />
        <ActionButton onPress={() => dispatch(CounterState.decrement())} text='-' />
      </View>
    );
  }
});

// styles are defined inline
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'white'
  },
  counter: {
    textAlign: 'center',
    fontSize: 40
  }
});

export default CounterView;
Container

The Container (or View Container) is responsible for connect()ing the View component to the Redux store.

Redux connect() takes in two arguments, first mapStateToProps which selects relevant parts of the application state to pass to the view, and second mapActionsToProps, which binds Action Creators to the store's dispatcher so the actions are executed in the right context. These functions are often called selectors.

We think using mapStateToProps is a good practice, but avoid using mapActionsToProps in favour of calling dispatch ourselves in the view. In our experience this leads to simpler, easier to reason about code (and a little less verbose PropTypes on the View).

Every time the app state changes, the Container is automatically called with the latest state. If the props returned by the container differ from the previous props, the connected View is re-rendered. If the props are identical, the view is not re-rendered. For this reason it's a good idea to define your props as ImmutableJS data structures or JavaScript primitives, because if you toJS() your immutable Maps and Lists to objects and arrays in the Container, the results of each pass are not referentially equal, and we lose the benefit of this performance optimisation.

Using the Counter example, the container would be very simple:

import {connect} from 'react-redux';
import CounterView from './CounterView';

// pass the counter's value to the component as a prop called `value`.
// Because we omit the second parameter, the `dispatch` function is
// automatically passed as a prop.
export default connect(
  state => ({
    value: state.getIn(['counter', 'value'])
  })
)(CounterView);

Often this file doesn't contain a lot of code, but it's important to define the Container in its own file anyway to be able to support platform-specific view implementations, as well as test the Views and their data bindings separately.

If a View needs data from other modules (i.e. other parts of the application state than the subtree managed by that module), the Container is the correct place to access. In database-speak, this way you can keep your data "normalized" (to a degree), and "join" them when required.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容