【问题标题】:React Native: Maximum Call Stack Size Exceeded Error After Adding ConditionalReact Native:添加条件后超出最大调用堆栈大小错误
【发布时间】:2017-12-15 00:14:07
【问题描述】:

我收到错误“超出最大调用堆栈大小”。在我的代码中向导航文件添加条件后。我使用的堆栈是带有 Expo、Redux、GraphQL 的 React-Native,而我的导航库是 react-navigation。我正在开发 iOS 模拟器。这是打破 应用:

//navigations.js     
if (!this.props.user.isAuthenticated) {
          return <AuthenticationScreen />;
        }

这就是navigations.js的全部内容

import React, { Component } from "react";
import {
  addNavigationHelpers,
  StackNavigator,
  TabNavigator
} from "react-navigation";
import { connect } from "react-redux";
import { FontAwesome } from "@expo/vector-icons";

import HomeScreen from "./screens/HomeScreen";
import ExploreScreen from "./screens/ExploreScreen";
import FavoritesScreen from "./screens/FavoritesScreen";
import ProfileScreen from "./screens/ProfileScreen";
import AuthenticationScreen from "./screens/AuthenticationScreen";

import { colors } from "./utils/constants";

const TAB_ICON_SIZE = 20;

const Tabs = TabNavigator(
  {
    Home: {
      screen: HomeScreen,
      navigationOptions: () => ({
        headerTitle: "Saga",
        tabBarIcon: ({ tintColor }) => (
          <FontAwesome size={TAB_ICON_SIZE} color={tintColor} name="home" />
        )
      })
    },
    Explore: {
      screen: ExploreScreen,
      navigationOptions: () => ({
        headerTitle: "Explore",
        tabBarIcon: ({ tintColor }) => (
          <FontAwesome size={TAB_ICON_SIZE} color={tintColor} name="search" />
        )
      })
    },
    Favorites: {
      screen: FavoritesScreen,
      navigationOptions: () => ({
        headerTitle: "Favorites",
        tabBarIcon: ({ tintColor }) => (
          <FontAwesome
            size={TAB_ICON_SIZE}
            color={tintColor}
            name="file-video-o"
          />
        )
      })
    },
    Profile: {
      screen: ProfileScreen,
      navigationOptions: () => ({
        headerTitle: "Profile",
        tabBarIcon: ({ tintColor }) => (
          <FontAwesome size={TAB_ICON_SIZE} color={tintColor} name="user" />
        )
      })
    }
  },
  {
    lazy: true,
    tabBarPosition: "bottom",
    swipeEnabled: false,
    tabBarOptions: {
      showIcon: true,
      showLabel: false,
      activeTintColor: colors.PRIMARY,
      inactiveTintColor: colors.LIGHT_GRAY,
      style: {
        backgroundColor: colors.BASE_GRAY,
        height: 50,
        paddingVertical: 5
      }
    }
  }
);

const AppMainNav = StackNavigator(
  {
    Home: {
      screen: Tabs
    }
  },
  {
    cardStyle: {
      backgroundColor: "#F1F6FA"
    },
    navigationOptions: () => ({
      headerStyle: {
        backgroundColor: colors.BASE_GRAY
      },
      headerTitleStyle: {
        fontWeight: "bold",
        fontSize: 24,
        color: colors.BLUE
      }
    })
  }
);

class AppNavigator extends Component {
  render() {
    const nav = addNavigationHelpers({
      dispatch: this.props.dispatch,
      state: this.props.nav
    });
    if (!this.props.user.isAuthenticated) {
      return <AuthenticationScreen />;
    }
    return <AppMainNav navigation={nav} />;
  }
}

export default connect(state => ({
  nav: state.nav,
  user: state.user
}))(AppNavigator);

export const router = AppMainNav.router;

这是 user.js 减速器:

const initialState = {
  token: null,
  isAuthenticated: false,
  info: null
};

export default (state = initialState, action) => {
  switch (action.type) {
    default:
      return state;
  }
};

我看不出是什么导致了无限循环。

更新 #1

AuthenticationScreen.js(减去导入)

const Root = styled.View``;

const T = styled.Text``;

class AuthenticationScreen extends Component {
  state = {};
  render() {
    return (
      <Root>
        <T>AuthenticationScreen</T>
      </Root>
    );
  }
}

export default AuthenticationScreen;

【问题讨论】:

    标签: ios react-native redux react-navigation


    【解决方案1】:

    据我了解,您已将问题与此代码隔离开来......

    if (!this.props.user.isAuthenticated) {
      return <AuthenticationScreen />;
    }
    return <AppMainNav navigation={nav} />;
    

    如果你删除...

    if (!this.props.user.isAuthenticated) {
      return <AuthenticationScreen />;
    }
    

    它工作正常,这意味着 必须没问题。所以无限循环必须在你的 代码中。

    【讨论】:

    • 当您注释掉应用程序的代码时,这是正确的。 中的某些内容被破坏是我最初的想法,但目前它只是设置为呈现文本。这是减去导入的代码。 ` const Root = styled.View; const T = styled.Text;类 AuthenticationScreen 扩展组件 { state = {}; render() { return ( AuthenticationScreen ); } } 导出默认 AuthenticationScreen;`
    • 我将开始进一步隔离它,进一步简化它,直到您确定具体问题。注释掉 AuthenticationScreen 导入并将其替换为 const AuthenticationScreen = () =&gt; &amp;lt;View&gt;&amp;lt;Text&gt;Auth Screen&amp;lt;/Text&gt;&amp;lt;/View&gt;。如果可行,请将其移动到导入文件中并在没有样式元素的情况下尝试。等等等等。慢慢增加复杂性,直到您确定它中断的确切点。
    • 我将类组件更改为您所说的无状态函数,并且立即生效;然而,当我增加复杂性时,似乎什么都没有破坏应用程序。因为我正在研究应用程序的其他元素,所以我把它作为创可贴留下了。一旦到了声明initialState 的时间,我就能够将组件呈现为一个类。就像对研究同一问题的任何人的说明一样。我还尝试重新启动服务器和捆绑程序,擦除模拟器的内容,清除缓存并重置守望者,但均无济于事。虽然应用程序现在正在运行,但我永远无法确定它在哪里中断。
    • 很高兴它现在可以工作了。这是最重要的。
    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 2021-10-24
    • 2011-08-31
    • 1970-01-01
    • 2020-06-18
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多