Git Product home page Git Product logo

react-native-flux-doc's Introduction

Android集成 react-native-flux 组件的知识点

 我们大量参考了官方文档,但是官方文档中并没有写明怎样去分发后台请求来的数据,也就是缺少action组件,当然不排除我没有找到相对应的文档。这个文档主要是对刚才的官方文档做的一个扩充。

####目录结构

首先看一下我们的目录结构,部分代码没有粘贴出来,是因为和官方文档有重复,比如程序的入口文件index.android.js

  ├── action.js
  ├── actions
  │   └── getCartData.js
  ├── page
  │   ├── Auth
  │   ├── Cart
  │   │   ├── Edit.js
  │   │   └── Index.js
  │   └── Home
  │       └── Index.js
  ├── reducers
  │   ├── cart.js
  │   ├── index.js
  │   └── routes.js
  1. action.js只是定义了一些action的常量信息,供actions文件夹里的获取数据进行标示和reducers文件夹里的reducer进行数据关联

actions/getCartData.js

  import * as types from '../action';

  export function getCartData(){
      // request server
      return {
          type : types.GET_CART_DATA,
          data : {
              name : 'Hello World'
          }
      }
  }
  export function updateCartData(_name){
      return {
          type : types.GET_CART_DATA,
          data : {
              name : _name
          }
      }
  }

reducers/cart.js

    import { ActionConst } from 'react-native-router-flux';
    import * as types from '../action';
    const initialState = {
        cartData: {},
    };

    export default function reducer(state = initialState, action = {}) {
        switch (action.type) {
            case types.GET_CART_DATA:
                return{
                    ...state,
                    cartData : action.data,
                };    
            default:
                return state;
        }
    }

2.page文件夹中相当于view层,所有页面渲染都在当前,Index.js

    import React, { Component, PropTypes } from 'react';
    import { Text, View,Button } from 'react-native';
    import {getCartData,updateCartData} from '../../actions/getCartData';
    import { connect } from 'react-redux';
    import {
        Actions,
    } from 'react-native-router-flux';
    class Cart extends Component {
      static propTypes = {
          routes: PropTypes.object,
          dispatch : PropTypes.func,
      };
      static contextTypes = {
          routes: PropTypes.object.isRequired,
      };
      componentDidMount(){
          this.props.dispatch(getCartData());


          //TODO:实验是否可以异步更新数据
          RestApi.doGet("productindex"
              ,{}
              ,(code, message) => {
                  //Actions
                  console.log('helo')
                  this.props.dispatch(updateCartData("fangfang"))

              }
          );

      }
      render() {
          console.log(this.props,'this is index props')
          const { routes } = this.context;
          //console.log(routes,'this is refresh')
          //var name = this.props.routes ? this.props.routes.scene.name : '';
          var requireRefresh = this.props.routes.scene.requireRefresh;
          return (
              <View>
                    <Text>
                          The current scene is titled {this.props.routes.scene.title}.
                      The current scene is titled {this.props.routes.scene.title}.
                      The current scene is titled {this.props.routes.scene.title}.
                      The current scene is titled {this.props.cart.cartData.name}.
                  </Text>
                  <Button onPress={() => routes.cartEdit({ "data": "hello" })} title="编辑"/>    
                      <Text>{requireRefresh ? 'refresh' : ''}</Text>
              </View>

          );
      }
  }
  //这里要注意最后导出组件的时候,用```connect```方法将```reducer```中的数据进行关联
  export default connect(({ routes,cart }) => ({ routes,cart }))(Cart);

3.reducer文件夹中的文件说明。

  import { combineReducers } from 'redux';
  import routes from './routes';
  // ... other reducers
  import cart from './cart';

  export default combineReducers({
    routes,
    // ... other reducers
    cart, //这里每定义新的actions中,都要在这里声明
  });


  //cart.js
  import { ActionConst } from 'react-native-router-flux';
  import * as types from '../action';
  const initialState = {
      cartData: {},
  };

  export default function reducer(state = initialState, action = {}) {
      switch (action.type) {
          case types.GET_CART_DATA:
              //console.log(action.data,'this is actions in cart.js   ==== = = == = = == = =')
              return{
                  ...state,
                  cartData : action.data,
              };    
          default:
              return state;
      }
  }

 

react-native-flux-doc's People

Contributors

manbali avatar weishiji avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

anddygon weishiji

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.