简体   繁体   English

在加载过程中显示文本

[英]Show texts during a loading

I have a question.我有个问题。 I have a loader and during the loading I would show three different texts.我有一个加载器,在加载过程中我会显示三个不同的文本。 Like text1, then this disappear and it's show text2 and then text3.像 text1 一样,然后它会消失,然后显示 text2,然后是 text3。

<View style={style.container}>
        <View style={style.page}>
          <ActivityIndicator size="large" color="#56cbbe" />
          <Text>Text1.. </Text> 
          <Text>Text2.. </Text> 
          <Text>Text3.. </Text>
        </View>
</View> 

In this case I only show the three texts together.在这种情况下,我只将三个文本一起显示。 How can I do?我能怎么做?

Thank you:)谢谢:)

What you want is to show indicator and text1 during loading time and then text2 and text3 .您想要的是在加载期间显示indicatortext1 ,然后显示text2text3 Is that right?那正确吗?

So I made an example for you.所以我给你做了一个例子。 This should solve the problem by changing the status value.这应该通过更改status值来解决问题。 You can display the indicator for the duration of loading and show the text by changing the status value when loading is complete.您可以显示加载持续时间的indicator ,并通过在加载完成时更改status值来显示文本。

gif

Example Code示例代码

//This is an example code to understand ActivityIndicator//

import React from 'react';
//import react in our code.

import { ActivityIndicator, Button, View, StyleSheet,Text } from 'react-native';
//import all the components we are going to use.

export default class App extends React.Component {
  state = { showIndicator: true };

  componentDidMount() {
     setTimeout(() => {this.setState({showIndicator: false})}, 2000)
  }

  onButtonPress = () => {
    //function to change the state to true to view activity indicator
    //changing state will re-render the view and indicator will appear
  };

  render() {
    //Check if showIndicator state is true the show indicator if not show button
    if (this.state.showIndicator) {
      return (
        <View style={styles.container}>
          {/*Code to show Activity Indicator*/}
          <ActivityIndicator size="large" color="#0000ff" />
                    <Text>Text1.. </Text> 
          {/*Size can be large/ small*/}
        </View>
      );
    } else {
      return (
        <View style={styles.container}>
           <Text>Text2.. </Text> 
          <Text>Text3.. </Text>
        </View>
      );
    }
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    flexDirection: 'column',
    alignItems: "center"
  },
});

One way to solve this is to use setInterval and call update function to loop through the texts assuming if they are present in form of array.解决这个问题的一种方法是使用 setInterval 并调用 update function 来循环遍历文本,假设它们是否以数组的形式存在。

Simply saying for example.简单说一下。

Let's maintain loadingText in state as loadingText: ['text1', 'text2', 'text3'] ,A variable to track the present item as currentLoadingTextIndex: 0 and call setInterval in componentDidUpdate .让我们将 state 中的 loadingText 维护为loadingText: ['text1', 'text2', 'text3'] ,用于跟踪当前项目的变量currentLoadingTextIndex: 0并在componentDidUpdate中调用 setInterval 。 Be careful when calling update function in here,as one wrong mistake will make your app crash.在这里调用更新 function 时要小心,因为一个错误的错误会导致您的应用程序崩溃。

componentDidUpdate(prevProps, prevState) {
    if (!prevState.isLoading && this.state.isLoading) {
      this.timerId = setInterval(this.changeLoadText, 2000);
    } else if (prevState.isLoading && !this.state.isLoading) {
      clearInterval(this.timerId);
    }
  }

and finally our update function最后是我们的更新 function

changeLoadText = () => {
    this.setState(prevState => {
      return {
        currentLoadingTextIndex:
          (prevState.currentLoadingTextIndex + 1) %
          prevState.loadingText.length,
      };
    });
  };

I am attaching a working expo Demo for clarity purpose.为了清楚起见,我附上了一个有效的 expo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM