【问题标题】:Is there any chance to use a component as a global ActivityIndicator on React-Native有没有机会在 React-Native 上使用组件作为全局 ActivityIndi​​cator
【发布时间】:2017-01-09 18:17:12
【问题描述】:

有没有机会将组件用作全局ActivityIndicator,它具有透明颜色并且是我在 React-Native 上创建的?

详情:

  • 我使用 redux 存储来更新 UI。所以我打算通过更新商店来显示一个 ActivityIndi​​cator。
  • 我创建了一个名为 ActIndicatorActivityIndicator 组件。
  • 我有一个包含应用程序的主要 App 组件。
  • 我有一个 Root 组件,它包装了 ActIndicatorApp 组件。

Root组件的render方法最终代码如下:

render() {

    if (this.state.showActivityIndicator) {
        return(
            <ActIndicator>
                <App />
            </ActIndicator>
        )
    }

    return (</App>)

}

我尝试了几种方法,但都无法成功。

我猜我的大脑已经停止了。

我也猜可能有逻辑错误。

【问题讨论】:

    标签: javascript reactjs react-native react-native-component


    【解决方案1】:
    const withLoadingOverlay = (Component) => class withLoadingOverlayView extends React.Component {   props: withLoadingOverlayProps
    
        // Indicator view styles   loadingOverlay = (style) => (
            <View style={[style, styles.someDefaultStyles]}>
              <ActivityIndicator color={someColor} size="large" />
            </View>   )
    
          render() {
            const { pending, ...passProps } = this.props;
            const { width, height } = Dimensions.get('window');
            return (
              <View style={{ flex: 1 }}>
                <Component {...passProps} />
                {pending && this.loadingOverlay({ width, height })}
              </View>
            );   } };
    

    我曾经用 HOC 和 redux 操作包装整个容器以设置启动挂起道具 true 和成功或失败设置为 false 所以这个道具将被 HOC 消耗并且指示器将仅在设置挂起时显示真的。

    在容器中你必须将组件包装在连接中

    export default connect(
      (state) => ({ 
        pending: state.reducer.pending, // pending prop should be here
      }),
      (dispatch) => ({ dispatching redux actions })
    )(withLoadingOverlay(WrappedComponent));
    

    【讨论】:

      【解决方案2】:

      我觉得你小时候不应该传App,我使用它的方式更像是这样的:

      render() {
      
      if (this.state.showActivityIndicator) {
          return(
            <View>
              <ActivityIndicator />
              <App />
            </View>
          )
      }
      
      return (<App />)
      
      }
      

      但最好这样设置:

      render() {
        return (
          <View>
            <ActivityIndicator animating={this.state.showActivityIndicator} />
            <App />
          </View>
        )
      }
      

      【讨论】:

      • 谢谢@MattAft。明天我会试试你的代码。编写所有代码将是太长的行。我想要一个像你一样的答案!实际上我曾尝试过这种方式,但我一定做错了什么!在我尝试这种方式后,我可以接受答案。
      • @efkan 听起来不错,如果您明天尝试时有任何疑问或问题,请告诉我!
      • 我不得不接受 Wojtek 的回答,因为它的细节。其实你的解决方案和 Wojtek 的解决方案类似。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 2019-04-05
      • 2021-09-04
      相关资源
      最近更新 更多