【呆萌の学习】分析一个“简单”的项目来学习react-native
前言突发情况,不得不快速入门学习react-native?, 结构新建src文件夹来存放页面内容和素材。 Root.js作用:用于react-navigation的配置。 react-navigation StackNavigator:用来跳转页面和传递参数 这里要实现首页底部的菜单,实现菜单内的页面之间的跳转,要用TabNavigator, import TabBarItem from './common/tabBarItem' import theme from './common/color' 这里还引入了tabBarItem和color, const TabBarItem = ({focused,tintColor,selectedImage,normalImage}) => { return ( <Image source = {focused ? selectedImage : normalImage} style={{ tintColor: tintColor,width: 25,height: 25 }} /> ) } 返回的是导航图标被选中和没被选中的一些改变设定,用于tabBarIcon自定义的设置。 common/viewpagerViewPager是一种滚动功能,也就是首页的轮播图了,这里的代码应该是直接引用过来的。 prop-types:对react组件中props对象中的变量进行类型检测(老版中直接可以从react里面引入PropTypes,后面独立出来成为了模块)。 react-timer-mixin:https://www.npmjs.com/package... 不熟悉的react-native组件/API: TouchableOpacity:https://reactnative.cn/docs/0... mobxmobx官网:http://cn.mobx.js.org/ observable,顾名思义,用于观察一个对象,当对象变化的时候,如果对象在autorun()中,就会执行对应的动作。 const plus = computed(() => number.get() > 0); 会返回true或者false,改变的时候,对象在autorun()中且结果变化才会执行。 mobx推荐将修改被观测变量的行为放在action中 项目中使用了ES7语法,所以要安装模块transform-decorators-legacy。 scene/Home
主页拥有一个轮播图,以及新品水果的呈现,所以肯定要引入轮播组件和新品水果的数据。 @inject('rootStore')来实现数据的获取,然后再一步步地把数据传到它们的子组件中。 再来看主类中的内容: static navigationOptions = { title: '迷你水果商城',headerTitleStyle: {alignSelf: 'center',fontSize: 15,color: theme.fontColor},headerStyle: {height: 38,backgroundColor: theme.color} }; 定义了页面头部的信息的样式内容,也是react-navigation中的(这里留个疑问,它怎么定位到头顶的,我如果想定位到其他地方要怎么改呢)。 NewGoodsItem.js: const NewGoodsItem = ({name,price,image,onPress}) => { return ( <TouchableOpacity onPress={() => onPress && onPress()}> <View style={styles.item}> <Image source={image} style={styles.image}/> <Text>{name}</Text> <Text>¥ {price}/500g</Text> </View> </TouchableOpacity> ) } 看代码看得出,你点击每个小块的时候,还会有一个透明度的变化。 <NewGoodsView itemDatas={data} navigation={this.props.navigation}/> 传入水果数据和导航给NewsGoodsView,这里this.props.navigation是react-navigation中带的: 在界面组件注入到StackNavigator中时,界面组件就被赋予了navigation属性,即在界面组件中可以通过【this.props.navigation】获取并进行一些操作。 在NewGoodsView相应写的是: <NewGoodsItem onPress={()=> props.navigation.navigate('ItemDetail',{value})} name={value.name} price={value.price} image={value.image} key={index}/> 能想到意思是,当点下新品的单个项目,页面导航会跳转到水果细则。 scene/Category分类页面 这个页面有一个子导航,以及下面对应的商品列表。 CategoryListView.js: scene/ItemDetail
这里引用了一个react-native-easy-toast模块:https://github.com/crazycodeb... toast是“吐司”的意思,它属于android杂项组件,是一个简单的消息提示框,类似于javascript中的alert 商品信息页面,除了水果的信息之外,还有一个加入购物车的操作。 this.state.bounceValue.setValue(1.5); Animated.spring( this.state.bounceValue,toValue: 1,friction: 1,} ).start(); 实现了购车图标蹭的一下变大又弹回去的动画。 MessageView.js: const List = ({ message }) => ( <View style={{ flex: 1 }}> <Text>{message}</Text> </View> ) scene/Cart
CartScreen.js: CartList.js: CartCheckOut.js: 以及react-native-loading-spinner-overlay模块:https://www.npmjs.com/package... <Spinner visible={this.state.visible} textContent={this.state.loadText} textStyle={{fontSize: 15,color: '#FFF'}} /> 等支付的时候才显示这个控件。 scene/Order & scene/MineOrder的情况类比原来几个页面,简单的数据获取就可以了。Mine里是比较普通的页面,不仔细探究了。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |