【问题标题】:React Native Stack navigator - onPress to another pageReact Native Stack navigator - onPress 到另一个页面
【发布时间】:2020-12-02 01:42:02
【问题描述】:

我是本机反应的新手,我正在尝试让我的按钮导航到 SignUpEmail 页面。我最初在 GetStartedButton 代码中有 onPress,但最近尝试将其放入实际的welcome.js 页面,因为它可以从正在渲染的堆栈中看到导航器。我似乎无法弄清楚如何让我的按钮将我引导到我想要的页面!非常感谢任何见解!谢谢。

我在 GetStartedButton.js 的第 23 行收到错误消息,它在 onPress 之后立即指向括号集

App.js

import React, { Component } from 'react';
import { StyleSheet, Text, View, Button} from 'react-native';
import WelcomePage from './src/pages/Welcome';
import SignUpEmailPage from './src/pages/SignUpEmail'

import {createStackNavigator} from '@react-navigation/stack';

const Stack = createStackNavigator();


export default class App extends Component {
 
  createHomeStack = () =>
  <Stack.Navigator>
    <Stack.Screen name="Welcome" component= {Welcome}/>
    <Stack.Screen name="SignUpEmail" component= {SignUpEmail}/>
  </Stack.Navigator>
 
  render() {
    return (
      <View style={styles.welcomeScreen}>
       <WelcomePage/>
      </View>


    )
  }
}

const styles = StyleSheet.create({
  welcomeScreen: {
    flex: 1,
    backgroundColor: 'black'  
  },
  signupemailScreen: {
    flex: 1,
    backgroundColor: '#272933' 

  }
}
);

GetStartedButton.js

import React, { Component } from 'react';
import { StyleSheet, Text, TouchableOpacity, Animated  } from 'react-native';
import { LinearGradient } from 'expo-linear-gradient';

export default class GetStartedButton extends Component {
  
    constructor(props) {
      super(props)
  
      this.fadeAnimation = new Animated.Value(0)
    }
    componentDidMount() {
        Animated.timing(this.fadeAnimation, {
          toValue: 1,
          duration: 5000,
          useNativeDriver: true,      
        }).start()
      }

    render(){
        return(
        <Animated.View style={[styles.container, { opacity: this.fadeAnimation}]}>
        <TouchableOpacity onPress={() => this.props.navigation.navigate('SignUpEmail')} >
            <LinearGradient   
                 
            colors={['#DB004C','#FC008E']}
            style={styles.linearGradient}
            start={{ x: 0, y: 0.5 }}
            end={{ x: 1, y: 0.5 }}
            >
              
                <Text style={styles.text}>
                    Get Started
                </Text>     
               
             </LinearGradient>
        </TouchableOpacity>
        </Animated.View>
    )
    
}
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
        
    },
    linearGradient: {
        alignItems: 'center',
        justifyContent: 'center',
        borderRadius: 10,
        width: 340,
        padding: 20,
    },

    text: {
        color: 'white',
        fontSize: 20,
        justifyContent: 'center',
    alignItems: 'center',
    }
});




Welcome.js

import { createStackNavigator } from '@react-navigation/stack';
import React, { Component } from 'react';
import { StyleSheet, Text, View, Animated, TouchableOpacity} from 'react-native';
import GetStartedButton from '../components/GetStartedButton';



export default class WelcomePage extends Component {
  
  constructor(props) {
    super(props)

    this.fadeAnimation = new Animated.Value(0)
  }

  componentDidMount() {
    Animated.timing(this.fadeAnimation, {
      toValue: 1,
      duration: 5000,
      useNativeDriver: true,      
    }).start()
  }
  
  render() {
    return (
        <View style={styles.containerMain}>
        <View style={styles.containerClub}>
        <Animated.Text style={[styles.title, { opacity: this.fadeAnimation}]}>Word</Animated.Text>
        </View>
        
      <View style={styles.containerCaption}>   
        <Animated.Text style={[styles.caption, { opacity: this.fadeAnimation}]}> words words words  </Animated.Text> 
      </View>
          
      <View style={styles.containerBottom}>   

        
      
          <GetStartedButton/>
         
        

        
      </View>
    </View>

    );
    }
}



const styles = StyleSheet.create({
    containerMain: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'black',
    },

containerClub: {
    position: 'absolute',
    bottom: 288
  },

  containerCaption: {
    position: 'absolute',
    bottom: 250

  },
  /* To style the button and positioning*/
  containerBottom: {
    
    position: 'absolute',
    bottom: 100
  },
  /* To style "Word"*/
  title: {
    color: 'white',
    fontSize: 35,
    fontWeight: "bold",

  },
  /* To style "Words words words"*/
  caption:
  {
   color: 'white',
    fontSize: 16
  },
  line: {
    borderWidth: 0.5,
    borderColor:'black',
     margin:10,
  }
}
)

【问题讨论】:

    标签: javascript css react-native


    【解决方案1】:

    最终输出:

    进行以下更改:

    App.js

    import React, { Component } from 'react';
    import { StyleSheet, Text, View, Button } from 'react-native';
    import { NavigationContainer } from '@react-navigation/native';
    import { createStackNavigator } from '@react-navigation/stack';
    import WelcomePage from './WelCome';
    import SignUpEmailPage from './Signup';
    
    const Stack = createStackNavigator();
    
    export default class App extends Component {
      render() {
        return (
          <NavigationContainer>
            <Stack.Navigator>
              <Stack.Screen name="Welcome" component={WelcomePage} />
              <Stack.Screen name="SignUpEmail" component={SignUpEmailPage} />
            </Stack.Navigator>
          </NavigationContainer>
        );
      }
    }
    
    

    Welcome.js:

    import React, { Component } from 'react';
    import {
      StyleSheet,
      Text,
      View,
      Animated,
      TouchableOpacity,
    } from 'react-native';
    import GetStartedButton from './GetStartedButton';
    
    export default class WelcomePage extends Component {
      constructor(props) {
        super(props);
    
        this.fadeAnimation = new Animated.Value(0);
      }
    
      componentDidMount() {
        Animated.timing(this.fadeAnimation, {
          toValue: 1,
          duration: 5000,
          useNativeDriver: true,
        }).start();
      }
    
      render() {
        return (
          <View style={styles.containerMain}>
            <View style={styles.containerClub}>
              <Animated.Text
                style={[styles.title, { opacity: this.fadeAnimation }]}>
                Word
              </Animated.Text>
            </View>
    
            <View style={styles.containerCaption}>
              <Animated.Text
                style={[styles.caption, { opacity: this.fadeAnimation }]}>
                words words words
              </Animated.Text>
            </View>
    
            <View style={styles.containerBottom}>
              <GetStartedButton
                onPress={() => this.props.navigation.navigate('SignUpEmail')}
              />
            </View>
          </View>
        );
      }
    }
    
    

    GetStartedButton.js:

    import React, { Component } from 'react';
    import { StyleSheet, Text, TouchableOpacity, Animated } from 'react-native';
    import { LinearGradient } from 'expo-linear-gradient';
    
    export default class GetStartedButton extends Component {
      constructor(props) {
        super(props);
    
        this.fadeAnimation = new Animated.Value(0);
      }
      componentDidMount() {
        Animated.timing(this.fadeAnimation, {
          toValue: 1,
          duration: 5000,
          useNativeDriver: true,
        }).start();
      }
    
      render() {
        return (
          <Animated.View
            style={[styles.container, { opacity: this.fadeAnimation }]}>
            <TouchableOpacity
              onPress={() => {
                this.props.onPress();
              }}>
              <LinearGradient
                colors={['#DB004C', '#FC008E']}
                style={styles.linearGradient}
                start={{ x: 0, y: 0.5 }}
                end={{ x: 1, y: 0.5 }}>
                <Text style={styles.text}>Get Started</Text>
              </LinearGradient>
            </TouchableOpacity>
          </Animated.View>
        );
      }
    }
    

    这是工作的Expo Snack Demo

    【讨论】:

    • 太棒了!我非常感谢您的帮助,我将如何编辑顶部的白条?我用谷歌搜索了 10 分钟,但似乎找不到任何好的信息。你能指出我在哪里可以找到这样做的方向吗?谢谢
    • 你想用那个白色的状态栏做什么?
    • 这里有一篇关于如何自定义它的好文章:aboutreact.com/…
    • 那篇文章是黄金。你是男人!谢谢
    • 不客气,在应用样式方面做得很好。看起来很时尚。
    【解决方案2】:

    您已创建堆栈但未在 App.js 中呈现它。相反,您渲染欢迎页面直接将创建的堆栈添加到返回方法:

    export default class App extends Component {
     
      createHomeStack = () =>
      <Stack.Navigator>
        <Stack.Screen name="Welcome" component= {Welcome}/>
        <Stack.Screen name="SignUpEmail" component= {SignUpEmail}/>
      </Stack.Navigator>
     
      render() {
        return (
          <View style={styles.welcomeScreen}>
           {this.createHomeStack()}
          </View>
    
    
        )
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      相关资源
      最近更新 更多