简体   繁体   English

将 onPress 重定向传递给子组件 React Native

[英]Passing onPress redirect to child component React Native

I am struggling a little bit.我有点挣扎。 I have tried to create more components for my React native app but after I did it my ButtonSaving stoped redirecting to Dashboard for some reason.我曾尝试为我的 React 本机应用程序创建更多组件,但在我完成之后,我的 ButtonSaving 出于某种原因停止了重定向到仪表板。 I was trying some ways to pass onRowPress to component but without luck.我正在尝试一些方法将 onRowPress 传递给组件,但没有运气。 What do I do incorrectly here please?请问我在这里做错了什么?

Login button is working fine => redirecting to Dashboard ButtonSaving not working at all => should redirect to Dashboard登录按钮工作正常 => 重定向到仪表板 ButtonSaving 根本不起作用 => 应该重定向到仪表板

AppNavigator.js AppNavigator.js

import { createStackNavigator } from 'react-navigation-stack'
import { createAppContainer } from 'react-navigation';
import Homepage from './components/Homepage/Homepage';
import Dashboard from './components/Dashboard/Dashboard';

const AppNavigator = createStackNavigator({

  Homepage: Homepage,
  Dashboard: { screen: Dashboard},
},
{
  initialRouteName: 'Homepage',
  defaultNavigationOptions: {
    headerStyle: {
      backgroundColor: 'white',
      opacity: 70,
      borderBottomColor: 'white',
      borderColor: 'white'
    },
    headerTintColor: 'black',
    headerTitleStyle: {
      fontWeight: 'bold'
    }
  }
}
);

const Container = createAppContainer(AppNavigator);

export default Container;

Homepage.js主页.js

import React from 'react';
import { StyleSheet, Text, View, Button, Image } from 'react-native';
import {NavigationActions} from 'react-navigation';

// COMPONENTS
import ButtonSaving from './ButtonSaving/ButtonSaving';


class Homepage extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      isLoading: false
    },
    this.handleClick = this.handleClick.bind(this);
    this.onRowPress = this.onRowPress.bind(this);
  }

  handleClick() {
    const counterApp = this.state.counter;
    this.setState({
      counter: counterApp + 1,
      dashboard: 'Dashboard'
    })
  }

  onRowPress = ({ navigation }) => {

        this.props.navigation.navigate(this.state.dashboard);
    }



  render() {


    return(
      <View style={styles.container}>
        {/* LOGIN BUTTON */}
        <View style={styles.buttonContainer}>
          <View style={styles.buttonLogin}>
            <Button title="log in"
              color="white"
              onPress={() => this.props.navigation.navigate('Dashboard')}/>
          </View>
        </View>

        {/* LOGO CONTAINER */}
        <View style={styles.logoContainer}>
          <Image
            style={{height: 147, width: 170}}
            source= {require('./REACT_NATIVE/AwesomeProject/logo.png')}
            ></Image>
        </View>

        {/* EXPLAINATION OF WALT */}
        <Text style={styles.textContainer}>Lorem ipsum lorem upsum></Text>

        {/* Needs to be refactored to VIEW */}
        <ButtonSaving onPress={() => this.onRowPress}/>

      </View>)
}

ButtonSaving.js ButtonSaving.js

import React from 'react';
import { StyleSheet, Text, View, Button, Image, TouchableOpacity } from 'react-native';
import { LinearGradient } from 'expo-linear-gradient';


class ButtonSaving extends React.Component {
  constructor(props) {
    super(props);
    this.state = {

    },
    this.onRowPress = this.onRowPress.bind(this);
  }

  onRowPress = ({ navigation }) => {

        this.props.navigation.navigate(this.state.dashboard);
    }

  render(){

    return(
        <View style={styleButton.container}>
          <LinearGradient
            colors={[
              '#00b38f',
              '#7dcf5a'
            ]}
            style={styleButton.opacityContainer}>
            <TouchableOpacity>
              <Button
                title="Start Saving"
                color='white'
                onPress={this.onRowPress}/>
            </TouchableOpacity>
          </LinearGradient>
        </View>
    )
  }
}


const styleButton = StyleSheet.create({
  container: {
    display: 'flex',
    flexDirection: 'row',
    flexWrap: 'wrap',
    width: '100%',
    height: 50,
    justifyContent: 'center',
    marginTop: '39%'
  },
  opacityContainer: {
    height: 48,
    borderRadius: 25,
    backgroundColor: 'darkgreen',
    width: '70%',
    justifyContent: 'center',
    alignItems: 'center'
  }
})



export default ButtonSaving;

You miss to put dashboard in your state in ButtonSaving.js您错过了在 ButtonSaving.js 中将仪表板置于您的状态

In Homepage.js when your are calling handleClick ?.在 Homepage.js 中,当您调用 handleClick 时? Dunno how you got that working...不知道你是怎么做到的...

You say in the onRowPress this:你在onRowPress说:

this.props.navigation.navigate(this.state.dashboard);

but I don't see anywhere that you set this.state.dashboard.但我没有看到你设置 this.state.dashboard 的任何地方。

Probabbly you missed set it up.可能你错过了设置。

There is no point to save "dashboard" in the Homepage state or the ButtonSaving state.Homepage状态或ButtonSaving状态下保存“仪表板”没有意义。

In Homepage.js you don't need to pass onPress to ButtonSavingHomepage.js您不需要将 onPress 传递给ButtonSaving

 ...
        <ButtonSaving navigation={this.props.navigation}/>
 ...

Next in ButtonSaving.js ButtonSaving.js下一步

  onRowPress = () => {
        this.props.navigation.navigate('Dashboard');
    }

It was simple refactor and this helped!这是简单的重构,这有帮助!

<ButtonSaving navigation ={this.props.navigation}/>

I will update solution for others later.稍后我会为其他人更新解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM