react native 组件间通信 局部刷新
来源:互联网 发布:存储管理系统编程 编辑:程序博客网 时间:2024/06/10 09:47
组件化是react-native的最大优点之一,因为组件化我们可以做到局部刷新,提高性能。
如图所示 父组件包含连个子组件 添加可以动态增加子组件
log输出:
add关键代码:
_onPress() { if(show){/*显示删除时 不能添加*/ return ; } let arr = this.state.list; arr.push(this._renderItem()); this.setState({ list: arr, }); }; _renderItem() { count = this.id+1; this.id = count; return (<CeilItem ref={(component)=>{ this.reflist.push(component) }} id={count} onChildHanlder={this.onChildHanlder} > </CeilItem> ); }
点击show动态更新子组件 但不刷新父组件
如下所示:
log:
只刷新了子view 没有刷新父view
show关键代码:
父组件:
/*改变item布局*/ _show(){ show = show?false:true; for(let i=0;i<this.reflist.length;i++){ this.reflist[i].change(show); } } onChildHanlder(inputId){ let len = this.reflist.length; for(let i=0;i<len;i++){ let id=this.reflist[i].props.id; if(inputId==id){ del++; console.log("delete i: ",i); this.reflist[i].delete(); } } /*全部删除后才可以添加*/ show = del==count?false:show; }
子组件代码:
change(flag){ this.setState({flag:flag}); } delete(){ this.setState({delete:true}); } hide(){ let centerPart_right = 0; let rightPart_width = 0; return (<View style={styles.Item}> <View style={styles.leftPart}> </View> <View style={[styles.centerPart, {marginRight:centerPart_right}]}> </View> <View style={[styles.rightPart,{width:rightPart_width,position:'absolute', right:0}]}> </View> </View>); } show(){ let id=this.props.id; let onChildHanlder=this.props.onChildHanlder; let centerPart_right = 70; let rightPart_width = 60; return (<View style={styles.Item}> <View style={styles.leftPart}> </View> <View style={[styles.centerPart, {marginRight:centerPart_right}]}> </View> <View style={[styles.rightPart,{width:rightPart_width,position:'absolute', right:0},{justifyContent:'center', alignItems:'center'}]}> <TouchableOpacity onPress={()=>{onChildHanlder(id)}}> <Text style={[styles.textStyle]}> del </Text> </TouchableOpacity> </View> </View>); }
github完整代码:https://github.com/wuyunqiang/react-/tree/master/test/view
0 0
- react native 组件间通信 局部刷新
- react native组件通信以及
- react native组件通信方式
- react/react native 组件的刷新机制
- 《React-Native系列》30、 RN组件间通信
- 《React-Native系列》30、 RN组件间通信
- react组件间通信
- React组件间通信
- React -- 组件间通信
- React 组件间通信
- react-native 与 native组件之间的通信
- React Native控件之PullToRefreshViewAndroid下拉刷新组件讲解(20)
- React Native下拉刷新组件 ios 平台 支持ScrollView,ListView
- react-native纯JS上下拉刷新组件
- react-native之上拉加载,下拉刷新组件封装
- 这是一个实现下拉刷新的react native组件 react-native-pullview
- React-Native学习十九:组件之间的通信-1
- React-Native学习十九:组件之间的通信-1
- 屡试不爽的“3个”iPad使用技巧
- Unity中的Path对应各平台中的Path
- JSONP
- 强大的jquery颜色选择器gradX.js
- Android的Canvas各函数全解析
- react native 组件间通信 局部刷新
- 401. Binary Watch
- Android PercentRelativeLayout
- Android6.0权限管理
- Servlet生命周期
- JS对象与访问器属性
- Codeforces643A【一种暴力】
- day14 pickle只支持字节方式 读取和写入
- Unity Shaders and Effects Cookbook (D-2) Cull Back背面剔除 -- 模型半边不可见