Reactive Native學習

開發(fā)環(huán)境的搭建

參照官方文檔按照步驟來褐墅,開發(fā)工具選擇的是WebStorm 環(huán)境搭建

We want to share code on iOS and Android, so lets delete the contents of index.ios.js and index.android.js and replace it with import './App'

基礎(chǔ)語法

props 和 state

props:
this.props contains the props that were defined by the caller of this component
state:
The state contains data specific to this component that may change over time. The state is user-defined, and it should be a plain JavaScript object.
If you don't use it in render(), it shouldn't be on the state. For example, you can put timer IDs directly on the instance.
Never mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable.

component生命周期
Mounting

These methods are called when an instance of a component is being created and inserted into the DOM

  • constructor() The constructor for a React component is called before it is mounted
constructor(props) {
  super(props);
  this.state = {
    color: props.initialColor
  };
}
  • componentWillMount() is invoked immediately before mounting occurs. It is called before render()
  • componentDidMount() is invoked immediately after a component is mounted.
Updating
  • componentWillReceiveProps() is invoked before a mounted component receives new props.
  • shouldComponentUpdate() is invoked before rendering when new props or state are being received. Defaults to true
  • componentWillUpdate() is invoked immediately before rendering when new props or state are being received.

Note

  • you cannot call this.setState() here. If you need to update state in response to a prop change, use componentWillReceiveProps() instead.
  • componentWillUpdate() will not be invoked if shouldComponentUpdate() returns false.
  • componentDidUpdate is invoked immediately after updating occurs
Unmounting
  • componentWillUnmount() is invoked immediately before a component is unmounted and destroyed. Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any DOM elements that were created in componentDidMount
待補充
FlexBox布局
flexDirection
  • column: (默認值)豎直軸,子視圖上下排列
  • row :水平軸兄猩,姿勢圖水平排列
alignItems
  • flex-start
  • center
  • flex-end
  • stretch
justifyContent
  • flex-start
  • center
  • flex-end
  • space-around
  • space-between
React-Navigation

安裝:npm install --save react-navigation

StackNavigator
export type NavigationScreenProp<S, A> = {
  state: S,
  dispatch: NavigationDispatch<A>,
  goBack: (routeKey?: ?string) => boolean,
  navigate: (
    routeName: string,
    params?: NavigationParams,
    action?: NavigationAction,
  ) => boolean,
  setParams: (newParams: NavigationParams) => boolean,
};
  • navigate
    跳轉(zhuǎn)到某個界面曹阔,可以傳遞參數(shù)半开,也可以傳遞一個callback
  • goBack
    返回,出棧
  • params
    傳值到某個界面赃份,在該界面中可以通過this.props.navigation.state.params去獲得;
  • navigate的時候傳值
constructor (props) {
       super(props);
       //定義屬性
       this.state = {
          userName: '' 
       };
   };
   
   render() {
       const { params } = this.props.navigation.state;
       return (
           <View>
               <Text>Chat with {params.user}</Text>
               <Text style={{backgroundColor: 'red'}}>Chat with {this.state.userName}</Text>
           </View>
       );
   };

   componentDidMount () {
       //解析數(shù)據(jù)
       const {params} = this.props.navigation.state;
       this.setState({
          userName: params.user
       });
   }
  • goBack的時候回傳值
render() {
       const { navigate } = this.props.navigation;
       return(
           <View>
               <Text>{this.state.content}</Text>
               <Button
                   onPress={() => navigate('Chat', { user: 'HeChao', callback: (data) => {
                       //console.log(data);
                       this.setState({content: `從chat界面回傳的值 ${data}`});
                   }})}
                   title="Chat with HeChao"
               />
           </View>
       ); 
   }

傳遞一個callback寂拆,在跳轉(zhuǎn)到的界面回到該界面的時候執(zhí)行callback回傳值

static navigationOptions = ({ navigation }) => {
     const {params} = navigation.state;
     const {navigate,goBack,state} = navigation;
     navigation.goBack();
     return {
         title: `Chat with ${params.user}`,
         headerLeft : <Button title={'Back'} onPress={() => {
             if (state.params.callback) {
                 state.params.callback('hahahaha');
             }
             goBack();
         }} />
     };
 };
TabNavigator
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市抓韩,隨后出現(xiàn)的幾起案子纠永,更是在濱河造成了極大的恐慌,老刑警劉巖谒拴,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件尝江,死亡現(xiàn)場離奇詭異,居然都是意外死亡英上,警方通過查閱死者的電腦和手機炭序,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來苍日,“玉大人惭聂,你說我怎么就攤上這事∠嗍眩” “怎么了辜纲?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我耕腾,道長见剩,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任扫俺,我火速辦了婚禮苍苞,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘牵舵。我一直安慰自己柒啤,他們只是感情好,可當我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布畸颅。 她就那樣靜靜地躺著担巩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪没炒。 梳的紋絲不亂的頭發(fā)上涛癌,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天,我揣著相機與錄音送火,去河邊找鬼拳话。 笑死,一個胖子當著我的面吹牛种吸,可吹牛的內(nèi)容都是我干的弃衍。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼坚俗,長吁一口氣:“原來是場噩夢啊……” “哼镜盯!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起猖败,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤速缆,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后恩闻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體艺糜,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年幢尚,在試婚紗的時候發(fā)現(xiàn)自己被綠了破停。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡尉剩,死狀恐怖辱挥,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情边涕,我是刑警寧澤,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站功蜓,受9級特大地震影響园爷,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜式撼,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一童社、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧著隆,春花似錦扰楼、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至浦辨,卻和暖如春蹬竖,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背流酬。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工币厕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人芽腾。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓旦装,卻偏偏與公主長得像,于是被迫代替她去往敵國和親摊滔。 傳聞我的和親對象是個殘疾皇子阴绢,可洞房花燭夜當晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容