gpt4 book ai didi

javascript - 根据许可更改按钮状态 react native

转载 作者:行者123 更新时间:2023-11-30 19:28:11 27 4
gpt4 key购买 nike

我在这里显示三个按钮发票、充值和付款。默认情况下,当我来到这个页面时,“发票”按钮被选中,其余两个按钮被禁用。但是,如果我通过许可隐藏此“发票”按钮,它应该会自动切换到“充值”选项卡并且充值应该处于事件状态。请建议。

export default class Financial extends Component {
constructor(props){
super(props)
this.state ={
activeTab: 'invoices'
}
}

render(){
const { activeTab } = this.state;
const { customer,rechargeDeatails,navigation,permission } = this.props;
console.log("permission-------",permission);
return (
<View
style={{
flex: 1,
paddingLeft: 10,
paddingRight: 10,
backgroundColor: '#f1f1f1',
flexDirection: 'column'
}}
>
<View style={{flexDirection:'row', padding: 10}}>
{permission.invoiceTab &&
<View style={{marginRight: 5}}>
<TouchableOpacity onPress={()=>this.setState({activeTab:'invoices'})}>
<Button small rounded disabled={activeTab!=='invoices'} style={{padding:2, height: 28}}>
<Text style={styles.btnLabel}> Invoices </Text>
</Button>
</TouchableOpacity>
</View>}
<View style={{marginRight: 5}}>
<TouchableOpacity onPress={()=>this.setState({activeTab:'recharges'})}>
<Button small rounded disabled={activeTab!=='recharges'} style={{padding:2, height: 28}}>
<Text style={styles.btnLabel}> Recharges </Text>
</Button>
</TouchableOpacity>
</View>
<View style={{marginRight: 5}}>
<TouchableOpacity onPress={()=>this.setState({activeTab:'payments'})}>
<Button small rounded disabled={activeTab!=='payments'} style={{padding:2, height: 28}}>
<Text style={styles.btnLabel}> Payments </Text>
</Button>
</TouchableOpacity>
</View>
</View>
<View style={{flexDirection:'column'}}>
{ activeTab=='recharges' && <Recharges customer={customer} rechargeDeatails={rechargeDeatails}/>}
{ activeTab=='invoices' && <Invoices navigation={navigation}/>}
{ activeTab=='payments' && <Payments/>}
</View>
</View>
);
}
}

最佳答案

好吧,如果你根据 Prop 设置 activeTab 状态,你可以在构造函数中检查你是否有 permission.invoiceTab 然后将 activeTab 设置为 invoices else to recharges

喜欢:

export default class Financial extends Component {
constructor(props) {
super(props)
const { permission } = this.props
let activeTab = 'invoices'
if (permission.invoiceTab) {
activeTab = 'invoices'
} else if (permission.rechargeTab) {
activeTab = 'recharges'
} else {
activeTab = 'payments'
}
this.state = {
activeTab,
}
}

render() {
const { activeTab } = this.state;
const { customer, rechargeDeatails, navigation, permission } = this.props;
console.log("permission-------", permission);
return (
<View
style={{
flex: 1,
paddingLeft: 10,
paddingRight: 10,
backgroundColor: '#f1f1f1',
flexDirection: 'column'
}}
>
<View style={{ flexDirection: 'row', padding: 10 }}>
{permission.invoiceTab &&
<View style={{ marginRight: 5 }}>
<TouchableOpacity onPress={() => this.setState({ activeTab: 'invoices' })}>
<Button small rounded disabled={activeTab !== 'invoices'} style={{ padding: 2, height: 28 }}>
<Text style={styles.btnLabel}> Invoices </Text>
</Button>
</TouchableOpacity>
</View>}
<View style={{ marginRight: 5 }}>
<TouchableOpacity onPress={() => this.setState({ activeTab: 'recharges' })}>
<Button small rounded disabled={activeTab !== 'recharges'} style={{ padding: 2, height: 28 }}>
<Text style={styles.btnLabel}> Recharges </Text>
</Button>
</TouchableOpacity>
</View>
<View style={{ marginRight: 5 }}>
<TouchableOpacity onPress={() => this.setState({ activeTab: 'payments' })}>
<Button small rounded disabled={activeTab !== 'payments'} style={{ padding: 2, height: 28 }}>
<Text style={styles.btnLabel}> Payments </Text>
</Button>
</TouchableOpacity>
</View>
</View>
<View style={{ flexDirection: 'column' }}>
{activeTab == 'recharges' && <Recharges customer={customer} rechargeDeatails={rechargeDeatails} />}
{activeTab == 'invoices' && <Invoices navigation={navigation} />}
{activeTab == 'payments' && <Payments />}
</View>
</View>
);
}
}

关于javascript - 根据许可更改按钮状态 react native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56697844/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com