javascript - React-Native 中的 NavigatorIOS 错误?

标签 javascript ios reactjs react-native navigator

我目前正在尝试使用 NavigatorIOS 从我的 React-Native 应用程序中的索引 ios 页面导航到另一个页面。但是,当我这样做时,我收到此错误并且无法加载下一页:警告:propType 失败:提供给“NavigatorIOS”的“object”类型的无效 prop“initialRoute.component”,预期为“function” .

我很确定我在过去以完全相同的方式成功地实现了这一点,但我可能遗漏了一些东西——非常感谢任何反馈!

index.ios.js中的相关代码:

onLoginPressed() {
    return (
      <React.NavigatorIOS
      style={styles.container}
      initialRoute={{
        title: 'Home',
        component: Home,
      }}/>
    );
}

render() {
  return (
    <View style={styles.container}>
      <Text style={styles.welcome}>
        Welcome!
      </Text>
    <TouchableHighlight
      onPress={() => {this.onLoginPressed()}}
      activeOpacity={75 / 100}
      underlayColor={"rgb(210,210,210)"}>
      <Text>Login</Text>
    </TouchableHighlight>
  </View>
);

}

首页相关代码:

class Home extends Component {

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

    render() {
       console.log('loaded here'); //this is never being called
       return (
         <View>
           <Text
           style={{
               color: 'black',
               fontSize: 16,
               fontWeight: 'normal',
               fontFamily: 'Helvetica Neue',
            }}>
            My Text
          </Text>
        </View>
      )
    }
  }

export default Home

最佳答案

您需要在应用程序的根目录下安装 NavigatorIOS 和初始路由:

class App extends Component {
    render() {
       return (
          <NavigatorIOS
            style={styles.container}
            initialRoute={{
              title: 'Home',
              component: Home,
            }}/>
        )
    }
}

onLoginPressed 应该将新路由推送到路由堆栈:

onLoginPressed () {
    this.props.navigator.push({
      component: Login,
    title: 'Login'
  })
}

我已经用您的代码设置了一个工作示例 here .

https://rnplay.org/apps/Cl8aPQ

关于javascript - React-Native 中的 NavigatorIOS 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37738499/

相关文章:

javascript - 子级继承父级所有属性的方式

javascript - node.js & express - 应用程序结构的全局模块和最佳实践

javascript - Jquery 每个函数在服务器上不工作

ios - 使用 AFNetworking 获取 OMDb API 数据

ios - 使用NSConditionLock卡住iOS应用

javascript - react : iterating through javascript object key values

reactjs - npx create-react-app 不工作 "Must use import to load ES Module:"

javascript - 如何将此菜单的可点击区域限制为其文本?

javascript - 布局和加载元素的异步问题

ios - 苹果提供的git和官方的git有区别吗?