Reactive Native学习

2017-05-24  本文已影响0人  jayhe

开发环境的搭建

参照官方文档按照步骤来,开发工具选择的是WebStorm 环境搭建

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'

基础语法

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(props) {
  super(props);
  this.state = {
    color: props.initialColor
  };
}
Updating

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.
Unmounting
待补充
FlexBox布局
flexDirection
alignItems
justifyContent
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,
};
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 () {
       //解析数据
       const {params} = this.props.navigation.state;
       this.setState({
          userName: params.user
       });
   }
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,在跳转到的界面回到该界面的时候执行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
上一篇 下一篇

猜你喜欢

热点阅读