React Native 的子组件向父组件传值的简单例子
2017-04-24 15:03
288 查看
目前只是找到了一组传值的方法
基本原理是利用RN自带的TouchableOpacity组件中的callbackParent属性来实现
上代码
父组件代码
/**
* @flow
*/
/* @flow */
//只适用于一组图片的轮播效果
/*import {
Image,
StatusBar,
StyleSheet,
Text,
TouchableWithoutFeedback,
View,
ScrollView,
AppRegistry
} from 'react-native';
import React, { Component } from 'react';
import Index from './app/compotent/article/Index.js';
export default class App extends Component {
render() {
return (
<ScrollView>
<Index/>
</ScrollView>
);
}
}*/
/**
* Created by mymac on 2017/4/1.
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
View,
Text,
ListView,
TouchableHighlight,
} from 'react-nati
4000
ve';
import ListViewItem from './ListViewItem_Test.js'
export default class App extends Component {
constructor(props) {
super(props);
//其中提供给数据源的rowHasChanged函数可以告诉ListView它是否需要重绘一行数据
// 即数据是否发生了改变,即在需要重绘界面的时候会进行判断
// 如果之前页面中的改行数据没有发生变化,则不再进行重绘,否则进行重绘
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
//然后通过cloneWithRows方法为其传递一个数组
dataSource: ds.cloneWithRows(this.getDataArray()),
ImageUrl:[1,],
};
//将this的指针转移到父组件
this.onChildChanged = this.onChildChanged.bind(this);
}
//初始化数组数据
getDataArray () {
var dataArray = [];
for (let i = 0 ; i<10 ; i ++) {
dataArray.push('row' + i);
}
return dataArray;
}
getImageUrl(){
let array=this.state.dataSource;
// array.push(newState);
console.log(array,'arry');
// this.setState({ duration: data.duration });
}
//子组件向父组件传值需要调用的方法
onChildChanged (newState) {
alert(newState);
// this.getImageUrl();
let array=this.state.ImageUrl;
array.push(newState);
console.log(array,'arry');
}
//相当于iOS中的cell,返回一个视图布局
_renderRow (rowData, sectionID,rowID){
return (
<ListViewItem rowData={rowData}
sectionID={sectionID}
rowID={rowID}
callbackParent={this.onChildChanged}/>
);
}
//点击cell触发的方法
selectCellAction(rowData, sectionID,rowID) {
alert('rowID:'+rowID);
}
_renderSeparator(rowData, sectionID, rowID, highlightRow) {
return(
<View style={{backgroundColor:'red',height:1}}></View>
)
}
render() {
return (
<ListView style={{flex:1}}
dataSource={this.state.dataSource}
renderRow={this._renderRow.bind(this)}
renderSeparator={this._renderSeparator.bind(this)}
/>
);
}
}
AppRegistry.registerComponent('App', () => App);
子组件代码
/**
* Created by mymac on 2017/4/5.
*/
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
} from 'react-native';
export default class ListViewItem_Test extends Component {
setNativeProps(nativeProps) {
this.refs._root.setNativeProps(nativeProps);
}
render() {
const {rowData,sectionID,rowID} = this.props;
return (
<TouchableOpacity ref="_root"
style={ListViewItem_TestStyle.itemStyle}
onPress={() => {
this.props.callbackParent(this.props.rowData);
}}>
<Text>{'rowdata:'+this.props.rowData}</Text>
<Text>{'sectionID:'+this.props.sectionID}</Text>
<Text>{'rowID:'+this.props.rowID}</Text>
</TouchableOpacity>
);
}
}
let ListViewItem_TestStyle =StyleSheet.create({
itemStyle:{
height:60,
}
})
基本原理是利用RN自带的TouchableOpacity组件中的callbackParent属性来实现
上代码
父组件代码
/**
* @flow
*/
/* @flow */
//只适用于一组图片的轮播效果
/*import {
Image,
StatusBar,
StyleSheet,
Text,
TouchableWithoutFeedback,
View,
ScrollView,
AppRegistry
} from 'react-native';
import React, { Component } from 'react';
import Index from './app/compotent/article/Index.js';
export default class App extends Component {
render() {
return (
<ScrollView>
<Index/>
</ScrollView>
);
}
}*/
/**
* Created by mymac on 2017/4/1.
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
View,
Text,
ListView,
TouchableHighlight,
} from 'react-nati
4000
ve';
import ListViewItem from './ListViewItem_Test.js'
export default class App extends Component {
constructor(props) {
super(props);
//其中提供给数据源的rowHasChanged函数可以告诉ListView它是否需要重绘一行数据
// 即数据是否发生了改变,即在需要重绘界面的时候会进行判断
// 如果之前页面中的改行数据没有发生变化,则不再进行重绘,否则进行重绘
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
//然后通过cloneWithRows方法为其传递一个数组
dataSource: ds.cloneWithRows(this.getDataArray()),
ImageUrl:[1,],
};
//将this的指针转移到父组件
this.onChildChanged = this.onChildChanged.bind(this);
}
//初始化数组数据
getDataArray () {
var dataArray = [];
for (let i = 0 ; i<10 ; i ++) {
dataArray.push('row' + i);
}
return dataArray;
}
getImageUrl(){
let array=this.state.dataSource;
// array.push(newState);
console.log(array,'arry');
// this.setState({ duration: data.duration });
}
//子组件向父组件传值需要调用的方法
onChildChanged (newState) {
alert(newState);
// this.getImageUrl();
let array=this.state.ImageUrl;
array.push(newState);
console.log(array,'arry');
}
//相当于iOS中的cell,返回一个视图布局
_renderRow (rowData, sectionID,rowID){
return (
<ListViewItem rowData={rowData}
sectionID={sectionID}
rowID={rowID}
callbackParent={this.onChildChanged}/>
);
}
//点击cell触发的方法
selectCellAction(rowData, sectionID,rowID) {
alert('rowID:'+rowID);
}
_renderSeparator(rowData, sectionID, rowID, highlightRow) {
return(
<View style={{backgroundColor:'red',height:1}}></View>
)
}
render() {
return (
<ListView style={{flex:1}}
dataSource={this.state.dataSource}
renderRow={this._renderRow.bind(this)}
renderSeparator={this._renderSeparator.bind(this)}
/>
);
}
}
AppRegistry.registerComponent('App', () => App);
子组件代码
/**
* Created by mymac on 2017/4/5.
*/
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
} from 'react-native';
export default class ListViewItem_Test extends Component {
setNativeProps(nativeProps) {
this.refs._root.setNativeProps(nativeProps);
}
render() {
const {rowData,sectionID,rowID} = this.props;
return (
<TouchableOpacity ref="_root"
style={ListViewItem_TestStyle.itemStyle}
onPress={() => {
this.props.callbackParent(this.props.rowData);
}}>
<Text>{'rowdata:'+this.props.rowData}</Text>
<Text>{'sectionID:'+this.props.sectionID}</Text>
<Text>{'rowID:'+this.props.rowID}</Text>
</TouchableOpacity>
);
}
}
let ListViewItem_TestStyle =StyleSheet.create({
itemStyle:{
height:60,
}
})
相关文章推荐
- 通用权限管理系统组件 (GPM - General Permissions Manager) 中最简单的例子程序
- ExtJS4中GridPanel组件简单例子
- react native简单组件初始化
- 【转】一个用Visual C#做组件的简单例子
- Android小例子:Service组件的简单使用
- 自定义 React Native 二维码扫描组件(简单,易用!)
- NanoXML组件解析xml简单例子
- nodejs模仿http请求组件nodegrass简单例子
- nodejs模仿http请求组件nodegrass简单例子
- React Native 二维码扫描组件(简单,易用!)
- react native 学习笔记-----理解redux的一个极其简单例子
- 通用权限管理系统组件 (GPM - General Permissions Manager) 中最简单的例子程序,如何时间通讯录管理
- 通用权限管理系统组件 (GPM - General Permissions Manager) 中最简单的例子程序,如何时间通讯录管理
- 用vb6写asp组件的简单例子
- 自定义 React Native 二维码扫描组件(简单,易用!)
- React Native中NavigatorIOS组件的简单使用详解
- ExtJS4中GridPanel组件简单例子
- React Native 定义组件(简单)
- 从一个简单的例子看UI多线程处理(基于eclipse的SWT组件)
- chrome组件开发--填表组件简单例子