Javascript 在react native中使用两个构造函数
我在react native中有一个项目,在我的项目中有列表视图和模式,列表视图需要构造函数,模式需要构造函数当我尝试使用两个构造函数时,我遇到了错误,我如何解决这个问题?请帮忙Javascript 在react native中使用两个构造函数,javascript,android,ios,react-native,Javascript,Android,Ios,React Native,我在react native中有一个项目,在我的项目中有列表视图和模式,列表视图需要构造函数,模式需要构造函数当我尝试使用两个构造函数时,我遇到了错误,我如何解决这个问题?请帮忙 import React, { Component } from 'react'; import { StyleSheet, ToolbarAndroid ,AppRegistry, View, Text, TouchableHighlight, Mo
import React, { Component } from 'react';
import {
StyleSheet,
ToolbarAndroid
,AppRegistry,
View,
Text,
TouchableHighlight,
Modal,
TextInput,
ListView,ActionButton,
Image,
Alert
} from 'react-native';
var DialogAndroid = require('react-native-dialogs');
export default class HygexListView extends Component {
constructor() {
super(...arguments);
this.state = {
visible: false
};
}
constructor(props){
super(props);
var ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 != r2
});
this.state = {
ds:[{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"}],
dataSource:ds,
}
}
componentDidMount(){
this.setState({
dataSource:this.state.dataSource.cloneWithRows(this.state.ds),
})
}
pressRow(rowData){
var newDs = [];
newDs = this.state.ds;
newDs[0].Selection = newDs[0] == "CustomerName" ? "CustomerPhone" : "";
this.setState({
dataSource: this.state.dataSource.cloneWithRows(newDs)
})
showModal = () => {
this.setState({
visible: true
});
};
hideModal = () => {
this.setState({
visible: false
});
};
}
renderRow(rowData){
return (
<TouchableHighlight
onPress={()=> this.showModal()}
underlayColor = '#ddd'>
<View style ={styles.row}>
<Text style={{fontSize:19}}>{rowData.CustomerName} {"\n"} {rowData.CustomerPhone} </Text>
<View style={{flex:1}}>
<Text style={styles.selectionText}>{rowData[rowData.Selection]}</Text>
</View>
</View>
</TouchableHighlight>
)
}
render() {
return (
<View style={styles.container}>
<View style={styles.toolbar}>
<TouchableHighlight >
<Image style={styles.imagestyle}
source={require('./ic_search.png')}/>
</TouchableHighlight>
<Text style={styles.toolbarTitle}>CUSTOMERS</Text>
<TouchableHighlight onPress={() => this.moveToAddNewCustomer()}>
<Image style={styles.imagestyle}
source={require('./ic_action_name.png')} />
</TouchableHighlight>
</View>
<ListView
dataSource = {this.state.dataSource}
renderRow = {this.renderRow.bind(this)}>
</ListView>
<View style={styles.x}>
<TouchableHighlight style={styles.action1}>
<Text style={styles.actionText}>CUSTOMERS</Text>
</TouchableHighlight>
<TouchableHighlight style={styles.action1}
onPress={() => this.moveToOrderEntry()}>
<Text style={styles.actionText}>Order Entry</Text>
</TouchableHighlight>
<TouchableHighlight style={styles.action}
onPress={() => this.moveToMyOredre()}>
<Text style={styles.actionTex1}>My Order</Text>
</TouchableHighlight>
</View>
<Modal
visible={this.state.visible}
>
<View style={styles.modalContainer}>
<View style={styles.toolbar}>
<View>
<Text style={styles.toolbarTitle}>X</Text>
</View>
<Text style={styles.toolbarTitle}>Details</Text>
</View>
<View style={styles.ButtonflexDirection}>
<Text >CUSTOMER Name</Text>
</View>
<View style={styles.ButtonflexDirection}>
<Text >Address</Text>
</View>
<View style={styles.ButtonflexDirection}>
<Text >Phone Number</Text>
</View>
<View style={styles.ButtonflexDirection}>
<Text >Interested Product</Text>
</View>
<View style={styles.ButtonflexDirection}>
<Text >Discount</Text>
</View>
<View >
<TouchableHighlight style={styles.button}
onPress={() => this.moveToHygexListView()}>
<Text style={styles.buttonText}> ADD ORDER</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
</View>
)
}
moveToMyOredre() {
this.props.navigator.push({
id: 'MyOrder'
})
}
moveToOrderEntry() {
this.props.navigator.push({
id: 'OrderEntry'
})
}
moveToAddNewCustomer() {
this.props.navigator.push({
id: 'AddNewCustomer'
})
}
}
import React,{Component}来自'React';
导入{
样式表,
工具栏安卓
,评估学,
看法
文本,
触控高光,
情态动词
文本输入,
ListView、ActionButton、,
形象,,
警觉的
}从“反应本机”;
var DialogAndroid=require('react-native-dialogs');
导出默认类HyExListView扩展组件{
构造函数(){
超级(…参数);
此.state={
可见:假
};
}
建造师(道具){
超级(道具);
var ds=new ListView.DataSource({
行已更改:(r1,r2)=>r1!=r2
});
此.state={
ds:[{客户名称:“客户名称”,客户电话:+564659878964},{客户名称:“客户名称”,客户电话:+564659878964},{客户名称:“客户名称”,客户电话:+564659878964},{客户名称:“客户名称”,客户电话:+564659878964},{客户名称:“客户名称”,客户电话:+564659878964},{客户名称:“客户名称”,客户电话:“+564659878964”},{CustomerName:“客户名称”,客户电话:“+564659878964”},{CustomerName:“客户名称”,客户电话:“+564659878964”},{CustomerName:“客户名称”,客户电话:“+564659878964”},
数据源:ds,
}
}
componentDidMount(){
这是我的国家({
dataSource:this.state.dataSource.cloneWithRows(this.state.ds),
})
}
按ROW(行数据){
var newDs=[];
newDs=this.state.ds;
newDs[0]。Selection=newDs[0]=“CustomerName”?“CustomerPhone”:“;
这是我的国家({
dataSource:this.state.dataSource.cloneWithRows(newDs)
})
showModal=()=>{
这是我的国家({
可见:正确
});
};
hideModal=()=>{
这是我的国家({
可见:假
});
};
}
renderRow(行数据){
返回(
this.showModal()}
参考底色='#ddd'>
{rowData.CustomerName}{“\n”}{rowData.CustomerPhone}
{rowData[rowData.Selection]}
)
}
render(){
返回(
客户
this.moveToAddNewCustomer()}>
客户
this.moveToOrderEntry()}>
订单输入
this.moveToMyOredre()}>
我点的菜
X
细节
客户名称
地址
电话号码
感兴趣的产品
优惠
this.moveToHygexListView()}>
添加订单
)
}
moveToMyOredre(){
这个是.props.navigator.push({
id:'我的订单'
})
}
moveToOrderEntry(){
这个是.props.navigator.push({
id:'OrderEntry'
})
}
moveToAddNewCustomer(){
这个是.props.navigator.push({
id:'AddNewCustomer'
})
}
}
仅使用一个构造函数:
constructor(props){
super(props);
var ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 != r2
});
this.state = {
ds:[{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"},{CustomerName: "Customer Name", CustomerPhone: "+564659878964"}],
dataSource:ds,
visible: false,
}
}
ES6中没有这种类型的多态性,您的实例将始终在参数中接收
props
值,即使您没有在函数声明中写入任何参数,因此,您不能有两个构造函数。您说视图和模式需要一个构造函数。这到底意味着什么?这是我的代码检查它,当我运行代码时,我遇到错误,“重复使用构造函数”!!如何解决它?你不能有两个构造函数。将两者的逻辑合并为一个,它应该可以工作。如何使它?你能合并它吗?我的代码在这里!!!