gpt4 book ai didi

reactjs - 如何隐藏和显示加载微调器 - 事件指示器 native react ,管理 Prop 和状态

转载 作者:行者123 更新时间:2023-12-03 13:34:38 24 4
gpt4 key购买 nike

我创建了一个自定义事件指示器类,我想从使用它的位置控制它的隐藏/显示。

这是我所做的。

自定义事件指示器.js

    import React, { Component } from 'react';
import { ActivityIndicator, View, Text, TouchableOpacity, StyleSheet, Dimensions } from 'react-native';
import colors from '../../../styles/colors';
import { consoleLog } from '../../../utils/globalFunctions';

const { width, height } = Dimensions.get('window');

export default class CustomActivityIndicator extends Component {

constructor(props){
super(props);
this.state = {
show: this.props.show
}
}

static getDerivedStateFromProps(nextProps, prevState) {

let outObj = {};
consoleLog('Login - nextProps.show - ' + nextProps.show + ' prevState.show - ' + prevState.show);
if(nextProps.show !== prevState.show) {
return {
show: nextProps.show
};
}
}

render() {
consoleLog('CustomActivityIndicator - ' + this.state.show );
return (
<View style={styles.container}>
<ActivityIndicator
animating = {this.state.show}
color = {colors.primaryColor}
size = "large"/>
</View>
);
}
}

const styles = StyleSheet.create ({
container: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
justifyContent: 'center',
alignItems: 'center'
}
})

我在登录中使用这只是为了演示

我最初在登录中将show状态设置为false,当我单击Login按钮时,我想要显示 ActivityIndi​​cator

你能指导我哪里出错了吗?

登录.js

 class Login extends React.Component {

constructor(props) {
super(props);
this.state = {
show: false
};
}


loginEndpointDecider = () => {
this.setState({show: true}) ;
}

render() {
return (
<ScrollView style={styles.mainContainer}>
<CustomActivityIndicator show={this.state.show}/>
<TouchableOpacity title='Transactions'
style = {{ height: 60, backgroundColor: '#673fb4', marginTop: 20, alignItems: 'center', justifyContent: 'center' }}
onPress={() => {
this.loginEndpointDecider();
}}>
<CommonText style={{ color: 'white'}}>
{strings.signInLower}
</CommonText>
</TouchableOpacity>
</ScrollView>
);
}
}

谢谢

最佳答案

“React 思维方式”中更好的方法是能够有条件地渲染灵活的组件,而不是将所有 props 都放在实际组件本身中。这意味着在您的 Login.js 文件中,您可以使用状态在 render 方法中显示某些内容。

class Login extends React.Component {



constructor(props) {
super(props);
this.state = {
show: false
};
}


loginEndpointDecider = () => {
this.setState({show: true}) ;
}

render() {
return (
<ScrollView style={styles.mainContainer}>
{this.state.show ? <CustomActivityIndicator/> : "not shown"}
<TouchableOpacity title='Transactions'
style = {{ height: 60, backgroundColor: '#673fb4', marginTop: 20, alignItems: 'center', justifyContent: 'center' }}
onPress={() => {
this.loginEndpointDecider();
}}>
<CommonText style={{ color: 'white'}}>
{strings.signInLower}
</CommonText>
</TouchableOpacity>
</ScrollView>
);
}
}

{this.state.show ? <CustomActivityIndicator/> : "not shown"}是 if 语句的简写。

关于reactjs - 如何隐藏和显示加载微调器 - 事件指示器 native react ,管理 Prop 和状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52668835/

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