Javascript 反应本机StackNavigator

Javascript 反应本机StackNavigator,javascript,react-native,navigation,react-redux,Javascript,React Native,Navigation,React Redux,好了,伙计们,我有一个问题,我是一个新的反应原生开发和有一个问题与StackNavigator。当我按下一个按钮来浏览当前屏幕时,它会给我一个类似这样的错误: 这是我正在使用的代码,我无法让它工作 static navigationOptions = { title: 'Welcome', }; <Button onPress={() => navigate('News')} title="News" />

好了,伙计们,我有一个问题,我是一个新的反应原生开发和有一个问题与StackNavigator。当我按下一个按钮来浏览当前屏幕时,它会给我一个类似这样的错误:

这是我正在使用的代码,我无法让它工作

   static navigationOptions = {
     title: 'Welcome',
     };

    <Button
      onPress={() => navigate('News')}
      title="News"
    />

    const NewsApp = StackNavigator({
    Home: { screen: Splash },
    News: { screen: News }
    });
静态导航选项={
标题:"欢迎",,
};
导航('News')}
title=“新闻”
/>
const NewsApp=StackNavigator({
主页:{screen:Splash},
新闻:{屏幕:新闻}
});

在渲染函数中,必须按如下方式提取导航器:

render()
{
  const { navigate } = this.props.navigation;
  return (
    <Button
      onPress={() => navigate('News')}
      title="News"
    />
  )
}
render()
{
const{navigate}=this.props.navigation;
返回(
导航('News')}
title=“新闻”
/>
)
}
堆栈导航入门:

//First Screen

      import {
      StackNavigator,
    } from 'react-navigation';
//Added screens to navigate.    
    const BasicApp = StackNavigator({
      Main: {screen: MainScreen},
      Profile: {screen: ProfileScreen},
    });
//Second screen (Main Screen)
//This is a welcome screen which appears first in the route declared in the previous code
    class MainScreen extends React.Component {
      static navigationOptions = {
        title: 'Welcome',
      };
      render() {
      //constant for navigation 
        const { navigate } = this.props.navigation;
        return (
          <Button
            title="Go to Jane's profile"
            onPress={() =>
            navigate('Profile', { name: 'Jane' })//Navigate to Profile with name
            }
          />
        );
      }
    }

//Third screen (Profile screen)

    class ProfileScreen extends React.Component {
      static navigationOptions = ({navigation}) => ({
        title: navigation.state.params.name, //extracted name from the params
      });
      render() {
        const { goBack } = this.props.navigation;//used to goBack prop
        return (
          <Button
            title="Go back"
            onPress={() => goBack()} 
          />
        );
      }
    }
//第一屏
进口{
StackNavigator,
}从“反应导航”;
//增加了导航屏幕。
const BasicApp=StackNavigator({
Main:{screen:MainScreen},
配置文件:{screen:ProfileScreen},
});
//第二屏(主屏)
//这是一个欢迎屏幕,它首先出现在前面代码中声明的路线中
类MainScreen扩展了React.Component{
静态导航选项={
标题:"欢迎",,
};
render(){
//导航常数
const{navigate}=this.props.navigation;
返回(
导航('Profile',{name:'Jane'})//导航到名为
}
/>
);
}
}
//第三屏幕(配置文件屏幕)
类ProfileScreen扩展了React.Component{
静态导航选项=({navigation})=>({
标题:navigation.state.params.name,//从参数中提取名称
});
render(){
const{goBack}=this.props.navigation;//用于goBack prop
返回(
goBack()}
/>
);
}
}

干杯:)

在渲染函数中,您必须像这样提取导航器:

render()
{
  const { navigate } = this.props.navigation;
  return (
    <Button
      onPress={() => navigate('News')}
      title="News"
    />
  )
}
render()
{
const{navigate}=this.props.navigation;
返回(
导航('News')}
title=“新闻”
/>
)
}
堆栈导航入门:

//First Screen

      import {
      StackNavigator,
    } from 'react-navigation';
//Added screens to navigate.    
    const BasicApp = StackNavigator({
      Main: {screen: MainScreen},
      Profile: {screen: ProfileScreen},
    });
//Second screen (Main Screen)
//This is a welcome screen which appears first in the route declared in the previous code
    class MainScreen extends React.Component {
      static navigationOptions = {
        title: 'Welcome',
      };
      render() {
      //constant for navigation 
        const { navigate } = this.props.navigation;
        return (
          <Button
            title="Go to Jane's profile"
            onPress={() =>
            navigate('Profile', { name: 'Jane' })//Navigate to Profile with name
            }
          />
        );
      }
    }

//Third screen (Profile screen)

    class ProfileScreen extends React.Component {
      static navigationOptions = ({navigation}) => ({
        title: navigation.state.params.name, //extracted name from the params
      });
      render() {
        const { goBack } = this.props.navigation;//used to goBack prop
        return (
          <Button
            title="Go back"
            onPress={() => goBack()} 
          />
        );
      }
    }
//第一屏
进口{
StackNavigator,
}从“反应导航”;
//增加了导航屏幕。
const BasicApp=StackNavigator({
Main:{screen:MainScreen},
配置文件:{screen:ProfileScreen},
});
//第二屏(主屏)
//这是一个欢迎屏幕,它首先出现在前面代码中声明的路线中
类MainScreen扩展了React.Component{
静态导航选项={
标题:"欢迎",,
};
render(){
//导航常数
const{navigate}=this.props.navigation;
返回(
导航('Profile',{name:'Jane'})//导航到名为
}
/>
);
}
}
//第三屏幕(配置文件屏幕)
类ProfileScreen扩展了React.Component{
静态导航选项=({navigation})=>({
标题:navigation.state.params.name,//从参数中提取名称
});
render(){
const{goBack}=this.props.navigation;//用于goBack prop
返回(
goBack()}
/>
);
}
}

干杯:)

如果您确实试图从创建导航器的组件进行导航,您将无法访问导航器,因为它只向
新闻应用程序
中的任何内容注入导航


您应该在导航堆栈中最上面的组件内处理反压。但是,如果您需要在某些位置控制导航,例如您拥有的位置,那么实施redux可能是一个很好的解决方案。

如果您确实试图从创建它的组件进行导航,您将无法访问导航器,因为它只向
NewsApp
中的任何内容注入导航


您应该在导航堆栈中最上面的组件内处理反压。但是,如果您需要在某些位置控制导航,例如您拥有的位置,那么实现redux可能是一个很好的解决方案。

这是导出默认类Splash extends组件的第一部分{static navigationOptions={title:'Welcome',};render(){const{navigate}='this.props.navigation';return(News navigate('News')}title=“News”/>);}}}这是第二部分类聊天扩展React.Component{static navigationOptions={title:'Chat with Lucy',};render(){const{navigate}='this.props.navigation';return(与Lucy聊天);}}const NewsApp=StackNavigator({Home:{screen:Splash},News:{screen:Chat});为什么在引号中保留const{navigate}='this.props.navigation'?删除引号并再次运行代码在我删除引号后,它说undefined不是一个对象(评估'this.props.navigation.navigate')。请将上述代码添加到问题中,并向我解释应用程序崩溃的原因。这是导出默认类Splash extends组件的第一部分{static navigationOptions={title:'Welcome',};render(){const{navigate}='this.props.navigation';return(News-navigate('News')}title=“News”/>);}}}这是第二部分类Chat.Component{static navigationOptions={title:'Chat with Lucy',};render(){const{navigate}='this.props.navigation';return(Chat with Lucy);}}}const NewsApp=StackNavigator({Home:{screen:Splash},News:{screen:Chat});你为什么一直保持const{navigate}='this.props.navigation'在quot中